diff options
author | SerenaFeng <feng.xiaowei@zte.com.cn> | 2016-09-20 13:58:21 +0800 |
---|---|---|
committer | SerenaFeng <feng.xiaowei@zte.com.cn> | 2016-09-21 15:02:44 +0800 |
commit | 6183cefebbc1af8ad1987dc61372d6c2d6a59f6d (patch) | |
tree | d2300ba196eb5e61fd2366a44e29f018791ad961 /utils | |
parent | fac2a3bff97efed6d4e50bfe249c8107100b8f52 (diff) |
add unified logger process for qtip
log the output information to both file and console, and
unify the log file path and level
JIRA: QTIP-108
Change-Id: I3a881e9da1f74c6959250a94f05cf90a231c34e6
Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'utils')
-rw-r--r-- | utils/__init__.py | 0 | ||||
-rw-r--r-- | utils/logger_utils.py | 65 |
2 files changed, 65 insertions, 0 deletions
diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/utils/__init__.py diff --git a/utils/logger_utils.py b/utils/logger_utils.py new file mode 100644 index 00000000..780696f4 --- /dev/null +++ b/utils/logger_utils.py @@ -0,0 +1,65 @@ +############################################################################## +# Copyright (c) 2016 ZTE Corp and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Logging levels: +# Level Numeric value +# CRITICAL 50 +# ERROR 40 +# WARNING 30 +# INFO 20 +# DEBUG 10 +# NOTSET 0 +# +# Usage: +# from utils import logger_utils +# logger = logger_utils.QtipLogger(__file__).get +# logger.info("message to be shown with - INFO - ") +# logger.debug("message to be shown with - DEBUG -") +############################################################################## + +import logging +import os + + +class Logger(object): + file_path = '/var/log' + formatter = logging.Formatter('%(asctime)s - %(name)s - ' + '%(levelname)s - %(message)s') + + def __init__(self, logger_name): + + IF_DEBUG = os.getenv('IF_DEBUG') + + self.logger_name = logger_name + self.logger = logging.getLogger(logger_name) + self.logger.propagate = 0 + self.logger.setLevel(logging.DEBUG) + + ch = logging.StreamHandler() + ch.setFormatter(self.formatter) + if IF_DEBUG is not None and IF_DEBUG.lower() == "true": + ch.setLevel(logging.DEBUG) + else: + ch.setLevel(logging.INFO) + self.logger.addHandler(ch) + + hdlr = logging.FileHandler('%s/%s.log' % (self.file_path, logger_name)) + hdlr.setFormatter(self.formatter) + hdlr.setLevel(logging.DEBUG) + self.logger.addHandler(hdlr) + + @property + def get(self): + return self.logger + + +class QtipLogger(Logger): + file_path = '/var/log/qtip' + + def __init__(self, logger_name): + super(QtipLogger, self).__init__(logger_name) |