Dynamically Change Logging Level of a Python Process

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












The intention of following code is to dynamically change the logging level of a running python process that has imported this module.



#!/usr/bin/python
import logging
import time
import signal

logging.basicConfig(level=logging.ERROR)
logger = logging.getLogger(__name__)
# create logger object with 'ERROR' level logging.

def handler1(signum, frame):
logger.setLevel(logging.DEBUG)

def handler2(signum, frame):
logger.setLevel(logging.INFO)

signal.signal(signal.SIGUSR1, handler1)
# if process receives SIGUSR1 signal, the logging level changes to DEBUG.

signal.signal(signal.SIGUSR2, handler2)
# if process receives SIGUSR1 signal, the logging level changes to INFO.

while True:
logger.info('info logging')
logger.debug('debug logging')
logger.error('error logging')
logger.warn('warn logging')
time.sleep(2)


The above script runs as expected but I am not sure if this the right way to full fill the goal. Please review the code, as well as issues in above resolution.







share|improve this question

















  • 2




    There are problems with the indentation of the code, please fix them. Is there any particular reason why you want to use signals for this?
    – Daniel
    Jun 9 at 18:04










  • there is no particular reason of using the signal.
    – Abhinav Agarwal
    Jun 10 at 4:15

















up vote
2
down vote

favorite












The intention of following code is to dynamically change the logging level of a running python process that has imported this module.



#!/usr/bin/python
import logging
import time
import signal

logging.basicConfig(level=logging.ERROR)
logger = logging.getLogger(__name__)
# create logger object with 'ERROR' level logging.

def handler1(signum, frame):
logger.setLevel(logging.DEBUG)

def handler2(signum, frame):
logger.setLevel(logging.INFO)

signal.signal(signal.SIGUSR1, handler1)
# if process receives SIGUSR1 signal, the logging level changes to DEBUG.

signal.signal(signal.SIGUSR2, handler2)
# if process receives SIGUSR1 signal, the logging level changes to INFO.

while True:
logger.info('info logging')
logger.debug('debug logging')
logger.error('error logging')
logger.warn('warn logging')
time.sleep(2)


The above script runs as expected but I am not sure if this the right way to full fill the goal. Please review the code, as well as issues in above resolution.







share|improve this question

















  • 2




    There are problems with the indentation of the code, please fix them. Is there any particular reason why you want to use signals for this?
    – Daniel
    Jun 9 at 18:04










  • there is no particular reason of using the signal.
    – Abhinav Agarwal
    Jun 10 at 4:15













up vote
2
down vote

favorite









up vote
2
down vote

favorite











The intention of following code is to dynamically change the logging level of a running python process that has imported this module.



#!/usr/bin/python
import logging
import time
import signal

logging.basicConfig(level=logging.ERROR)
logger = logging.getLogger(__name__)
# create logger object with 'ERROR' level logging.

def handler1(signum, frame):
logger.setLevel(logging.DEBUG)

def handler2(signum, frame):
logger.setLevel(logging.INFO)

signal.signal(signal.SIGUSR1, handler1)
# if process receives SIGUSR1 signal, the logging level changes to DEBUG.

signal.signal(signal.SIGUSR2, handler2)
# if process receives SIGUSR1 signal, the logging level changes to INFO.

while True:
logger.info('info logging')
logger.debug('debug logging')
logger.error('error logging')
logger.warn('warn logging')
time.sleep(2)


The above script runs as expected but I am not sure if this the right way to full fill the goal. Please review the code, as well as issues in above resolution.







share|improve this question













The intention of following code is to dynamically change the logging level of a running python process that has imported this module.



#!/usr/bin/python
import logging
import time
import signal

logging.basicConfig(level=logging.ERROR)
logger = logging.getLogger(__name__)
# create logger object with 'ERROR' level logging.

def handler1(signum, frame):
logger.setLevel(logging.DEBUG)

def handler2(signum, frame):
logger.setLevel(logging.INFO)

signal.signal(signal.SIGUSR1, handler1)
# if process receives SIGUSR1 signal, the logging level changes to DEBUG.

signal.signal(signal.SIGUSR2, handler2)
# if process receives SIGUSR1 signal, the logging level changes to INFO.

while True:
logger.info('info logging')
logger.debug('debug logging')
logger.error('error logging')
logger.warn('warn logging')
time.sleep(2)


