Created
February 13, 2020 07:26
-
-
Save jsenin/958d2f5a3bfc5e4d96a729e288c2ec70 to your computer and use it in GitHub Desktop.
colorized python app log
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 coloredlogs | |
def init_logger(dunder_name, testing_mode): | |
log_format = ( | |
'%(asctime)s - ' | |
'%(name)s - ' | |
'%(funcName)s - ' | |
'[%(levelname)s] - ' | |
'%(processName)s (%(process)d)' | |
'%(message)s' | |
) | |
log_format = '[%(asctime)s][%(levelname)s] %(name)s %(threadName)s:%(filename)s:%(funcName)s:%(lineno)d | %(message)s' | |
if testing_mode: | |
log_format = '[%(asctime)s][%(levelname)s] %(name)s %(threadName)s:%(pathname)s:%(funcName)s:%(lineno)d | %(message)s' | |
bold_seq = '\033[1m' | |
colorlog_format = ( | |
'{bold_seq} ' | |
'%(log_color)s' | |
'{log_format}'.format(bold_seq=bold_seq, log_format=log_format) | |
) | |
colorlog.basicConfig(format=colorlog_format) | |
logger = logging.getLogger(dunder_name) | |
if testing_mode: | |
logger.setLevel(logging.DEBUG) | |
else: | |
logger.setLevel(logging.INFO) | |
# Output full log | |
fh = logging.FileHandler('app.log') | |
fh.setLevel(logging.DEBUG) | |
formatter = logging.Formatter(log_format) | |
fh.setFormatter(formatter) | |
logger.addHandler(fh) | |
init_logger('my_app', True) | |
logger.info('info') | |
logger.warning('warning') | |
logger.error('warning') | |
logger.debug('debug') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment