Skip to content

Instantly share code, notes, and snippets.

@ariel-frischer
Forked from gongzhitaao/py-logger-template.py
Last active October 23, 2019 23:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ariel-frischer/ea2ab72cd18977616ffea05b4fe4100c to your computer and use it in GitHub Desktop.
Save ariel-frischer/ea2ab72cd18977616ffea05b4fe4100c to your computer and use it in GitHub Desktop.
Python logging template
import logging
import logging.config
from datetime import datetime
from pathlib import Path
import sys
BASE_DIR = Path()
LOGFILE = BASE_DIR / 'bin' / (datetime.now().strftime("%H-%M") + '.log')
DEFAULT_LOGGING = {
'version': 1,
'formatters': {
'standard': {
'format': '%(asctime)s %(levelname)s: %(message)s',
'datefmt': '%Y-%m-%d %H:%M:%S',
},
'simple': {
'format': '%(message)s',
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'standard',
'level': 'ERROR',
'stream': sys.stdout,
},
'file': {
'class': 'logging.FileHandler',
'formatter': 'simple',
'level': 'DEBUG',
'filename': LOGFILE,
'mode': 'w',
},
},
'loggers': {
__name__: {
'level': 'DEBUG',
'handlers': ['console', 'file'],
'propagate': False,
},
}
}
def init():
logging.basicConfig(level=logging.ERROR)
logging.config.dictConfig(DEFAULT_LOGGING)
log = logging.getLogger(__name__) # name of logger will usually be pyLogger
def close():
logging.shutdown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment