-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoggingTest.py
95 lines (73 loc) · 2.3 KB
/
LoggingTest.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
# Code taken from https://www.golinuxcloud.com/python-logging/
import logging
import sys
import logging.config
LogConfig = 1
# To be used with logging.conf file
""" [loggers]
keys=root,my_logger
[handlers]
keys=consoleHandler,fileHandler
[formatters]
keys=consoleFormatter,fileFormatter
[logger_root]
level=DEBUG
handlers=consoleHandler
[logger_my_logger]
level=DEBUG
handlers=consoleHandler,fileHandler
qualname=my_logger
propagate=0
[handler_consoleHandler]
class=StreamHandler
level=CRITICAL
formatter=consoleFormatter
args=(sys.stdout,)
[handler_fileHandler]
class=FileHandler
level=DEBUG
formatter=fileFormatter
args=('/tmp/debug.log', 'a')
[formatter_consoleFormatter]
format=%(levelname)-8s %(name)-12s %(message)s
[formatter_fileFormatter]
format=[%(asctime)s] %(levelname)-8s %(name)-12s %(message)s """
if LogConfig:
# Define the logging.conf filePath
logging.config.fileConfig('logging.conf', disable_existing_loggers=False)
# Define your own logger name
logger = logging.getLogger("droidlogger")
# Write messages with all different types of levels
logger.debug('debug')
logger.info('info')
logger.warning('warning')
logger.error('error')
logger.critical('critical')
# Code to place in script
else:
# Log file location
logfile = 'debug.log'
# Define your own logger name
logger = logging.getLogger("my_logger")
# Set default logging level to DEBUG
logger.setLevel(logging.DEBUG)
# create console handler
print_format = logging.Formatter('%(levelname)-8s %(name)-12s %(message)s')
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.WARNING)
console_handler.setFormatter(print_format)
# create log file handler
# and define a custom log format, set its log level to DEBUG
log_format = logging.Formatter('[%(asctime)s] %(levelname)-8s %(name)-12s %(message)s')
file_handler = logging.FileHandler(logfile)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(log_format)
#Add handlers to the logger
logger.addHandler(file_handler)
logger.addHandler(console_handler)
# Write messages with all different types of levels
logger.debug('debug')
logger.info('info')
logger.warning('warning')
logger.error('error')
logger.critical('critical')