Skip to content

Instantly share code, notes, and snippets.

@liviaerxin
Last active March 8, 2023 06:50
Show Gist options
  • Save liviaerxin/4ecd56ac408361b68f71d35114197035 to your computer and use it in GitHub Desktop.
Save liviaerxin/4ecd56ac408361b68f71d35114197035 to your computer and use it in GitHub Desktop.
Python Logging Configuration Examples Best Practice #python

Python Logging Configuration Examples

Quick And Dirty Python Logging Lesson

Python Logging

Using an INI File

Using a Dictionary

Using Code

Project Structure,

projetctA
    logging_config.py
    main.py
    others.py

in logging_config.py

import logging
import sys
from logging.handlers import TimedRotatingFileHandler

FORMATTER = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
LOG_FILE = "my_app.log"

def create_console_handler():
	handler = logging.StreamHandler(sys.stdout)
	handler.setFormatter(FORMATTER)
	return handler

# [Python logging split between stdout and stderr](https://stackoverflow.com/questions/16061641/python-logging-split-between-stdout-and-stderr)
def create_console_out_handler():
	handler = logging.StreamHandler(sys.stdout)
	handler.setFormatter(FORMATTER)
	handler.setLevel(logging.DEBUG)
	handler.addFilter(lambda record: record.levelno <= logging.INFO)
	return handler

def create_console_err_handler():
	handler = logging.StreamHandler(sys.stderr)
	handler.setFormatter(FORMATTER)
	handler.setLevel(logging.WARNING)
	return handler

def create_file_handler():
	handler = TimedRotatingFileHandler(LOG_FILE, when='midnight')
	handler.setFormatter(FORMATTER)
	return handler

def init_logger(logger_name):
	logger = logging.getLogger(logger_name)

	logger.setLevel(logging.DEBUG) # better to have too much log than not enough

	logger.addHandler(get_console_handler())
	# logger.addHandler(get_file_handler())

	# with this pattern, it's rarely necessary to propagate the error up to parent
	logger.propagate = False

	return logger

def init():
	# set root logger, !!! all child loggers will descend from root looger.
	loggers = logging.getLogger()

	loggers.setLevel(logging.INFO)

	loggers.addHandler(create_console_err_handler())
	loggers.addHandler(create_console_out_handler())
	# logger.addHandler(get_file_handler())

	loggers.info("logging configured in root logger!")

init()

in main.py,

import logging_config
import logging

logger = logging.getLogger(__name__)
logger.info("I am from main logger")

in others.py,

import logging

logger = logging.getLogger(__name__)
logger.info("I am from others logger")

All loggers created by logging.getLogger(__name__) will descend from logging settings in logging_config.py.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment