-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogging_errors.py
97 lines (69 loc) · 3.39 KB
/
logging_errors.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'''Logging Error Messages'''
# The Python standard library module for logging is logging.
# https://docs.python.org/3/library/logging.html
# The logging module includes:
# – The message that you want to save to the log
# – Ranked priority levels as functions:
# debug(), info(), warning(), error(), and critical()
# – One or more logger objects as the main connection with the module
# – Handlers that direct the message to your terminal, a file, a database,
# or somewhere else
# – Formatters that create the output
# – Filters that make decisions based on the input
import logging
fmt = '%(asctime)s %(levelname)s %(lineno)s %(message)s'
logging.basicConfig(level=logging.DEBUG, filename='data/test.log', format=fmt)
# priority levels:
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')
# Breakdown
# -----------------------------------------------------------------------------
# The default priority level is warn. Set the default by using basicConfig().
# This config should appear before any other logging functions as above.
logging.basicConfig(level=logging.DEBUG)
# handlers direct messages to different places, for example, a log file:
logging.basicConfig(level=logging.DEBUG, filename='data/test.log')
# you can format your logged messages:
fmt = '%(asctime)s %(levelname)s %(lineno)s %(message)s'
logging.basicConfig(level=logging.DEBUG, filename='data/test.log', format=fmt)
# here's a logger object:
logger = logging.getLogger('MyLogger')
logger.debug('here is my debug message')
# Calling basicConfig() with a filename argument created a FileHandler and made
# it available to the logger. The logging module includes at least 15 handlers
# to send messages to places such as the screen, email, web servers and files.
# The information here is pretty scant. For more information on this topic, go
# through the following in Doug Hellman's Python 3 Standard Library by example:
# p.980–986, p.563–564, p.644–647, p.650–653, p.671–674
# Log Record attributes
# -----------------------------------------------------------------------------
# There are a number of attributes (bits of information) that can be included
# in your logged message format. For a full list see:
# https://docs.python.org/3/library/logging.html#logrecord-attributes
# Logging Levels
# -----------------------------------------------------------------------------
# The predifined logging levels all have a numeric value associated to them.
# This is mostly of interest because you can define your own levels.
# Level | Numeric value
# ----------|--------------
# CRITICAL | 50
# ERROR | 40
# WARNING | 30
# INFO | 20
# DEBUG | 10
# NOTSET | 0
# To define your own level:
SPECIAL_INFO_LEVEL_NUM = 21
logging.addLevelName(SPECIAL_INFO_LEVEL_NUM, 'SPECIAL_INFO')
def special_info(self, message, *args, **kws):
if self.isEnabledFor(SPECIAL_INFO_LEVEL_NUM):
self._log(SPECIAL_INFO_LEVEL_NUM, message, args, **kws)
logging.Logger.special_info = special_info
logger.special_info('Hey, this is special')
# That being said, while defining your own levels is possible, it shouldn't
# really be necessary, as the existing levels have been chosen on the basis of
# practical experience. It's also a very bad idea if you are developing a
# library as your custom level could conflict with others.