From e98e967740e58cf429b76b187828e3e0625215a5 Mon Sep 17 00:00:00 2001 From: Tommy Carpenter Date: Thu, 5 Jul 2018 17:19:28 -0400 Subject: Bugfixes, add EELF error log func Change-Id: I159c9e663e8f17871862bb3ba42d8ea76fc2c05c Issue-ID: DCAEGEN2-581 Signed-off-by: Tommy Carpenter --- app/app/config_binding_service/logging.py | 86 ++++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 23 deletions(-) (limited to 'app/app/config_binding_service/logging.py') diff --git a/app/app/config_binding_service/logging.py b/app/app/config_binding_service/logging.py index f5cd6af..7966e2f 100644 --- a/app/app/config_binding_service/logging.py +++ b/app/app/config_binding_service/logging.py @@ -16,13 +16,15 @@ # # ECOMP is a trademark and service mark of AT&T Intellectual Property. -from logging import getLogger, StreamHandler, Formatter +from logging import getLogger, Formatter from logging.handlers import RotatingFileHandler from os import makedirs import datetime LOGGER = getLogger("defaultlogger") +_AUDIT_LOGGER = getLogger("defaultlogger") +_ERROR_LOGGER = getLogger("defaultlogger") def _create_logger(name, logfile): @@ -36,23 +38,28 @@ def _create_logger(name, logfile): maxBytes=10000000, backupCount=2) # 10 meg with one backup.. formatter = Formatter('%(message)s') file_handler.setFormatter(formatter) - stream_handler = StreamHandler() - stream_handler.setFormatter(formatter) logger.setLevel("DEBUG") logger.addHandler(file_handler) - logger.addHandler(stream_handler) return logger -def create_logger(): +def create_loggers(): """ 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) + + # create the audit log + aud_file = "/opt/logs/audit.log" + open(aud_file, 'a').close() # this is like "touch" + global _AUDIT_LOGGER + _AUDIT_LOGGER = _create_logger("config_binding_service_audit", aud_file) + + # create the error log + err_file = "/opt/logs/error.log" + open(err_file, 'a').close() # this is like "touch" + global _ERROR_LOGGER + _ERROR_LOGGER = _create_logger("config_binding_service_error", err_file) def utc(): @@ -65,23 +72,23 @@ def audit(raw_request, bts, xer, rcode, calling_mod, msg="n/a"): write an EELF audit record per https://wiki.onap.org/download/attachments/1015849/ONAP%20application%20logging%20guidelines.pdf?api=v2 %The audit fields implemented: - 1 BeginTimestamp Implemented (bts) - 2 EndTimestamp Auto Injected when this is called - 3 RequestID Implemented (xer) - 7 serviceName Implemented (from Req) - 9 StatusCode Auto injected based on rcode - 10 ResponseCode Implemented (rcode) - 15 Server IP address Implemented (from Req) - 16 ElapsedTime Auto Injected (milliseconds) - 18 ClientIPaddress Implemented (from Req) - 19 class name Implemented (mod), though docs say OOP, I am using the python module here - 20 Unused ...implemented.... - 21-25 Custom n/a - 26 detailMessage Implemented (msg) + 1 BeginTimestamp Implemented (bts) + 2 EndTimestamp Auto Injected when this is called + 3 RequestID Implemented (xer) + 7 serviceName Implemented (from Req) + 9 StatusCode Auto injected based on rcode + 10 ResponseCode Implemented (rcode) + 15 Server IP address Implemented (from Req) + 16 ElapsedTime Auto Injected (milliseconds) + 18 ClientIPaddress Implemented (from Req) + 19 class name Implemented (mod), though docs say OOP, I am using the python module here + 20 Unused ...implemented.... + 21-25 Custom n/a + 26 detailMessage Implemented (msg) """ ets = utc() - LOGGER.info("{bts}|{ets}|{xer}||||{path}||{status}|{rcode}|||||{servip}|{et}||{clientip}|{calling_mod}|||||||{msg}".format( + _AUDIT_LOGGER.info("{bts}|{ets}|{xer}||||{path}||{status}|{rcode}|||||{servip}|{et}||{clientip}|{calling_mod}|||||||{msg}".format( bts=bts.isoformat(), ets=ets.isoformat(), xer=xer, rcode=rcode, @@ -92,3 +99,36 @@ def audit(raw_request, bts, xer, rcode, calling_mod, msg="n/a"): clientip=raw_request.remote_addr, calling_mod=calling_mod, msg=msg )) + + +def error(raw_request, xer, severity, ecode, tgt_entity="n/a", tgt_path="n/a", msg="n/a", adv_msg="n/a"): + """ + write an EELF error record per + the error fields implemented: + + 1 Timestamp Auto Injected when this is called + 2 RequestID Implemented (xer) + 4 ServiceName Implemented (from Req) + 6 TargetEntity Implemented (tgt_entity) + 7 TargetServiceName Implemented (tgt_path)/ + 8 ErrorCategory Implemented (severity) + 9. ErrorCode Implemented (ecode) + 10 ErrorDescription Implemented (msg) + 11. detailMessage Implemented (adv_msg) + + Not implemented: + 3 ThreadID - n/a + 5 PartnerName - nothing in the request tells me this + """ + ets = utc() + + _ERROR_LOGGER.error("{ets}|{xer}||{path}||{tge}|{tgp}|{sev}|{ecode}|{msg}|{amsg}".format( + ets=ets, + xer=xer, + path=raw_request.path.split("/")[1], + tge=tgt_entity, + tgp=tgt_path, + sev=severity, + ecode=ecode, + msg=msg, + amsg=adv_msg)) -- cgit 1.2.3-korg