-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.py
49 lines (38 loc) · 1.76 KB
/
example.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
import logging
import pprint
import colorlogging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler()
logger.addHandler(handler)
# Color/style modifiers can be added to format strings
fmt = "%(asctime)s %(levelname)s: #(magenta)%(message)s"
formatter = colorlogging.ColorFormatter(fmt)
handler.setFormatter(formatter)
logger.info('This will be printed in magenta!')
# They can also be added to messages
logger.info('#(underlined green)This message will be underlined and green!')
# level is a special color that depends on the log level
# The usual use case would be coloring the level name
# Note that #(plain) is used so that the message doesn't get styled
fmt = "%(asctime)s #(level)%(levelname)s#(plain): %(message)s"
formatter = colorlogging.ColorFormatter(fmt)
handler.setFormatter(formatter)
# The standard log levels have default colors. INFO is green, WARN yellow, etc.
# setLevelColor can be used to modify the defaults,
formatter.setLevelColor(logging.INFO, 'inverted light cyan')
logger.info('Nothing to see, just a humble log message.')
# Or to add colors for custom log levels
formatter.setLevelColor(35, 'blink red')
logger.log(35, 'There are too many FOOs in your BARs')
# By default, style modifiers clear previous styles
formatter = colorlogging.ColorFormatter()
handler.setFormatter(formatter)
logger.info('#(bold)This is bold. #(blue)This is only blue, not bold.')
# This can be changed by setting the additive parameter
# If set to True, styles will be applied cumulatively
formatter = colorlogging.ColorFormatter(additive=True)
handler.setFormatter(formatter)
logger.info('#(bold)Bold. #(blue)Bold blue. #(not bold)Blue.')
# You can find the list of availabe style and colors in COLORS
pprint.pprint(colorlogging.COLORS)