From 6c920a68ac7912188e23ca19a743edba22a540ff Mon Sep 17 00:00:00 2001 From: "Jason R. Coombs" Date: Wed, 20 Jan 2016 13:44:49 -0500 Subject: [PATCH] Configure logging initialization such that it can be readily initialized by a programmatic execution of aspen. Fixes #540. --- aspen/__init__.py | 23 +++++++++++++++++++++++ aspen/__main__.py | 28 +++------------------------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/aspen/__init__.py b/aspen/__init__.py index 4257e5868..59095750a 100644 --- a/aspen/__init__.py +++ b/aspen/__init__.py @@ -62,7 +62,9 @@ from __future__ import unicode_literals import sys +import logging.config import pkg_resources +import functools from .backcompat import is_callable @@ -79,6 +81,27 @@ __version__ = dist.version WINDOWS = sys.platform[:3] == 'win' +_logging_cfg = { + 'version': 1, + 'formatters': { + 'threadinfo': { + 'format': "%(asctime)s pid-%(process)d thread-%(thread)d (%(threadName)s) %(levelname)s: %(message)s" + } + }, + 'handlers': { + 'console': { + 'class': 'logging.StreamHandler', + 'formatter': 'threadinfo', + 'level': 'INFO', + 'stream': 'ext://sys.stderr' + } + }, + 'root': { + 'handlers': [ 'console' ] + } +} + +init_logging = functools.partial(logging.config.dictConfig, _logging_cfg) def serve(website, host='0.0.0.0', port='8080'): diff --git a/aspen/__main__.py b/aspen/__main__.py index 83d6d9e39..a398e83e6 100644 --- a/aspen/__main__.py +++ b/aspen/__main__.py @@ -21,31 +21,9 @@ from __future__ import print_function from __future__ import unicode_literals -from . import serve, website - -import logging.config - -logging_cfg = { - 'version': 1, - 'formatters': { - 'threadinfo': { - 'format': "%(asctime)s pid-%(process)d thread-%(thread)d (%(threadName)s) %(levelname)s: %(message)s" - } - }, - 'handlers': { - 'console': { - 'class': 'logging.StreamHandler', - 'formatter': 'threadinfo', - 'level': 'INFO', - 'stream': 'ext://sys.stderr' - } - }, - 'root': { - 'handlers': [ 'console' ] - } -} - -logging.config.dictConfig(logging_cfg) +from . import serve, website, init_logging + if __name__ == '__main__': + init_logging() serve(website.Website())