-
-
Save ariel-frischer/ea2ab72cd18977616ffea05b4fe4100c to your computer and use it in GitHub Desktop.
Python logging template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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