loggers.py

changeset 79
b075b3db3044
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/loggers.py	Sun Jan 28 11:38:01 2018 +0000
@@ -0,0 +1,61 @@
+#
+# Loggers
+#
+
+import os
+import logging
+import logging.handlers
+import atexit
+
+from fifolog import FIFOHandler
+import branding
+import locations
+
+#
+# Logging configuration
+#
+
+loglevel=logging.INFO
+logfmt="%(asctime)s:%(levelname)s:%(name)s:%(message)s"
+fifo_capacity=1000
+fifo_fmt="%(asctime)s:%(levelname)s:%(message)s"
+
+#
+# Setting up the main logger with fifo, stderr, and rotating files output
+#
+
+mainlogger=logging.getLogger(branding.appname)
+mainlogger.setLevel(loglevel)
+mainlogger.propagate=True
+
+mainlogger.handlers.clear()
+
+# Internal FIFO history
+fifo=FIFOHandler(fifo_capacity)
+fifo.setFormatter(logging.Formatter(fifo_fmt))
+mainlogger.addHandler(fifo)
+
+# stderr
+stderrlog=logging.StreamHandler()
+stderrlog.setLevel(logging.WARNING)
+mainlogger.addHandler(stderrlog)
+
+# Rotating files
+if not os.path.isdir(locations.logs_dir):
+    os.makedirs(locations.logs_dir)
+
+fileslog=logging.handlers.TimedRotatingFileHandler(
+    os.path.join(locations.logs_dir, branding.appname+'.log'),
+    when='D', interval=1)
+fileslog.setFormatter(logging.Formatter(logfmt))
+mainlogger.addHandler(fileslog)
+
+atexit.register(logging.shutdown)
+
+#
+# Routine for obtaining sub-loggers
+#
+
+def get(name):
+    return mainlogger.getChild(name)
+

mercurial