diff options
author | Yujun Zhang <zhang.yujunz@zte.com.cn> | 2016-09-21 07:17:52 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@172.30.200.206> | 2016-09-21 07:17:52 +0000 |
commit | 58ac79cd54821bf8c3891d136be4254aa79bfa08 (patch) | |
tree | 52bd6f03a9e7d50b44ffce15dab47d5ddead381c /utils/logger_utils.py | |
parent | ad90940e82c0039372d68f3136791851994557a9 (diff) | |
parent | 6183cefebbc1af8ad1987dc61372d6c2d6a59f6d (diff) |
Merge "add unified logger process for qtip"
Diffstat (limited to 'utils/logger_utils.py')
-rw-r--r-- | utils/logger_utils.py | 65 |
1 files changed, 65 insertions, 0 deletions
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) |