-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
51 lines (40 loc) · 1.81 KB
/
util.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
import os
import logging
import warnings
import pikaconfig
try:
from cloghandler import ConcurrentRotatingFileHandler as RotatingFileHandler
except ImportError:
warnings.warn("cloghandler not available, will use NON-concurrent log handler")
from logging.handlers import RotatingFileHandler as OrigRotatingFileHandler
def RotatingFileHandler(*args, **kwargs):
# Function for conforming with ConcurrentRotatingFileHandler
# which accepts a debug parameter.
kwargs.pop('debug', None)
return OrigRotatingFileHandler(*args, **kwargs)
USE_GELF = getattr(pikaconfig, 'USE_GELF', False)
def setupLogHandlers(fname, formatter=None, **kwargs):
"""
Create a RotatingFileHandler to be used by a logger, and possibly a
GELFHandler.
By default the RotatingFileHandler stores 100 MB before starting a new
log file and the last 10 log files are kept. The default formatter shows
the logging level, current time, the function that created the log entry,
and the specified message.
:param str fname: path to the filename where logs will be written to
:param logging.Formatter formatter: a custom formatter for this logger
:param kwargs: custom parameters for the RotatingFileHandler
:rtype: tuple
"""
if formatter is None:
formatter = logging.Formatter('%(levelname)s [%(asctime)s] %(funcName)s: %(message)s')
opts = {'maxBytes': 100 * 1024 * 1024, 'backupCount': 10, 'debug': False}
opts.update(kwargs)
handler = RotatingFileHandler(os.path.join(pikaconfig.LOG_DIR, fname), **opts)
handler.setFormatter(formatter)
handlers = (handler, )
if USE_GELF:
gelf_handler = GELFHandler(**USE_GELF)
gelf_handler.setLevel(logging.INFO) # Ignore DEBUG messages.
handlers += (gelf_handler, )
return handlers