summaryrefslogtreecommitdiffstats
path: root/config_binding_service/logging.py
diff options
context:
space:
mode:
authorTommy Carpenter <tommy@research.att.com>2018-03-09 10:41:05 -0500
committerTommy Carpenter <tommy@research.att.com>2018-03-11 11:14:57 -0400
commit226719d5b23c7b82940a0e4c5b6922eead419788 (patch)
treef1bb5016e6842a4c2cf78231e6ca53d3c41b8c24 /config_binding_service/logging.py
parent914d8ccb42befc04ec5b759dd84b242591bb5e91 (diff)
Implement logging that can get to ELK
This adds more logging into the front end, and also logs to a file so ELK can get them. All public calls to the CBS are logged with the return code. This is not to say logging is completed in the CBS, more may come later and under a more structured format. Change-Id: I63c966876972e06c51276eaf1204b5b9b9e6cea7 Issue-ID: DCAEGEN2-387 Signed-off-by: Tommy Carpenter <tommy@research.att.com>
Diffstat (limited to 'config_binding_service/logging.py')
-rw-r--r--config_binding_service/logging.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/config_binding_service/logging.py b/config_binding_service/logging.py
new file mode 100644
index 0000000..e66851d
--- /dev/null
+++ b/config_binding_service/logging.py
@@ -0,0 +1,56 @@
+# ============LICENSE_START=======================================================
+# Copyright (c) 2017-2018 AT&T Intellectual Property. All rights reserved.
+# ================================================================================
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ============LICENSE_END=========================================================
+#
+# ECOMP is a trademark and service mark of AT&T Intellectual Property.
+
+from logging import getLogger, StreamHandler, Formatter
+from logging.handlers import RotatingFileHandler
+from os import makedirs
+
+
+LOGGER = getLogger("defaultlogger")
+
+
+def _create_logger(name, logfile):
+ """
+ Create a RotatingFileHandker
+ https://docs.python.org/3/library/logging.handlers.html
+ what's with the non-pythonic naming in these stdlib methods? Shameful.
+ """
+ logger = getLogger(name)
+ file_handler = RotatingFileHandler(logfile,
+ maxBytes=10000000, backupCount=2) # 10 meg with one backup..
+ file_formatter = Formatter('%(asctime)s | %(name)s | %(module)s | %(funcName)s | %(lineno)d | %(levelname)s | %(message)s') # right now the same, but intending to change
+ file_handler.setFormatter(file_formatter)
+
+ stream_handler = StreamHandler()
+ stream_formatter = Formatter('%(asctime)s | %(name)s | %(module)s | %(funcName)s | %(lineno)d | %(levelname)s | %(message)s')
+ stream_handler.setFormatter(stream_formatter)
+ logger.setLevel("DEBUG") # a function is going to wrap this anyway
+ logger.addHandler(file_handler)
+ logger.addHandler(stream_handler)
+ return logger
+
+
+def create_logger():
+ """
+ Public method to set the global logger, launched from Run
+ """
+ LOGFILE = "/opt/logs/log.log"
+ makedirs("/opt/logs", exist_ok=True)
+ open(LOGFILE, 'a').close() # this is like "touch"
+ global LOGGER
+ LOGGER = _create_logger("config_binding_service", LOGFILE)