How come logging.error writes to a file, but not logging.debug or logging.info?

Cameron Simpson cs at cskk.id.au
Thu Mar 26 17:35:08 EDT 2020


On 26Mar2020 14:02, dcwhatthe at gmail.com <dcwhatthe at gmail.com> wrote:
>When we run
>
>    logging.basicConfig( filename = "TestLogging_" +  datetime.datetime.now().strftime("%Y%m%d_%H%M%S") + ".log" )
>
>, and then
>
>    logging.error( "Test01\n" )
>    logging.debug("Test02\n")
>    logging.info("Test03\n")
>    logging.error( "Test04\n" )
>
>, only the error log lines get sent to the file.
>
>How do I get info() and debug() to go to the file, as well?

You do this by adjusting the logging level of your programme. The idea 
is that you can put logging calls all through your code at suitable 
levels (info, error, warning debug) and change the logging verbosity 
just by adjusting the logging level of the logger involved.

So I often have a sniff around at startup in my programmes where I set 
up the logging, and default to logging.WARNING unless stderr is a 
terminal (inferring "interactive") and use logging.INFO. A command line 
switch or environment variable might be used to override these defaults.  
Having picked a level:

    root_logger = logging.getLogger()
    root_logger.setLevel(level)

sets the level of the root logger, thus changing the verbosity.

Obviously adjust if you've got a special purpose logger rather than the 
root logger.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-list mailing list