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

Dan Campbell dcwhatthe at gmail.com
Fri Mar 27 15:56:54 EDT 2020


Cameron, thanks.  I read something similar elsewhere, but I don't
understand what levels to set, and how many times.

Are we saying that we execute

    root_logger = logging.getLogger()
    root_logger.setLevel(1)  #Or some other level

, prior to running


 logging.basicConfig(...),

for each type of logging.

So like this?


    root_logger = logging.getLogger()
    root_logger.setLevel(1)  #Or some other level

logging.basicConfig( filename = "CheckConnectivity_error.log"  )


    root_logger = logging.getLogger()
    root_logger.setLevel(2)  #Or some other level

logging.basicConfig( filename = "CheckConnectivity_info.log"  )



    root_logger = logging.getLogger()
    root_logger.setLevel(3)  #Or some other level

logging.basicConfig( filename = "CheckConnectivity_debug.log"  )


?


Sorry, but I don't have the full context of this.


Regards,

DC





On Thu, Mar 26, 2020 at 5:35 PM Cameron Simpson <cs at cskk.id.au> wrote:

> 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