-
-
Save ausuwardi/fbcef4aa08fea86174e90641871ad408 to your computer and use it in GitHub Desktop.
Python Comprehensive Logging using YAML Configuration
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 os | |
import yaml | |
import logging.config | |
import logging | |
import coloredlogs | |
def setup_logging(default_path='logging.yaml', default_level=logging.INFO, env_key='LOG_CFG'): | |
""" | |
| **@author:** Prathyush SP | |
| Logging Setup | |
""" | |
path = default_path | |
value = os.getenv(env_key, None) | |
if value: | |
path = value | |
if os.path.exists(path): | |
with open(path, 'rt') as f: | |
try: | |
config = yaml.safe_load(f.read()) | |
logging.config.dictConfig(config) | |
coloredlogs.install() | |
except Exception as e: | |
print(e) | |
print('Error in Logging Configuration. Using default configs') | |
logging.basicConfig(level=default_level) | |
coloredlogs.install(level=default_level) | |
else: | |
logging.basicConfig(level=default_level) | |
coloredlogs.install(level=default_level) | |
print('Failed to load configuration file. Using default configs') |
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
version: 1 | |
disable_existing_loggers: true | |
formatters: | |
standard: | |
format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s" | |
error: | |
format: "%(levelname)s <PID %(process)d:%(processName)s> %(name)s.%(funcName)s(): %(message)s" | |
handlers: | |
console: | |
class: logging.StreamHandler | |
level: DEBUG | |
formatter: standard | |
stream: ext://sys.stdout | |
info_file_handler: | |
class: logging.handlers.RotatingFileHandler | |
level: INFO | |
formatter: standard | |
filename: /tmp/info.log | |
maxBytes: 10485760 # 10MB | |
backupCount: 20 | |
encoding: utf8 | |
error_file_handler: | |
class: logging.handlers.RotatingFileHandler | |
level: ERROR | |
formatter: error | |
filename: /tmp/errors.log | |
maxBytes: 10485760 # 10MB | |
backupCount: 20 | |
encoding: utf8 | |
debug_file_handler: | |
class: logging.handlers.RotatingFileHandler | |
level: DEBUG | |
formatter: standard | |
filename: /tmp/debug.log | |
maxBytes: 10485760 # 10MB | |
backupCount: 20 | |
encoding: utf8 | |
critical_file_handler: | |
class: logging.handlers.RotatingFileHandler | |
level: CRITICAL | |
formatter: standard | |
filename: /tmp/critical.log | |
maxBytes: 10485760 # 10MB | |
backupCount: 20 | |
encoding: utf8 | |
warn_file_handler: | |
class: logging.handlers.RotatingFileHandler | |
level: WARN | |
formatter: standard | |
filename: /tmp/warn.log | |
maxBytes: 10485760 # 10MB | |
backupCount: 20 | |
encoding: utf8 | |
root: | |
level: NOTSET | |
handlers: [console] | |
propogate: yes | |
loggers: | |
<module>: | |
level: INFO | |
handlers: [console, info_file_handler, error_file_handler, critical_file_handler, debug_file_handler, warn_file_handler] | |
propogate: no | |
<module.x>: | |
level: DEBUG | |
handlers: [info_file_handler, error_file_handler, critical_file_handler, debug_file_handler, warn_file_handler] | |
propogate: yes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment