From f2543f5880397831b566285c7ba843d689263832 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20=C5=9Eahin?= Date: Wed, 21 Sep 2016 11:49:21 +0300 Subject: [PATCH] log format changed, no need log message prefixes anymore and it becomes log line automatically --- opt/ahenk/base/logger/ahenk_logger.py | 69 ++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 13 deletions(-) diff --git a/opt/ahenk/base/logger/ahenk_logger.py b/opt/ahenk/base/logger/ahenk_logger.py index ff55b06..165f440 100644 --- a/opt/ahenk/base/logger/ahenk_logger.py +++ b/opt/ahenk/base/logger/ahenk_logger.py @@ -1,34 +1,77 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Author: İsmail BAŞARAN +# Author: Volkan Şahin + import logging import logging.config +from inspect import getframeinfo, stack +import sys from base.scope import Scope class Logger(object): - """docstring for Logger""" + """Ahenk Logger Service + This module is Ahenk logger service implementation. + Instance is stored in Scope. Service adds name of script which use logger, with line number. + + Example usage: + logger = Scope.getInstance().get_logger() + logger.debug('') + logger.info('') + logger.warning('') + + logger.error('') + + """ def __init__(self): super(Logger, self).__init__() - scope = Scope.getInstance() - configManager = scope.getConfigurationManager() + scope = Scope.get_instance() + config_manger = scope.get_configuration_manager() - logging.config.fileConfig(configManager.get('BASE', 'logConfigurationFilePath')) + logging.config.fileConfig(config_manger.get('BASE', 'logConfigurationFilePath')) self.logger = logging.getLogger() - def getLogger(self): + def get_logger(self): return self.logger - def info(self, logstring): - self.logger.info(logstring) + def debug(self, message): + caller = getframeinfo(stack()[1][0]) + self.logger.debug('[{0} {1}]\t {2}'.format(self.get_log_header(caller.filename), caller.lineno, message)) - def warning(self, logstring): - self.logger.warning(logstring) + def info(self, message): + caller = getframeinfo(stack()[1][0]) + self.logger.info('[{0} {1}]\t {2}'.format(self.get_log_header(caller.filename), caller.lineno, message)) - def error(self, logstring): - self.logger.error(logstring) + def warning(self, message): + caller = getframeinfo(stack()[1][0]) + self.logger.warning('[{0} {1}]\t {2}'.format(self.get_log_header(caller.filename), caller.lineno, message)) - def debug(self, logstring): - self.logger.debug(logstring) + def error(self, message): + try: + exc_type, exc_value, exc_trace_back = sys.exc_info() + caller = getframeinfo(stack()[1][0]) + + if exc_type is None and exc_value is None and exc_trace_back is None: + self.logger.error('[{0} {1}]\t {2}'.format(self.get_log_header(caller.filename), caller.lineno, message)) + else: + self.logger.error( + '[{0} {1} {2}]\t {3}'.format(self.get_log_header(caller.filename), exc_trace_back.tb_lineno, exc_type, + message)) + except Exception as e: + self.logger.error(message) + + @staticmethod + def get_log_header(file_path): + + if file_path is not None: + name_list = file_path.split('/') + result = '' + if len(name_list) > 1: + result = str(name_list[len(name_list) - 2]).upper() + ' >> ' + name_list[len(name_list) - 1] + + return result + else: + return None