-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
50 lines (38 loc) · 1.31 KB
/
logger.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
""" Configure a logger for the application. """
from logging import INFO, DEBUG, getLogger, FileHandler, Formatter, StreamHandler
from logentries import LogentriesHandler
from sys import stdout
from app import app
# noinspection PyIncorrectDocstring
def get_custom_logger(name):
""" Set up loggers according to environment and configuration. """
file = app.config['LOGGER_FILEPATH']
token = app.config['LOGENTRIES_TOKEN']
level = DEBUG if app.config['DEBUG'] else INFO
context = app.config['ENVIRONMENT']
log = getLogger(name)
log.setLevel(level)
# shared by all handlers
formatter = Formatter(
'[%(asctime)s] '
'[%(process)d] '
'[%(name)s] '
'[%(levelname)s] '
'%(message)s'
)
if context != 'testing' and file:
file = FileHandler(file, mode='a+')
file.setLevel(level)
file.setFormatter(formatter)
log.addHandler(file)
if context == 'development':
console = StreamHandler(stream=stdout)
console.setLevel(level)
console.setFormatter(formatter)
log.addHandler(console)
if context != 'testing' and token:
logentries = LogentriesHandler(token)
logentries.setLevel(level)
logentries.setFormatter(formatter)
log.addHandler(logentries)
return log