The above script runs as expected but I am not sure if this the right way to full fill the goal. Please review the code, as well as issues in above resolution.









share|improve this question












share|improve this question




share|improve this question








edited Jun 10 at 4:11
























asked Jun 9 at 17:32









Abhinav Agarwal

11916




11916







  • 2




    There are problems with the indentation of the code, please fix them. Is there any particular reason why you want to use signals for this?
    – Daniel
    Jun 9 at 18:04










  • there is no particular reason of using the signal.
    – Abhinav Agarwal
    Jun 10 at 4:15













  • 2




    There are problems with the indentation of the code, please fix them. Is there any particular reason why you want to use signals for this?
    – Daniel
    Jun 9 at 18:04










  • there is no particular reason of using the signal.
    – Abhinav Agarwal
    Jun 10 at 4:15








2




2




There are problems with the indentation of the code, please fix them. Is there any particular reason why you want to use signals for this?
– Daniel
Jun 9 at 18:04




There are problems with the indentation of the code, please fix them. Is there any particular reason why you want to use signals for this?
– Daniel
Jun 9 at 18:04












there is no particular reason of using the signal.
– Abhinav Agarwal
Jun 10 at 4:15





there is no particular reason of using the signal.
– Abhinav Agarwal
Jun 10 at 4:15











1 Answer
1






active

oldest

votes

















up vote
2
down vote













Logger.setLevel() is the right way to dynamically change the logging level of a Logger object. Your script will work fine.



I'll nitpick:



  • Code is read from top to bottom. You should place the comments above the group of statements they are attached to, not below them. If the reader reads the comment first, they won't have to spend extra effort to decipher the meaning of the following lines, because they have read the annotation first.


  • Be wary of using asynchronous signal handlers when you are using the logging module. I know that this is a tiny example script, but always refer to the documentation when you are using the Python standard library.






share|improve this answer





















    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196175%2fdynamically-change-logging-level-of-a-python-process%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    Logger.setLevel() is the right way to dynamically change the logging level of a Logger object. Your script will work fine.



    I'll nitpick:



    • Code is read from top to bottom. You should place the comments above the group of statements they are attached to, not below them. If the reader reads the comment first, they won't have to spend extra effort to decipher the meaning of the following lines, because they have read the annotation first.


    • Be wary of using asynchronous signal handlers when you are using the logging module. I know that this is a tiny example script, but always refer to the documentation when you are using the Python standard library.






    share|improve this answer

























      up vote
      2
      down vote













      Logger.setLevel() is the right way to dynamically change the logging level of a Logger object. Your script will work fine.



      I'll nitpick:



      • Code is read from top to bottom. You should place the comments above the group of statements they are attached to, not below them. If the reader reads the comment first, they won't have to spend extra effort to decipher the meaning of the following lines, because they have read the annotation first.


      • Be wary of using asynchronous signal handlers when you are using the logging module. I know that this is a tiny example script, but always refer to the documentation when you are using the Python standard library.






      share|improve this answer























        up vote
        2
        down vote










        up vote
        2
        down vote









        Logger.setLevel() is the right way to dynamically change the logging level of a Logger object. Your script will work fine.



        I'll nitpick:



        • Code is read from top to bottom. You should place the comments above the group of statements they are attached to, not below them. If the reader reads the comment first, they won't have to spend extra effort to decipher the meaning of the following lines, because they have read the annotation first.


        • Be wary of using asynchronous signal handlers when you are using the logging module. I know that this is a tiny example script, but always refer to the documentation when you are using the Python standard library.






        share|improve this answer













        Logger.setLevel() is the right way to dynamically change the logging level of a Logger object. Your script will work fine.



        I'll nitpick:



        • Code is read from top to bottom. You should place the comments above the group of statements they are attached to, not below them. If the reader reads the comment first, they won't have to spend extra effort to decipher the meaning of the following lines, because they have read the annotation first.


        • Be wary of using asynchronous signal handlers when you are using the logging module. I know that this is a tiny example script, but always refer to the documentation when you are using the Python standard library.







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jun 13 at 0:22









        Berk Özbalcı

        20113




        20113






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196175%2fdynamically-change-logging-level-of-a-python-process%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Greedy Best First Search implementation in Rust

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            C++11 CLH Lock Implementation