-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_logging.py
More file actions
61 lines (44 loc) · 1.87 KB
/
python_logging.py
File metadata and controls
61 lines (44 loc) · 1.87 KB
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
import logging
#references
#https://docs.python.org/ko/3/howto/logging-cookbook.html#logging-cookbook
#https://docs.python.org/ko/3/howto/logging.html#useful-handlers
logging.basicConfig(level = logging.INFO)
l = logging.getLogger()
l.setLevel(logging.INFO)
l.info('a')
capi = logging.getLogger('capi')
print(capi.handlers)
console = logging.StreamHandler()
BF = '%(asctime)s:'+logging.BASIC_FORMAT
formatter = logging.Formatter(BF)
console.setFormatter(formatter)
console.setLevel(logging.INFO)
capi.addHandler(console)
capi.propagate=False
capi.info('a')
import logging
# set up logging to file - see previous section for more details
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M',
filename='myapp.log',
filemode='w')
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)
# Now, we can log to the root logger, or any other logger. First the root...
logging.info('Jackdaws love my big sphinx of quartz.')
# Now, define a couple of other loggers which might represent areas in your
# application:
logger1 = logging.getLogger('myapp.area1')
logger2 = logging.getLogger('myapp.area2')
logger1.debug('Quick zephyrs blow, vexing daft Jim.')
logger1.info('How quickly daft jumping zebras vex.')
logger2.warning('Jail zesty vixen who grabbed pay from quack.')
logger2.error('The five boxing wizards jump quickly.')