summaryrefslogtreecommitdiff
path: root/ncserver/base/log.py
diff options
context:
space:
mode:
authorA. Wilcox <AWilcox@Wilcox-Tech.com>2020-10-20 21:24:07 -0500
committerA. Wilcox <AWilcox@Wilcox-Tech.com>2020-10-20 21:24:07 -0500
commitf0b93483f8ee7f22ed50da3b756bc40043ddff9f (patch)
tree4f414d02bc6eeaae9c8a88134d9259484e6a1bed /ncserver/base/log.py
parent8b92336a355a70cca6a665fead1149a17ccdd5ec (diff)
downloadnetconfapk-f0b93483f8ee7f22ed50da3b756bc40043ddff9f.tar.gz
netconfapk-f0b93483f8ee7f22ed50da3b756bc40043ddff9f.tar.bz2
netconfapk-f0b93483f8ee7f22ed50da3b756bc40043ddff9f.tar.xz
netconfapk-f0b93483f8ee7f22ed50da3b756bc40043ddff9f.zip
Add initial logging system implementation
Diffstat (limited to 'ncserver/base/log.py')
-rw-r--r--ncserver/base/log.py70
1 files changed, 70 insertions, 0 deletions
diff --git a/ncserver/base/log.py b/ncserver/base/log.py
new file mode 100644
index 0000000..053baec
--- /dev/null
+++ b/ncserver/base/log.py
@@ -0,0 +1,70 @@
+"""
+NETCONF for APK Distributions server:
+ Logging setup and routines.
+
+Copyright © 2020 Adélie Software in the Public Benefit, Inc.
+
+Released under the terms of the NCSA license. See the LICENSE file included
+with this source distribution for more information.
+
+SPDX-License-Identifier: NCSA
+"""
+
+
+import logging
+
+from socket import gethostname
+from taillight import Signal
+
+
+HOSTNAME_CHANGED = Signal(('node_changed', 'sys:hostname'))
+"""The signal that will fire when the hostname is changed."""
+
+HANDLER = logging.StreamHandler()
+"""The root log handler."""
+
+LOG_AUTH = logging.getLogger('ncserver.auth')
+"""The authentication logger."""
+
+LOG_CONFIG = logging.getLogger('ncserver.config')
+"""The configuration logger."""
+
+LOG_OPERATIONAL = logging.getLogger('ncserver.operational')
+"""The operational logger."""
+
+
+def configure_format(hostname: str):
+ """Configure the formatter for the root log handler."""
+ fmt = logging.Formatter('%(asctime)s ' + hostname +
+ ' %(name)s %(levelname)s: %(message)s',
+ '%Y-%m-%dT%H:%M:%S')
+ HANDLER.setFormatter(fmt)
+
+
+def configure_logging(level: int):
+ """Configure the logging system."""
+ configure_format(gethostname())
+
+ root = logging.getLogger()
+
+ HANDLER.setLevel(level)
+ root.setLevel(level)
+
+ root.addHandler(HANDLER)
+
+
+
+HOSTNAME_CHANGED.add(configure_format)
+
+if not hasattr(logging, 'NOTICE'):
+ logging.addLevelName(25, 'NOTICE')
+ logging.NOTICE = 25
+
+ def log_notice(self, message, *args, **kwargs):
+ if self.isEnabledFor(25):
+ self._log(25, message, args, **kwargs)
+ logging.getLoggerClass().notice = log_notice
+
+ def root_notice(message, *args, **kwargs):
+ logging.log(25, message, *args, **kwargs)
+ logging.notice = root_notice