-
Notifications
You must be signed in to change notification settings - Fork 957
/
log.py
132 lines (105 loc) · 4.7 KB
/
log.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import logging
import os
import google.auth
_LOG_TO_FILE_ENV = os.getenv("KAGGLE_LOG_TO_FILE")
class _LogFormatter(logging.Formatter):
"""A logging formatter which truncates long messages."""
_MAX_LOG_LENGTH = 10000 # Be generous, not to truncate long backtraces.
def format(self, record):
msg = super(_LogFormatter, self).format(record)
return msg[:_LogFormatter._MAX_LOG_LENGTH] if msg else msg
# TODO(vimota): Clean this up once we're using python 3.8 and can use
# (https://github.com/python/cpython/commit/dde9fdbe453925279ac3d2a6a72102f6f9ef247c)
# Right now, making the logging module display the intended frame's information
# when the logging calls (info, warn, ...) are wrapped (as is the case in our
# Log class) involves fragile logic.
class _Logger(logging.Logger):
# This is a copy of logging.Logger.findCaller with the filename ignore
# set expanded to include the current filename (".../log.py").
# Copyright 2001-2015 by Vinay Sajip. All Rights Reserved.
# License: https://github.com/python/cpython/blob/ce9e62544571e7ade7186697d5dd065fb4c5243f/LICENSE
def findCaller(self, stack_info=False, stacklevel=1):
f = logging.currentframe()
f = f.f_back
rv = "(unknown file)", 0, "(unknown function)", None
while hasattr(f, "f_code"):
co = f.f_code
filename = os.path.normcase(co.co_filename)
if filename in _ignore_srcfiles:
f = f.f_back
continue
sinfo = None
if stack_info:
sio = io.StringIO()
sio.write('Stack (most recent call last):\n')
traceback.print_stack(f, file=sio)
sinfo = sio.getvalue()
if sinfo[-1] == '\n':
sinfo = sinfo[:-1]
sio.close()
rv = (co.co_filename, f.f_lineno, co.co_name, sinfo)
break
return rv
_srcfile = os.path.normcase(_Logger.findCaller.__code__.co_filename)
_ignore_srcfiles = (_srcfile, logging._srcfile)
class Log:
""" Helper aggregate for all things related to logging activity. """
_GLOBAL_LOG = logging.getLogger("")
_initialized = False
# These are convenience helpers. For performance, consider saving Log.get_logger() and using that
@staticmethod
def critical(msg, *args, **kwargs):
Log._GLOBAL_LOG.critical(msg, *args, **kwargs)
@staticmethod
def fatal(msg, *args, **kwargs):
Log._GLOBAL_LOG.fatal(msg, *args, **kwargs)
@staticmethod
def exception(msg, *args, **kwargs):
Log._GLOBAL_LOG.exception(msg, *args, **kwargs)
@staticmethod
def error(msg, *args, **kwargs):
Log._GLOBAL_LOG.error(msg, *args, **kwargs)
@staticmethod
def warn(msg, *args, **kwargs):
Log._GLOBAL_LOG.warn(msg, *args, **kwargs)
@staticmethod
def warning(msg, *args, **kwargs):
Log._GLOBAL_LOG.warning(msg, *args, **kwargs)
@staticmethod
def debug(msg, *args, **kwargs):
Log._GLOBAL_LOG.debug(msg, *args, **kwargs)
@staticmethod
def info(msg, *args, **kwargs):
Log._GLOBAL_LOG.info(msg, *args, **kwargs)
@staticmethod
def set_level(loglevel):
if isinstance(loglevel, int):
Log._GLOBAL_LOG.setLevel(loglevel)
return
elif isinstance(loglevel, str):
# idea from https://docs.python.org/3.5/howto/logging.html#logging-to-a-file
numeric_level = getattr(logging, loglevel.upper(), None)
if isinstance(numeric_level, int):
Log._GLOBAL_LOG.setLevel(numeric_level)
return
raise ValueError('Invalid log level: %s' % loglevel)
@staticmethod
def _static_init():
if Log._initialized:
return
logging.setLoggerClass(_Logger)
# The root logger's type is unfortunately (and surprisingly) not affected by
# `setLoggerClass`. Monkey patch it instead. TODO(vimota): Remove this, see the TODO
# associated with _Logger.
logging.RootLogger.findCaller = _Logger.findCaller
log_to_file = _LOG_TO_FILE_ENV.lower() in ("yes", "true", "t", "1") if _LOG_TO_FILE_ENV is not None else True
if log_to_file:
handler = logging.FileHandler(filename='/tmp/kaggle.log', mode='w')
else:
handler = logging.StreamHandler()
# ".1s" is for the first letter: http://stackoverflow.com/a/27453084/1869.
format_string = "%(asctime)s %(levelname).1s %(process)d %(filename)s:%(lineno)d] %(message)s"
handler.setFormatter(_LogFormatter(format_string))
logging.basicConfig(level=logging.INFO, handlers=[handler])
Log._initialized = True
Log._static_init()