|
1 # |
|
2 # Loggers |
|
3 # |
|
4 |
|
5 import os |
|
6 import logging |
|
7 import logging.handlers |
|
8 import atexit |
|
9 |
|
10 from fifolog import FIFOHandler |
|
11 import branding |
|
12 import locations |
|
13 |
|
14 # |
|
15 # Logging configuration |
|
16 # |
|
17 |
|
18 loglevel=logging.INFO |
|
19 logfmt="%(asctime)s:%(levelname)s:%(name)s:%(message)s" |
|
20 fifo_capacity=1000 |
|
21 fifo_fmt="%(asctime)s:%(levelname)s:%(message)s" |
|
22 |
|
23 # |
|
24 # Setting up the main logger with fifo, stderr, and rotating files output |
|
25 # |
|
26 |
|
27 mainlogger=logging.getLogger(branding.appname) |
|
28 mainlogger.setLevel(loglevel) |
|
29 mainlogger.propagate=True |
|
30 |
|
31 mainlogger.handlers.clear() |
|
32 |
|
33 # Internal FIFO history |
|
34 fifo=FIFOHandler(fifo_capacity) |
|
35 fifo.setFormatter(logging.Formatter(fifo_fmt)) |
|
36 mainlogger.addHandler(fifo) |
|
37 |
|
38 # stderr |
|
39 stderrlog=logging.StreamHandler() |
|
40 stderrlog.setLevel(logging.WARNING) |
|
41 mainlogger.addHandler(stderrlog) |
|
42 |
|
43 # Rotating files |
|
44 if not os.path.isdir(locations.logs_dir): |
|
45 os.makedirs(locations.logs_dir) |
|
46 |
|
47 fileslog=logging.handlers.TimedRotatingFileHandler( |
|
48 os.path.join(locations.logs_dir, branding.appname+'.log'), |
|
49 when='D', interval=1) |
|
50 fileslog.setFormatter(logging.Formatter(logfmt)) |
|
51 mainlogger.addHandler(fileslog) |
|
52 |
|
53 atexit.register(logging.shutdown) |
|
54 |
|
55 # |
|
56 # Routine for obtaining sub-loggers |
|
57 # |
|
58 |
|
59 def get(name): |
|
60 return mainlogger.getChild(name) |
|
61 |