From e6bcf9d97d787893906b957927602f3cfb862a4a Mon Sep 17 00:00:00 2001 From: kakwa Date: Sun, 10 Feb 2019 18:12:45 +0100 Subject: [PATCH] adding the possibility to log to stdout --- conf/ldapcherry.ini | 8 ++++++++ docs/deploy.rst | 6 +++--- ldapcherry/__init__.py | 18 ++++++++++++++++++ tests/test_LdapCherry.py | 2 +- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/conf/ldapcherry.ini b/conf/ldapcherry.ini index 12c12fa..d6f4c6f 100644 --- a/conf/ldapcherry.ini +++ b/conf/ldapcherry.ini @@ -28,6 +28,14 @@ request.show_tracebacks = False ## error and ldapcherry log file #log.error_file = '/tmp/ldapcherry_error.log' +##################################### +# configuration to log to stdout # +##################################### +## logger stdout for access log +#log.access_handler = 'stdout' +## logger stdout for error and ldapcherry log +#log.error_handler = 'stdout' + ##################################### # configuration to log in syslog # ##################################### diff --git a/docs/deploy.rst b/docs/deploy.rst index 1c8a4c0..1a541ee 100644 --- a/docs/deploy.rst +++ b/docs/deploy.rst @@ -445,16 +445,16 @@ Logging LdapCherry has two loggers, one for errors and applicative actions (login, del/add, logout...) and one for access logs. -Each logger can be configured to log to syslog, file or be disabled. +Each logger can be configured to log to **syslog**, **file**, **stdout** or be disabled. Logging parameters: +--------------------+---------+---------------------------------+-------------------------------------------------+----------------------------------------+ | Parameter | Section | Description | Values | Comment | +====================+=========+=================================+=================================================+========================================+ -| log.access_handler | global | Logger type for access log | 'syslog', 'file', 'none' | | +| log.access_handler | global | Logger type for access log | 'syslog', 'file', 'stdout', 'none' | | +--------------------+---------+---------------------------------+-------------------------------------------------+----------------------------------------+ -| log.error_handler | global | Logger type for applicative log | 'syslog', 'file', 'none' | | +| log.error_handler | global | Logger type for applicative log | 'syslog', 'file', 'stdout', 'none' | | +--------------------+---------+---------------------------------+-------------------------------------------------+----------------------------------------+ | log.access_file | global | log file for access log | path to log file | only used if log.access_handler='file' | +--------------------+---------+---------------------------------+-------------------------------------------------+----------------------------------------+ diff --git a/ldapcherry/__init__.py b/ldapcherry/__init__.py index 4e61fc9..963b051 100644 --- a/ldapcherry/__init__.py +++ b/ldapcherry/__init__.py @@ -285,6 +285,15 @@ class LdapCherry(object): handler.setFormatter(syslog_formatter) cherrypy.log.access_log.addHandler(handler) + # if stdout, open a logger on stdout + elif access_handler == 'stdout': + cherrypy.log.access_log.handlers = [] + handler = logging.StreamHandler(sys.stdout) + formatter = logging.Formatter( + 'ldapcherry.access - %(levelname)s - %(message)s' + ) + handler.setFormatter(formatter) + cherrypy.log.access_log.addHandler(handler) # if file, we keep the default elif access_handler == 'file': pass @@ -330,6 +339,15 @@ class LdapCherry(object): handler.setFormatter(syslog_formatter) cherrypy.log.error_log.addHandler(handler) + # if stdout, open a logger on stdout + elif error_handler == 'stdout': + cherrypy.log.error_log.handlers = [] + handler = logging.StreamHandler(sys.stdout) + formatter = logging.Formatter( + 'ldapcherry.app - %(levelname)s - %(message)s' + ) + handler.setFormatter(formatter) + cherrypy.log.error_log.addHandler(handler) # if file, we keep the default elif error_handler == 'file': pass diff --git a/tests/test_LdapCherry.py b/tests/test_LdapCherry.py index 69445bc..d2a598f 100644 --- a/tests/test_LdapCherry.py +++ b/tests/test_LdapCherry.py @@ -152,7 +152,7 @@ class TestError(object): def testLog(self): app = LdapCherry() cfg = { 'global' : {}} - for t in ['none', 'file', 'syslog']: + for t in ['none', 'file', 'syslog', 'stdout']: cfg['global']['log.access_handler']=t cfg['global']['log.error_handler']=t app._set_access_log(cfg, logging.DEBUG)