1 Upvote

Creating Logs Using Python Logger

Python
General Python

In this code snippet we set the logger to output log messages to a log file named 'train.log'. Each message will be time stamped and include the function name where the logging occurs. The lowest logging level is set to info so all debug messages will be ignored.

import logging

logging.basicConfig(format='%(asctime)s[%(levelname)s] %(funcName)s: %(message)s',
                        datefmt='%d/%m/%Y %I:%M:%S %p',
                        filename='../logs/train.log',
                        level=logging.INFO)

# logger set to name of the script
LOG = logging.getLogger('__name__')

LOG.info('Start')
LOG.info('End')

"""List of log levels:
LOG.debug() 
LOG.info() 
LOG.warning()
LOG.error() 
LOG.critical()
LOG.exception()
"""

By analyseup - Last Updated June 11, 2022, 4 p.m.

Did you find this snippet useful?

Sign up to bookmark this in your snippet library

COMMENTS
RELATED SNIPPETS
Python For Else Loop
Python
General Python

Loop

2
Top Contributors
103
100