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
112
113
114
115
116
117
118
119
120
121
|
#!/usr/bin/python
##
## Copyright (c) 2020 Intel Corporation
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
import logging
from logging.handlers import RotatingFileHandler
from logging import handlers
import os
import sys
import time
class bcolors(object):
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
FLASH = '\033[5m'
class RapidLog(object):
"""
Class to deal with rapid logging
"""
log = None
@staticmethod
def log_init(log_file, loglevel, screenloglevel, version):
# create formatters
screen_formatter = logging.Formatter("%(message)s")
file_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
# get a top-level logger,
# set its log level,
# BUT PREVENT IT from propagating messages to the root logger
#
log = logging.getLogger()
numeric_level = getattr(logging, loglevel.upper(), None)
if not isinstance(numeric_level, int):
raise ValueError('Invalid log level: %s' % loglevel)
log.setLevel(numeric_level)
log.propagate = 0
# create a console handler
# and set its log level to the command-line option
#
console_handler = logging.StreamHandler(sys.stdout)
#console_handler.setLevel(logging.INFO)
numeric_screenlevel = getattr(logging, screenloglevel.upper(), None)
if not isinstance(numeric_screenlevel, int):
raise ValueError('Invalid screenlog level: %s' % screenloglevel)
console_handler.setLevel(numeric_screenlevel)
console_handler.setFormatter(screen_formatter)
# create a file handler
# and set its log level
#
file_handler = logging.handlers.RotatingFileHandler(log_file, backupCount=10)
#file_handler = log.handlers.TimedRotatingFileHandler(log_file, 'D', 1, 5)
file_handler.setLevel(numeric_level)
file_handler.setFormatter(file_formatter)
# add handlers to the logger
#
log.addHandler(file_handler)
log.addHandler(console_handler)
# Check if log exists and should therefore be rolled
needRoll = os.path.isfile(log_file)
# This is a stale log, so roll it
if needRoll:
# Add timestamp
log.debug('\n---------\nLog closed on %s.\n---------\n' % time.asctime())
# Roll over on application start
file_handler.doRollover()
# Add timestamp
log.debug('\n---------\nLog started on %s.\n---------\n' % time.asctime())
log.debug("rapid version: " + version)
RapidLog.log = log
@staticmethod
def exception(exception_info):
RapidLog.log.exception(exception_info)
raise Exception(exception_info)
def critical(critical_info):
RapidLog.log.critical(critical_info)
raise Exception(critical_info)
@staticmethod
def error(error_info):
RapidLog.log.error(error_info)
@staticmethod
def debug(debug_info):
RapidLog.log.debug(debug_info)
@staticmethod
def info(info):
RapidLog.log.info(info)
|