tksbrokerapi.UniLogger
This module init standard python logging system with some of pre-defined parameters, e.g. debug level for log-file, info level for console, log-rotate and so on.
Current internal logger name for TKSBrokerAPI module is TKSBrokerAPI-UniLogger
.
1# -*- coding: utf-8 -*- 2# 3# Author: Timur Gilmullin 4 5""" 6This module init standard python logging system with some of pre-defined parameters, 7e.g. debug level for log-file, info level for console, log-rotate and so on. 8 9Current internal logger name for TKSBrokerAPI module is `TKSBrokerAPI-UniLogger`. 10""" 11 12# Copyright (c) 2022 Gilmillin Timur Mansurovich 13# 14# Licensed under the Apache License, Version 2.0 (the "License"); 15# you may not use this file except in compliance with the License. 16# You may obtain a copy of the License at 17# 18# http://www.apache.org/licenses/LICENSE-2.0 19# 20# Unless required by applicable law or agreed to in writing, software 21# distributed under the License is distributed on an "AS IS" BASIS, 22# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 23# See the License for the specific language governing permissions and 24# limitations under the License. 25 26 27import sys 28import logging.handlers 29 30 31# initialize Main Parent Logger: 32UniLogger = logging.getLogger("TKSBrokerAPI-UniLogger") 33formatString = "%(filename)-20sL:%(lineno)-5d%(levelname)-8s[%(asctime)s] %(message)s" 34formatter = logging.Formatter(formatString) 35sys.stderr = sys.stdout 36 37 38def SetLevel(vLevel="ERROR"): 39 """ 40 This procedure setting up UniLogger verbosity level. 41 """ 42 UniLogger.level = logging.NOTSET 43 44 if isinstance(vLevel, str): 45 if vLevel == "5" or vLevel.upper() == "CRITICAL": 46 UniLogger.level = logging.CRITICAL 47 48 elif vLevel == "4" or vLevel.upper() == "ERROR": 49 UniLogger.level = logging.ERROR 50 51 elif vLevel == "3" or vLevel.upper() == "WARNING": 52 UniLogger.level = logging.WARNING 53 54 elif vLevel == "2" or vLevel.upper() == "INFO": 55 UniLogger.level = logging.INFO 56 57 elif vLevel == "1" or vLevel.upper() == "DEBUG": 58 UniLogger.level = logging.DEBUG 59 60 61class LevelFilter(logging.Filter): 62 """ 63 Class using to set up log level filtering. 64 """ 65 66 def __init__(self, level): 67 super().__init__() 68 self.level = level 69 70 def filter(self, record): 71 return record.levelno >= self.level 72 73 74def EnableLogger(logFile, parentHandler=UniLogger, useFormat=formatter): 75 """ 76 Adding new file logger with rotation. 77 """ 78 maxSizeBytes = 5 * 1024 * 1024 # 5Mb log rotate by default 79 logHandler = logging.handlers.RotatingFileHandler(logFile, encoding="UTF-8", maxBytes=maxSizeBytes, backupCount=4) 80 logHandler.level = logging.DEBUG # set up DEBUG verbosity level by default for file logging 81 logHandler.addFilter(LevelFilter(logging.DEBUG)) 82 83 if useFormat: 84 logHandler.setFormatter(useFormat) 85 86 else: 87 logHandler.setFormatter(formatter) 88 89 parentHandler.addHandler(logHandler) 90 91 return logHandler 92 93 94def DisableLogger(handler, parentHandler=UniLogger): 95 """ 96 Disable given file logger. 97 """ 98 if handler: 99 handler.flush() 100 handler.close() 101 102 if handler in parentHandler.handlers: 103 parentHandler.removeHandler(handler) 104 105 106# --- Main init: 107 108SetLevel("DEBUG") # set up DEBUG verbosity level by default for UniLogger 109 110streamHandler = logging.StreamHandler() # initialize STDOUT UniLogger 111streamHandler.setFormatter(formatter) # set formatter for STDOUT UniLogger 112streamHandler.level = logging.INFO # set up INFO verbosity level by default for STDOUT UniLogger 113UniLogger.addHandler(streamHandler) # adding STDOUT UniLogger handler to Parent UniLogger 114 115defaultLogFile = "TKSBrokerAPI.log" 116fileLogHandler = EnableLogger(logFile=defaultLogFile, parentHandler=UniLogger, useFormat=formatter) # add logging to file 117 118sepWide = "-" * 120 # long-long log separator 119sepLong = "-" * 80 # long log separator 120sepShort = "-" * 40 # short log separator 121sepLine = "=--=" * 20 # log part separator
def
SetLevel(vLevel='ERROR'):
39def SetLevel(vLevel="ERROR"): 40 """ 41 This procedure setting up UniLogger verbosity level. 42 """ 43 UniLogger.level = logging.NOTSET 44 45 if isinstance(vLevel, str): 46 if vLevel == "5" or vLevel.upper() == "CRITICAL": 47 UniLogger.level = logging.CRITICAL 48 49 elif vLevel == "4" or vLevel.upper() == "ERROR": 50 UniLogger.level = logging.ERROR 51 52 elif vLevel == "3" or vLevel.upper() == "WARNING": 53 UniLogger.level = logging.WARNING 54 55 elif vLevel == "2" or vLevel.upper() == "INFO": 56 UniLogger.level = logging.INFO 57 58 elif vLevel == "1" or vLevel.upper() == "DEBUG": 59 UniLogger.level = logging.DEBUG
This procedure setting up UniLogger verbosity level.
class
LevelFilter(logging.Filter):
62class LevelFilter(logging.Filter): 63 """ 64 Class using to set up log level filtering. 65 """ 66 67 def __init__(self, level): 68 super().__init__() 69 self.level = level 70 71 def filter(self, record): 72 return record.levelno >= self.level
Class using to set up log level filtering.
def
EnableLogger( logFile, parentHandler=<Logger TKSBrokerAPI-UniLogger (DEBUG)>, useFormat=<logging.Formatter object>):
75def EnableLogger(logFile, parentHandler=UniLogger, useFormat=formatter): 76 """ 77 Adding new file logger with rotation. 78 """ 79 maxSizeBytes = 5 * 1024 * 1024 # 5Mb log rotate by default 80 logHandler = logging.handlers.RotatingFileHandler(logFile, encoding="UTF-8", maxBytes=maxSizeBytes, backupCount=4) 81 logHandler.level = logging.DEBUG # set up DEBUG verbosity level by default for file logging 82 logHandler.addFilter(LevelFilter(logging.DEBUG)) 83 84 if useFormat: 85 logHandler.setFormatter(useFormat) 86 87 else: 88 logHandler.setFormatter(formatter) 89 90 parentHandler.addHandler(logHandler) 91 92 return logHandler
Adding new file logger with rotation.
def
DisableLogger(handler, parentHandler=<Logger TKSBrokerAPI-UniLogger (DEBUG)>):
95def DisableLogger(handler, parentHandler=UniLogger): 96 """ 97 Disable given file logger. 98 """ 99 if handler: 100 handler.flush() 101 handler.close() 102 103 if handler in parentHandler.handlers: 104 parentHandler.removeHandler(handler)
Disable given file logger.