1
|
|
|
"""Set up Application Logs
|
2
|
|
|
|
3
|
|
|
This module defines how the emitted application logs are handled and where
|
4
|
|
|
they are written/streamed.
|
5
|
|
|
The application logs are written in full details (ie with timestamps) to a file
|
6
|
|
|
and also streamed to the console in a more concise format.
|
7
|
|
|
|
8
|
|
|
Console:
|
9
|
|
|
Stream Logs of INFO (and above) Level on Console's stderr
|
10
|
|
|
The rendered Log format is: <logger name>: <log level> <log message>
|
11
|
|
|
|
12
|
|
|
Disk File:
|
13
|
|
|
Write Logs of ALL Levels on a Disk File (see DCR_LOGS_FILE variable below)
|
14
|
|
|
The rendered Log format is: <timestamp> <logger name>: <log level> <log message>
|
15
|
|
|
|
16
|
|
|
Log Levels:
|
17
|
|
|
- CRITICAL
|
18
|
|
|
- ERROR
|
19
|
|
|
- WARNING
|
20
|
|
|
- INFO
|
21
|
|
|
- DEBUG
|
22
|
|
|
|
23
|
|
|
Usage:
|
24
|
|
|
Do a 'from . import _logging' in the root __init__.py of your package and
|
25
|
|
|
all submodules 'inherit' the logging configuration
|
26
|
|
|
"""
|
27
|
|
|
import logging
|
28
|
|
|
|
29
|
|
|
DCR_LOGS_FILE = 'mac.log'
|
30
|
|
|
|
31
|
|
|
#### FILE LOGGING
|
32
|
|
|
# set up logging to file for DEBUG Level and above
|
33
|
|
|
# basically log ALL events to a file
|
34
|
|
|
logging.basicConfig(
|
35
|
|
|
level=logging.DEBUG,
|
36
|
|
|
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
|
37
|
|
|
datefmt='%m-%d %H:%M GGG',
|
38
|
|
|
filename=DCR_LOGS_FILE,
|
39
|
|
|
filemode='w',
|
40
|
|
|
)
|
41
|
|
|
|
42
|
|
|
#### CONSOLE LOGGING
|
43
|
|
|
# define a Handler which writes INFO messages or higher to the sys.stderr
|
44
|
|
|
console = logging.StreamHandler()
|
45
|
|
|
# console.setLevel(logging.INFO)
|
46
|
|
|
console.setLevel(logging.DEBUG)
|
47
|
|
|
# set a format which is simpler for console use
|
48
|
|
|
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
|
49
|
|
|
# tell the handler to use this format
|
50
|
|
|
console.setFormatter(formatter)
|
51
|
|
|
# add the handler to the root logger
|
52
|
|
|
logging.getLogger('').addHandler(console)
|
53
|
|
|
|
54
|
|
|
|
55
|
|
|
# Now, we can log to the root logger, or any other logger. First the root...
|
56
|
|
|
# logging.info('Blah blah')
|
57
|
|
|
|
58
|
|
|
# Now, define a couple of other loggers which might represent areas in your
|
59
|
|
|
# application:
|
60
|
|
|
|
61
|
|
|
# logger1 = logging.getLogger('myapp.area1')
|
62
|
|
|
# logger2 = logging.getLogger('myapp.area2')
|
63
|
|
|
# logger3 = logging.getLogger(__name__)
|
64
|
|
|
|
65
|
|
|
# logger1.debug('balh blah')
|
66
|
|
|
# logger1.info('balh blah')
|
67
|
|
|
# logger2.warning('balh blah')
|
68
|
|
|
# logger3.error('balh blah')
|
69
|
|
|
|