forked from Preston-Landers/concurrent-log-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
64 lines (56 loc) · 1.85 KB
/
example.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
import logging
import logging.config
from concurrent_log_handler import queue, ConcurrentRotatingFileHandler
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'default': {
'format': '%(asctime)s %(levelname)s %(name)s %(message)s'
},
},
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.handlers.ConcurrentRotatingFileHandler',
'formatter': 'default',
'filename': 'test.log',
'owner': ['greenfrog', 'admin'],
'chmod': 0o0660,
'maxBytes': 30,
'backupCount': 10,
'use_gzip': True,
'delay': True
}
},
'root': {
'handlers': ['file'],
'level': 'DEBUG',
},
}
LOG_NAME = 'test.log'
# logging.config.dictConfig(LOGGING)
logger = logging.getLogger(LOG_NAME)
logger.setLevel(logging.DEBUG)
handler = ConcurrentRotatingFileHandler(filename='test.log', mode='a', maxBytes=10 * 1000000,
backupCount=10, use_gzip=False,
encoding='utf-8')
handler.setLevel(logging.DEBUG)
TIME_FMT = "%Y-%m-%d %H:%M:%S"
FORMAT_STR = logging.Formatter(fmt='[%(asctime)s][%(levelname)s][%(filename)s:%(lineno)s][%(process)d][%(message)s]',
datefmt=TIME_FMT)
handler.setFormatter(FORMAT_STR)
logger.addHandler(handler)
# setup first time
queue.setup_logging_queues()
# setup second time
queue.setup_logging_queues()
# setup third time
queue.setup_logging_queues()
# setup forth time
queue.setup_logging_queues()
print("GLOBAL_LOGGER_HANDLERS:", queue.GLOBAL_LOGGER_HANDLERS)
print("logger.handlers:", logger.handlers)
for idx in range(0, 10):
logger.debug('%d > A debug message' % idx)
queue.stop_queue_listeners()