Saturday, 26 December 2015

Python: Log message using log method

By using ‘Logger.log’ method you can log messages.

Syntax
Logger.log(lvl, msg, *args, **kwargs)

Logs a message with integer level lvl on this logger. ‘logger.log(logging.DEBUG, "Debug message")’ logs a message with level DEBUG.
import logging

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s')

def logMessages():
    logger.log(logging.DEBUG, "Debug message")
    logger.log(logging.INFO,"Information Message")
    logger.log(logging.WARN,"Warning Message")
    logger.log(logging.ERROR,"Error Message")
    logger.log(logging.CRITICAL,"Critical Message")

logMessages()


Run above program, you will get following output.
2015-10-23 00:12:08,291 __main__     DEBUG    Debug message
2015-10-23 00:12:08,291 __main__     INFO     Information Message
2015-10-23 00:12:08,291 __main__     WARNING  Warning Message
2015-10-23 00:12:08,291 __main__     ERROR    Error Message
2015-10-23 00:12:08,291 __main__     CRITICAL Critical Message



Previous                                                 Next                                                 Home

No comments:

Post a Comment