logging module

dieter dieter at handshake.de
Mon Jul 2 01:34:01 EDT 2018

Sharan Basappa <sharan.basappa at gmail.com> writes:
> I am trying to use logging module and somehow I cannot make it work.
> A simple code that I am trying is below. The commented code line 5,6 are other options I have tried but don't work
> #importing module
> import logging
> #Create and configure logger
> #logging.basicConfig(filename="D:/Projects/Initiatives/machine learning/programs/newfile.log",
> #logging.basicConfig(filename="newfile.log",
> logging.basicConfig(format='%(asctime)s %(message)s')

You can provide more than a single argument to "basicConfig",
e.g. "...basicConfig(filename='...', format='...')".

> ...
> #Creating an object
> logger=logging.getLogger()
> #Setting the threshold of logger to DEBUG
> logger.setLevel(logging.DEBUG)
> #Test messages
> logger.debug("Harmless debug Message")
> logger.info("Just an information")
> logger.warning("Its a Warning")
> logger.error("Did you try to divide by zero")
> logger.critical("Internet is down")
> PS: I am running this under Enthought Canopy
> The following is the output
> %run "D:/Projects/Initiatives/machine learning/programs/debug_4.py"

Not sure, that "basicConfig" (without "filename" parameter)
logs to "stdout" or "stderr" (what you seem to expect).

Apart from that, your use of the "logging" module looks okay.
It should work (and usually does).

More information about the Python-list mailing list