wavedocs
11/1/2017 - 8:12 PM

[Python3 Logging] Simple Python3 Logging Configuration #tags: logs, python3

[Python3 Logging] Simple Python3 Logging Configuration #tags: logs, python3

import logging
import structlog

# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)

# We need factory, to return application-wide logger
def logger_factory():
    return logger

structlog.configure(
    processors=[
            structlog.processors.JSONRenderer(indent=2, sort_keys=True)      
        ],
        logger_factory=logger_factory
    )

log = structlog.getLogger()
log.debug("Now you see me")
logger.setLevel(logging.ERROR)
log.debug("Now you don't")
# Keep logs quiet (so only critical messages are shown, not INFO messages)
logging.getLogger("boto").setLevel(logging.CRITICAL)
logging.getLogger("nsq").setLevel(logging.CRITICAL)
# https://docs.python.org/3/library/logging.html#logrecord-attributes

import logging
log = logging.getLogger()  # <logging.RootLogger at 0x107f72f98>
log.setLevel(logging.DEBUG)
log.debug('123')  # DEBUG:root:123
log.info('456')  # INFO:root:456

# Alternatively...
import logging
logging.basicConfig(level=logging.DEBUG, format='%(relativeCreated)6d %(threadName)s %(message)s')

# Something I like...
import logging
logging.basicConfig(
    level=logging.INFO,
    format='[%(levelname)s %(asctime)s path:%(pathname)s lineno:%(lineno)s] %(message)s',
    datefmt='%Y/%m/%d %I:%M:%S'
)