Harry

Merge branch 'logging_1' into 'master'

Added combined logging for application to share CherryPy logs;



See merge request !7
Showing 1 changed file with 65 additions and 8 deletions
... ... @@ -2,6 +2,7 @@ import os
import sys
import cherrypy
import importlib
import logging.config
from cherrypy.lib.reprconf import Parser
... ... @@ -13,6 +14,10 @@ if __name__ == '__main__':
sys.path.append(base_path)
# Import web framework modules
from src.plugins import SQLAlchemyPlugin
from src.tools import Jinja2Tool, SQLAlchemyTool, DebugTool
# Load config before CherryPy
p = Parser()
try:
... ... @@ -26,14 +31,10 @@ if __name__ == '__main__':
# cherrypy.server.httpserver = CPHTTPServer(cherrypy.server)
# cherrypy.log('Disabled WSGI Server, using native HTTP server')
# Import web framework modules
from src.plugins import SQLAlchemyPlugin
from src.tools import Jinja2Tool, SQLAlchemyTool, DebugTool
# Load Plugins
SQLAlchemyPlugin(cherrypy.engine,
host = conf['db']['host'],
port = conf['db']['port'],
host = conf['db']['host'],
port = conf['db']['port'],
name = conf['db']['name'],
user = conf['db']['user'],
pw = conf['db']['pass']).subscribe()
... ... @@ -45,13 +46,69 @@ if __name__ == '__main__':
# Import application modules via config file
for classname, pair in conf['apps'].items():
module, script = pair
cherrypy.log(f'MOUNT from {module} import {classname}')
module_object = importlib.import_module(module)
classname = getattr(module_object, classname)
cherrypy.log(f"cherrypy.tree.mount({classname}(), '{script}', config={conf_path})")
cherrypy.tree.mount(classname(), script, conf_path)
cherrypy.log(f'MOUNT from {module} import {classname}')
cherrypy.log(f"cherrypy.tree.mount({classname}(), '{script}', config={conf_path})")
# Share logging with application
LOG_CONF = {
'version': 1,
'formatters': {
'standard': {
'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
},
'basic': {
'format': '[%(levelname)s] %(message)s'
},
},
'handlers': {
'default': {
'level':'INFO',
'class':'logging.handlers.RotatingFileHandler',
'formatter': 'basic',
'filename': conf['logs']['error'],
'maxBytes': 10485760,
'backupCount': 20,
'encoding': 'utf8'
},
'cherrypy_access': {
'level':'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'basic',
'filename': conf['logs']['access'],
'maxBytes': 10485760,
'backupCount': 20,
'encoding': 'utf8'
},
},
'loggers': {
'': {
'handlers': ['default'],
'level': 'INFO'
},
'cherrypy.access': {
'handlers': ['cherrypy_access'],
'level': 'INFO',
'propagate': False
},
'cherrypy.error': {
'handlers': ['default'],
'level': 'INFO',
'propagate': False
},
}
}
# Disable CherryPy logger, replace with standard logging
cherrypy.engine.unsubscribe('graceful', cherrypy.log.reopen_files)
logging.config.dictConfig(LOG_CONF)
# Pass config file location to CherryPy
cherrypy.config.update(conf_path)
... ...