Created
July 19, 2017 00:03
-
-
Save val314159/21e09d3d0b4d283296eca6a85dae76ea to your computer and use it in GitHub Desktop.
log goodness in python
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 | |
_FMT = '%(filename)s : %(asctime)s : %(levelname)s : %(message)s' | |
_log = logging.getLogger() | |
_log.setLevel(logging.DEBUG) | |
debug = _log.debug | |
dbug = _log.debug | |
info = _log.info | |
warn = _log.warn | |
crit = _log.critical | |
critical = _log.critical | |
def log_to_stderr(): | |
ch = logging.StreamHandler() | |
ch.setFormatter(logging.Formatter(_FMT)) | |
ch.setLevel(logging.DEBUG) | |
_log.addHandler(ch) | |
def log_to_file(filename): | |
fh = logging.FileHandler(filename) | |
fh.setFormatter(logging.Formatter(_FMT)) | |
fh.setLevel(logging.INFO) | |
_log.addHandler(fh) | |
import sys | |
import os.path | |
program = os.path.basename(sys.argv[0]) | |
program = program[:-3] if program.endswith('.py') else program | |
log_to_stderr() | |
log_to_file(program+'.log') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment