Skip to content

Instantly share code, notes, and snippets.

@windows98SE
Created August 18, 2018 23:45
Show Gist options
  • Save windows98SE/9cf29b0499553bfc466ae9744538db8e to your computer and use it in GitHub Desktop.
Save windows98SE/9cf29b0499553bfc466ae9744538db8e to your computer and use it in GitHub Desktop.
logging, coloredlogs, logging.config
{
"version": 1,
"disable_existing_loggers": false,
"formatters": {
"console-simple": {
"()": "ext://coloredlogs.ColoredFormatter",
"format": "[%(asctime)s.%(msecs)03d] : %(message)s",
"datefmt": "%H:%M:%S",
"field_styles": {
"asctime": {"color": "green"}
},
"level_styles": {
"info": {},
"warning": {"color": "yellow"},
"error": {"color": "red"},
"critical": {"color": "red", "bold": true}
}
},
"console-verbose": {
"()": "ext://coloredlogs.ColoredFormatter",
"format": "[%(asctime)s.%(msecs)03d] %(levelname)s : %(message)s",
"datefmt": "%H:%M:%S",
"field_styles": {
"asctime": {"color": "green"},
"levelname": {"color": "black", "bold": true}
},
"level_styles": {
"debug": {"color": 23},
"info": {},
"warning": {"color": "yellow"},
"error": {"color": "red"},
"critical": {"color": "red", "bold": true}
}
},
"console-dump": {
"()": "ext://coloredlogs.ColoredFormatter",
"format": "[%(asctime)s.%(msecs)03d] [%(threadName)s] [%(module)s:%(funcName)s;%(lineno)d] %(levelname)s : %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S",
"field_styles": {
"asctime": {"color": "green"},
"threadName": {"color": 27, "faint": true},
"module": {"color": "blue"},
"funcName": {"color": "blue"},
"lineno": {"color": "blue"},
"levelname": {"color": "black", "bold": true}
},
"level_styles": {
"debug": {"color": 23},
"info": {},
"warning": {"color": "yellow"},
"error": {"color": "red"},
"critical": {"color": "red", "bold": true}
}
},
"file-analysis": {
"format": "[%(asctime)s.%(msecs)03d] : %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S"
},
"file-dump": {
"format": "[%(asctime)s.%(msecs)03d] [%(process)s:%(threadName)s] [%(module)s:%(funcName)s;%(lineno)d] %(levelname)s : %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S"
}
},
"handlers": {
"console-simple": {
"level": "INFO",
"class": "logging.StreamHandler",
"formatter": "console-simple",
"stream": "ext://sys.stdout"
},
"console-verbose": {
"level": "INFO",
"class": "logging.StreamHandler",
"formatter": "console-verbose",
"stream": "ext://sys.stdout"
},
"console-dump": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"formatter": "console-dump",
"stream": "ext://sys.stdout"
},
"file-analysis": {
"level": "INFO",
"()": "ext://logger._log_file",
"formatter": "file-analysis",
"filename": "analysis.log"
},
"file-dump": {
"level": "DEBUG",
"()": "ext://logger._log_file",
"formatter": "file-dump",
"filename": "dump.log"
},
"errors": {
"level": "ERROR",
"()": "ext://logger._log_file",
"formatter": "file-dump",
"filename": "error.log"
}
},
"loggers": {
"info": {
"level": "INFO",
"handlers": ["console-simple"],
"propagate": false
},
"verbose": {
"level": "INFO",
"handlers": ["console-verbose", "file-analysis"],
"propagate": false
},
"debug": {
"level": "DEBUG",
"handlers": ["console-dump", "file-analysis", "file-dump", "errors"]
},
"requests": {
"level": "WARNING",
"handlers": ["errors"]
},
"urllib3": {
"level": "WARNING",
"handlers": ["errors"]
}
},
"root": {
"level": "CRITICAL",
"handlers": ["file-analysis", "file-dump", "errors"],
"propagate": false
}
}
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import coloredlogs
import json
import logging
import logging.config
from datetime import datetime
def _log_file(filename=None):
ret = 'logs-' + str(datetime.now().strftime("%Y-%m-%d")) + ('-' + filename if filename else '-logging.log')
return logging.FileHandler(ret)
class Logger:
def __init__(self, file_config='logger.json'):
self.file = file_config
def load(self, name='root'):
logging.config.dictConfig(json.load(open(self.file)))
logger = logging.getLogger(name)
coloredlogs.install(
logging.DEBUG,
milliseconds=True,
reconfigure=False,
logger=logger,
)
return logger
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from logger import Logger
logger = Logger('logger.json').load('info')
#logger = Logger('logger.json').load('verbose')
#logger = Logger('logger.json').load('debug')
def main():
logger.debug("this is a debugging message")
logger.info("this is an informational message")
logger.warning("this is a warning message")
logger.error("this is an error message")
logger.critical("this is a critical message")
if __name__ == '__main__':
main()
@windows98SE
Copy link
Author

logger = Logger('logger.json').load('info')

[06:44:28.243] : this is an informational message
[06:44:28.243] : this is a warning message
[06:44:28.244] : this is an error message
[06:44:28.244] : this is a critical message

@windows98SE
Copy link
Author

logger = Logger('logger.json').load('verbose')

[06:49:16.293] INFO : this is an informational message
[06:49:16.293] WARNING : this is a warning message
[06:49:16.294] ERROR : this is an error message
[06:49:16.294] CRITICAL : this is a critical message

@windows98SE
Copy link
Author

logger = Logger('logger.json').load('debug')

[2018-08-19 06:49:54.422] [MainThread] [main:main;12] DEBUG : this is a debugging message
[2018-08-19 06:49:54.422] [MainThread] [main:main;13] INFO : this is an informational message
[2018-08-19 06:49:54.422] [MainThread] [main:main;14] WARNING : this is a warning message
[2018-08-19 06:49:54.423] [MainThread] [main:main;15] ERROR : this is an error message
[2018-08-19 06:49:54.423] [MainThread] [main:main;16] CRITICAL : this is a critical message

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