server instances also log onto stdout for journalctl to handle
[sfa.git] / sfa / util / sfalogging.py
index e4b37c8..9afc334 100644 (file)
@@ -1,4 +1,4 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 
 """
 A reroutable logger that can handle deep tracebacks
@@ -34,7 +34,7 @@ Implementation:
 
 # pylint: disable=c0111, c0103, w1201
 
-from __future__ import print_function
+
 
 import os
 import os.path
@@ -121,9 +121,10 @@ logging.setLoggerClass(SfaLogger)
 # have the same set of keys
 def logging_config(context):
     if context == 'server':
-        handlername = 'file'
-        filename = '/var/log/sfa.log'
-        level = 'INFO'
+        # use stdout and let journalctl do the heavy lifting
+        handlername = 'stdout'
+        #filename = '/var/log/sfa.log'
+        level = 'DEBUG'
     elif context == 'import':
         handlername = 'file'
         filename = '/var/log/sfa-import.log'
@@ -134,13 +135,13 @@ def logging_config(context):
         level = 'DEBUG'
     elif context == 'console':
         handlername = 'stdout'
-        filename = 'ignored'
+        #filename = 'ignored'
         level = 'INFO'
     else:
         print("Cannot configure logging - exiting")
         exit(1)
 
-    return {
+    config = {
         'version': 1,
         # IMPORTANT: we may be imported by something else, so:
         'disable_existing_loggers': False,
@@ -151,23 +152,9 @@ def logging_config(context):
                            '%(filename)s:%(lineno)d %(message)s'),
             },
         },
+        # fill in later with just the one needed
+        # otherwise a dummy 'ignored' file gets created
         'handlers': {
-            'file': {
-                'filename': filename,
-                'level': level,
-                'formatter': 'standard',
-                'class': 'logging.handlers.TimedRotatingFileHandler',
-                # every monday and during 3 months
-                'when': 'w0',
-                'interval': 1,
-                'backupCount': 12,
-
-            },
-            'stdout': {
-                'level': level,
-                'formatter': 'standard',
-                'class': 'logging.StreamHandler',
-            },
         },
         'loggers': {
             'sfa': {
@@ -177,6 +164,24 @@ def logging_config(context):
             },
         },
     }
+    if handlername == 'stdout':
+        config['handlers']['stdout'] = {
+            'level': level,
+            'formatter': 'standard',
+            'class': 'logging.StreamHandler',
+        }
+    else:
+        config['handlers']['file'] = {
+            'filename': filename,
+            'level': level,
+            'formatter': 'standard',
+            'class': 'logging.handlers.TimedRotatingFileHandler',
+            # every monday and during 3 months
+            'when': 'w0',
+            'interval': 1,
+            'backupCount': 12,
+        }
+    return config
 
 
 logger = logging.getLogger('sfa')