|
1
|
|
|
"""Package handling different loggers |
|
2
|
|
|
|
|
3
|
|
|
.. Authors: |
|
4
|
|
|
Philippe Dessauw |
|
5
|
|
|
[email protected] |
|
6
|
|
|
|
|
7
|
|
|
.. Sponsor: |
|
8
|
|
|
Alden Dima |
|
9
|
|
|
[email protected] |
|
10
|
|
|
Information Systems Group |
|
11
|
|
|
Software and Systems Division |
|
12
|
|
|
Information Technology Laboratory |
|
13
|
|
|
National Institute of Standards and Technology |
|
14
|
|
|
http://www.nist.gov/itl/ssd/is |
|
15
|
|
|
""" |
|
16
|
|
|
import json |
|
17
|
|
|
import logging |
|
18
|
|
|
from time import sleep |
|
19
|
|
|
from pipeline.threads import StoppableThread |
|
20
|
|
|
from pipeline.queue import QueueManager |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
class AppLogger(object): |
|
24
|
|
|
"""Logger publishing to a redis queue |
|
25
|
|
|
""" |
|
26
|
|
|
|
|
27
|
|
|
def __init__(self, uid, local_logger, queue_ip="127.0.0.1", queue_port=6379): |
|
28
|
|
|
self.log_queue = QueueManager(host=queue_ip, port=queue_port, qname="logging") |
|
29
|
|
|
self.logger = local_logger |
|
30
|
|
|
|
|
31
|
|
|
# Identify the logging process |
|
32
|
|
|
self.uid = uid |
|
33
|
|
|
|
|
34
|
|
|
def log(self, level, message): |
|
35
|
|
|
"""Log a message with a given level |
|
36
|
|
|
|
|
37
|
|
|
Parameters |
|
38
|
|
|
level (int): Log level |
|
39
|
|
|
message (str): Message to store |
|
40
|
|
|
""" |
|
41
|
|
|
log_mess = { |
|
42
|
|
|
"uid": self.uid, |
|
43
|
|
|
"lvl": level, |
|
44
|
|
|
"msg": message |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
self.log_queue.push(json.dumps(log_mess)) |
|
48
|
|
|
self.logger.log(level, "["+self.uid+"] "+message) |
|
49
|
|
|
|
|
50
|
|
|
def debug(self, message): |
|
51
|
|
|
"""Log a debug message |
|
52
|
|
|
|
|
53
|
|
|
Parameters |
|
54
|
|
|
message (str): Message to store |
|
55
|
|
|
""" |
|
56
|
|
|
self.log(logging.DEBUG, message) |
|
57
|
|
|
|
|
58
|
|
|
def info(self, message): |
|
59
|
|
|
"""Log an info message |
|
60
|
|
|
|
|
61
|
|
|
Parameters |
|
62
|
|
|
message (str): Message to store |
|
63
|
|
|
""" |
|
64
|
|
|
self.log(logging.INFO, message) |
|
65
|
|
|
|
|
66
|
|
|
def warning(self, message): |
|
67
|
|
|
"""Log a warning message |
|
68
|
|
|
|
|
69
|
|
|
Parameters |
|
70
|
|
|
message (str): Message to store |
|
71
|
|
|
""" |
|
72
|
|
|
self.log(logging.WARNING, message) |
|
73
|
|
|
|
|
74
|
|
|
def error(self, message): |
|
75
|
|
|
"""Log an error message |
|
76
|
|
|
|
|
77
|
|
|
Parameters |
|
78
|
|
|
message (str): Message to store |
|
79
|
|
|
""" |
|
80
|
|
|
self.log(logging.ERROR, message) |
|
81
|
|
|
|
|
82
|
|
|
def fatal(self, message): |
|
83
|
|
|
"""Log a fatal message |
|
84
|
|
|
|
|
85
|
|
|
Parameters |
|
86
|
|
|
message (str): Message to store |
|
87
|
|
|
""" |
|
88
|
|
|
self.log(logging.FATAL, message) |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
class LogWriter(StoppableThread): |
|
92
|
|
|
"""Pops element from the logging queue and write them in the proper directory |
|
93
|
|
|
""" |
|
94
|
|
|
|
|
95
|
|
|
def __init__(self, app_logger, queue_ip="127.0.0.1", queue_port=6379): |
|
96
|
|
|
StoppableThread.__init__(self) |
|
97
|
|
|
|
|
98
|
|
|
self.log_queue = QueueManager(host=queue_ip, port=queue_port, qname="logging") |
|
99
|
|
|
self.logger = app_logger |
|
100
|
|
|
|
|
101
|
|
|
def write_logs(self): |
|
102
|
|
|
"""Write logs to a local file |
|
103
|
|
|
""" |
|
104
|
|
|
while not self.log_queue.is_empty(): |
|
105
|
|
|
log_json = self.log_queue.pop() |
|
106
|
|
|
log_data = json.loads(log_json) |
|
107
|
|
|
|
|
108
|
|
|
self.logger.log(log_data["lvl"], "["+log_data["uid"]+"] "+log_data["msg"]) |
|
109
|
|
|
sleep(0.2) |
|
110
|
|
|
|
|
111
|
|
|
def run(self): |
|
112
|
|
|
self.logger.debug("Logger initiatied") |
|
113
|
|
|
|
|
114
|
|
|
while not self.stop_event.isSet(): |
|
115
|
|
|
self.write_logs() |
|
116
|
|
|
sleep(0.5) |
|
117
|
|
|
|
|
118
|
|
|
self.write_logs() |
|
119
|
|
|
self.logger.info("Logger stopped") |
|
120
|
|
|
|