From 3f639daf1ebab51593e5a58f4b481fb81be266a8 Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Wed, 31 Mar 2021 21:20:56 +0200
Subject: [PATCH] Removed straying dev code, use our logging format

---
 SDP/SDP/SDP.py           |  9 ---------
 SDP/SDP/lofar_logging.py | 13 +++++++++++++
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/SDP/SDP/SDP.py b/SDP/SDP/SDP.py
index ee3209e07..88408be04 100644
--- a/SDP/SDP/SDP.py
+++ b/SDP/SDP/SDP.py
@@ -489,13 +489,4 @@ def main(args=None, **kwargs):
 
 
 if __name__ == '__main__':
-    x = Device.error_stream
-
-    def new_error_stream(self, *args, **kwargs):
-        print("logging to python")
-        logger.error(*args, **kwargs)
-        print("logging to tango")
-        x.error_stream(self, *args, **kwargs)
-    Device.error_stream = new_error_stream
-
     main()
diff --git a/SDP/SDP/lofar_logging.py b/SDP/SDP/lofar_logging.py
index 5c3996ddd..cf43235eb 100644
--- a/SDP/SDP/lofar_logging.py
+++ b/SDP/SDP/lofar_logging.py
@@ -1,6 +1,10 @@
 import logging
 from functools import wraps
 
+# Always also log the hostname because it makes the origin of the log clear.
+import socket
+hostname = socket.gethostname()
+
 def configure_logger(logger: logging.Logger, log_extra=None):
     logger.setLevel(logging.DEBUG)
 
@@ -16,6 +20,15 @@ def configure_logger(logger: logging.Logger, log_extra=None):
 
         # install the handler
         logger.addHandler(handler)
+
+        # for now, also log to stderr
+        # Set up logging in a way that it can be understood by a human reader, be
+        # easily grep'ed, be parsed with a couple of shell commands and
+        # easily fed into an Kibana/Elastic search system.
+        handler = logging.StreamHandler()
+        formatter = logging.Formatter(fmt = '%(asctime)s.%(msecs)d %(levelname)s - HOST="{}" PID="%(process)d" TNAME="%(threadName)s" TID="%(thread)d" FILE="%(pathname)s" LINE="%(lineno)d" FUNC="%(funcName)s" MSG="%(message)s"'.format(hostname), datefmt = '%Y-%m-%dT%H:%M:%S')
+        handler.setFormatter(formatter)
+        logger.addHandler(handler)
     except Exception:
         logger.exception("Cannot import or configure logstash_async module, not forwarding logs to ELK stack.")
 
-- 
GitLab