Skip to content

Instantly share code, notes, and snippets.

@howardhamilton
Created October 19, 2016 16:33
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save howardhamilton/3076f9912bf70c2a4a0737ee7ff8c140 to your computer and use it in GitHub Desktop.
Setup logging configuration
{
"version": 1,
"disable_existing_loggers": false,
"formatters": {
"default": {
"format": "%(asctime)s - %(name)s - %(levelname)s: %(message)s"
},
"console": {
"format": "%(name)-12s: %(message)s"
}
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "INFO",
"formatter": "console",
"stream": "ext://sys.stdout"
},
"err": {
"class": "logging.StreamHandler",
"level": "INFO",
"formatter": "console",
"stream": "ext://sys.stderr"
},
"main": {
"class": "logging.handlers.RotatingFileHandler",
"level": "DEBUG",
"formatter": "default",
"filename": "/path/to/main.log",
"maxBytes": 20971520,
"backupCount": 6,
"encoding": "utf8"
}
},
"loggers": {
"": {
"handlers": ["main", "console"],
"level": "DEBUG",
"propagate": true
}
}
}
def setup_logging(default_path="logging.json", default_level=logging.INFO):
"""Setup logging configuration"""
path = default_path
if os.path.exists(path):
with open(path, 'rt') as f:
config = json.load(f)
logging.config.dictConfig(config)
else:
logging.basicConfig(level=default_level)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment