forked from Tim55667757/FuzzyClassificator
-
Notifications
You must be signed in to change notification settings - Fork 11
/
FCLogger.py
111 lines (74 loc) · 2.94 KB
/
FCLogger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding: utf-8 -*-
# FuzzyClassificator - this program uses neural networks to solve classification problems,
# and uses fuzzy sets and fuzzy logic to interpreting results.
# Copyright (C) 2017, Timur Gilmullin
# e-mail: [email protected]
# This module implements universal logging system.
import sys
import logging
# initialize Main Parent Logger:
FCLogger = logging.getLogger("FCLogger")
formatString = "%(filename)-25s[Line:%(lineno)d]\t%(levelname)-10s[%(asctime)s]\t%(message)s"
formatter = logging.Formatter(formatString)
sys.stderr = sys.stdout
def SetLevel(vLevel='ERROR'):
"""
This procedure setting up FCLogger verbosity level.
"""
FCLogger.level = logging.NOTSET
FCLogger.parent.level = logging.DEBUG # fc.log always contains DEBUG log
if isinstance(vLevel, str):
if vLevel == '5' or vLevel.upper() == 'CRITICAL':
FCLogger.level = logging.CRITICAL
elif vLevel == '4' or vLevel.upper() == 'ERROR':
FCLogger.level = logging.ERROR
elif vLevel == '3' or vLevel.upper() == 'WARNING':
FCLogger.level = logging.WARNING
elif vLevel == '2' or vLevel.upper() == 'INFO':
FCLogger.level = logging.INFO
elif vLevel == '1' or vLevel.upper() == 'DEBUG':
FCLogger.level = logging.DEBUG
class LevelFilter(logging.Filter):
"""
Class using to set up log level filtering.
"""
def __init__(self, level):
super().__init__()
self.level = level
def filter(self, record):
return record.levelno >= self.level
def EnableLogger(logFile, parentHandler=FCLogger, useFormat=formatter):
"""
Adding new file logger.
"""
logHandler = logging.FileHandler(logFile)
logHandler.level = logging.DEBUG
logHandler.addFilter(LevelFilter(logging.DEBUG))
if useFormat:
logHandler.setFormatter(useFormat)
else:
logHandler.setFormatter(formatter)
parentHandler.addHandler(logHandler)
return logHandler
def DisableLogger(handler, parentHandler=FCLogger):
"""
Disable given file logger.
"""
if handler:
handler.flush()
handler.close()
if handler in parentHandler.handlers:
parentHandler.removeHandler(handler)
# Main init:
SetLevel('INFO') # set up INFO verbosity level by default for FCLogger
streamHandler = logging.StreamHandler() # initialize Console FCLogger by default
streamHandler.setFormatter(formatter) # set formatter for console FCLogger
if FCLogger.parent.handlers:
FCLogger.parent.handlers[0].setFormatter(formatter)
else:
FCLogger.addHandler(streamHandler) # adding console FCLogger handler to Parent Logger
# Constants and Global variables:
mainLogFile = 'fc.log' # file to full logging by default
fileLogHandler = EnableLogger(logFile=mainLogFile, parentHandler=FCLogger, useFormat=formatter) # add logging to file
sepLong = '-' * 80 # long log separator
sepShort = '-' * 40 # short log separator