-
-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathlog.py
More file actions
44 lines (38 loc) · 1.45 KB
/
log.py
File metadata and controls
44 lines (38 loc) · 1.45 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
# -----------------------------------------------------------------------------
# Copyright (c) 2009-2016 Nicolas P. Rougier. All rights reserved.
# Distributed under the (new) BSD License.
# -----------------------------------------------------------------------------
import logging
log = logging.getLogger('glumpy')
log.setLevel(logging.INFO)
# log.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
# ch.setLevel(logging.INFO)
# create formatter
# formatter = logging.Formatter('%(levelname)s: %(message)s')
# formatter = logging.Formatter('%(message)s')
class Formatter(logging.Formatter):
def format(self, record):
prefix = {logging.INFO : "[i]",
logging.WARNING : "[w]",
logging.ERROR : "[e]",
logging.CRITICAL: "[x]"}
if record.levelno in (
logging.INFO,
logging.WARNING,
logging.ERROR,
logging.CRITICAL):
# record.msg = '[%s] %s' % (record.levelname, record.msg)
record.msg = '%s %s' % (prefix[record.levelno], record.msg)
return super(Formatter , self).format(record)
formatter = Formatter('%(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
log.addHandler(ch)
# log.debug('debug message')
# log.info('info message')
# log.warn('warn message')
# log.error('error message')
# log.critical('critical message')