Skip to content

Instantly share code, notes, and snippets.

@Susensio
Last active August 2, 2018 07:54
Show Gist options
  • Save Susensio/ee7dd131858f7b55470554f76bcd879e to your computer and use it in GitHub Desktop.
Save Susensio/ee7dd131858f7b55470554f76bcd879e to your computer and use it in GitHub Desktop.
Python logging boilerplate for packages
from .module import foo
import logging
from logging.handlers import RotatingFileHandler
import os
import os.path
# Logging directory
LOGGING_FOLDER = 'logs/'
LOG_PATH_TEMPLATE = LOGGING_FOLDER + "{}.log"
if not os.path.exists(LOGGING_FOLDER):
os.makedirs(LOGGING_FOLDER)
logger = logging.getLogger(__name__)
# ROOT logger level
logger.setLevel(logging.DEBUG)
BASIC_FORMAT = '%(asctime)s - %(levelname)-8s %(threadName)-10s > %(message)s (%(name)s.%(funcName)s:%(lineno)d)'
# Only print time (not date) to stdout
console_formatter = logging.Formatter(BASIC_FORMAT, '%H:%M:%S')
file_formatter = logging.Formatter(BASIC_FORMAT)
# Logging to stdout
console_log = logging.StreamHandler()
console_log.setLevel(logging.INFO)
console_log.setFormatter(console_formatter)
logger.addHandler(console_log)
# Logging to files
# debug.log
debug_file_log = RotatingFileHandler(LOG_PATH_TEMPLATE.format("debug"), encoding="utf-8", maxBytes=10_000_000, backupCount=10)
debug_file_log.setLevel(logging.DEBUG)
debug_file_log.setFormatter(file_formatter)
logger.addHandler(debug_file_log)
# info.log
info_file_log = RotatingFileHandler(LOG_PATH_TEMPLATE.format("info"), encoding="utf-8", maxBytes=1_000_000, backupCount=10)
info_file_log.setLevel(logging.INFO)
info_file_log.setFormatter(file_formatter)
logger.addHandler(info_file_log)
# error.log
error_file_log = RotatingFileHandler(LOG_PATH_TEMPLATE.format("error"), encoding="utf-8", maxBytes=1_000_000, backupCount=10)
error_file_log.setLevel(logging.WARNING)
error_file_log.setFormatter(file_formatter)
logger.addHandler(error_file_log)
import logging
logger = logging.getLogger(__name__)
def foo(bar):
"""Do something fancy."""
logger.info("Doing fancy stuff")
try:
logger.debug("bar = {}".format(bar))
baz = bar / 0
except ZeroDivisionError:
logger.exception("Fancy stuff failed")
>>> import package
>>> package.foo(5)

stdout

09:53:19 - INFO     MainThread > Doing fancy stuff    (package.module.foo:7)
09:53:19 - ERROR    MainThread > Fancy stuff failed    (package.module.foo:12)
Traceback (most recent call last):
  File "C:\Users\snogueras\Desktop\package\module.py", line 10, in foo
    baz = bar / 0
ZeroDivisionError: division by zero

debug.log

2018-08-02 09:53:19,594 - INFO     MainThread > Doing fancy stuff    (package.module.foo:7)
2018-08-02 09:53:19,595 - DEBUG    MainThread > bar = 5    (package.module.foo:9)
2018-08-02 09:53:19,595 - ERROR    MainThread > Fancy stuff failed    (package.module.foo:12)
Traceback (most recent call last):
  File "C:\Users\snogueras\Desktop\package\module.py", line 10, in foo
    baz = bar / 0
ZeroDivisionError: division by zero

info.log

2018-08-02 09:53:19,594 - INFO     MainThread > Doing fancy stuff    (package.module.foo:7)
2018-08-02 09:53:19,595 - ERROR    MainThread > Fancy stuff failed    (package.module.foo:12)
Traceback (most recent call last):
  File "C:\Users\snogueras\Desktop\package\module.py", line 10, in foo
    baz = bar / 0
ZeroDivisionError: division by zero

error.log

2018-08-02 09:53:19,595 - ERROR    MainThread > Fancy stuff failed    (package.module.foo:12)
Traceback (most recent call last):
  File "C:\Users\snogueras\Desktop\package\module.py", line 10, in foo
    baz = bar / 0
ZeroDivisionError: division by zero

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