python log with timestamp 1

python log with timestamp

import logging
logging.basicConfig(
        format='%(asctime)s %(levelname)-8s %(message)s',
        level=logging.INFO,
        datefmt='%Y-%m-%d %H:%M:%S')

logging.info('an info messge')
# 2017-05-25 00:58:28 INFO     an info messge
logging.debug('a debug messag is not shown')

Here is what the above code is Doing:
1. The logging module is imported.
2. The basicConfig() function is called to configure the logging system.
3. The logging.info() function is called to log an info message.
4. The logging.debug() function is called to log a debug message.

The basicConfig() function takes a number of optional arguments.
The format argument is a format string for the message.
The level argument sets the root logger level to the specified level.
The datefmt argument specifies the format of the timestamp.

The logging.info() function takes a string as its only argument.
The logging.debug() function takes a string as its only argument.

The logging.debug() function is not shown because the root logger level is set to INFO.

Similar Posts