-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
511 lines (408 loc) · 16.2 KB
/
Copy path__init__.py
File metadata and controls
511 lines (408 loc) · 16.2 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
""" Polyfill for the `logging` module.
WARNING:
This module is not complete by any means, nor it follows strictly the `logging` module API.
For example, here are some differences:
- The `Logger` class does not currently support parents
- The `Logger` class does not currently support filters
- The `Logger` class does not currently support handlers
- The `Logger` class does not currently support propagation
"""
import sys as _sys
import time as _time
import unittest as _unittest
try:
# fmt: off
from java.lang.management import ManagementFactory as _ManagementFactory # pyright: ignore[reportMissingImports]
# fmt: on
except ImportError:
pass
try:
import warnings as _warnings
except ImportError:
pass
__all__ = ["getLogger", "basicConfig"]
# --------------------- Transform levels to values and viceversa ---------------------
# (completely stripped from the `logging` module :D)
CRITICAL = 50
FATAL = CRITICAL
ERROR = 40
WARNING = 30
WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0
_levelToName = {
CRITICAL: "CRITICAL",
ERROR: "ERROR",
WARNING: "WARNING",
INFO: "INFO",
DEBUG: "DEBUG",
NOTSET: "NOTSET",
}
_nameToLevel = {
"CRITICAL": CRITICAL,
"FATAL": FATAL,
"ERROR": ERROR,
"WARN": WARNING,
"WARNING": WARNING,
"INFO": INFO,
"DEBUG": DEBUG,
"NOTSET": NOTSET,
}
def _checkLevel(level):
# type: (str|int) -> int
"""Given a level name or level number, return the level number.
Args:
level (str|int): The level name or number.
Returns:
int: The level number.
"""
if type(level).__name__ in ["int", "org.python.core.PyInteger"]:
rv = level
elif str(level) == level:
if level not in _nameToLevel.keys():
raise ValueError("Unknown level: %r" % level)
rv = _nameToLevel[level]
else:
raise TypeError("Level not an integer or a valid string: %r" % level)
return rv
def getLevelName(level):
"""
Return the textual or numeric representation of logging level 'level'.
If the level is one of the predefined levels (CRITICAL, ERROR, WARNING,
INFO, DEBUG) then you get the corresponding string. If you have
associated levels with names using addLevelName then the name you have
associated with 'level' is returned.
If a numeric value corresponding to one of the defined levels is passed
in, the corresponding string representation is returned.
If a string representation of the level is passed in, the corresponding
numeric value is returned.
If no matching numeric or string value is passed in, the string
'Level %s' % level is returned.
"""
# See Issues #22386, #27937 and #29220 for why it's this way
result = _levelToName.get(level)
if result is not None:
return result
result = _nameToLevel.get(level)
if result is not None:
return result
return "Level %s" % level
# ------------------------------------------------------------------------------------
ROOT_LOGGER_NAME = "root"
DEFAULT_LOGGING_FORMAT = "%(levelname)s:%(name)s:%(message)s"
DEFAULT_LOGGING_DATE_FORMAT = "%Y-%m-%d %H:%M %Z"
# Define a logger class useful for printing helpful messages
class Logger:
"""Logger class for Python 2.x that mimics the `logging` module."""
def __init__(
self,
name,
level=NOTSET,
):
"""Create the logger.
Args:
name (str, optional): The name of the logger.
level (str, optional): The minimum logging level. Defaults to "info".
"""
self.name = name
self.level = _checkLevel(level)
self._format = DEFAULT_LOGGING_FORMAT
self._time_format = DEFAULT_LOGGING_DATE_FORMAT
# From the `logging` module
self._cache = {}
self.parent = None
try:
self._process_id = (
_ManagementFactory.getRuntimeMXBean().getName().split("@")[0]
)
except:
self._process_id = 0
def setLevel(self, level):
# type: (str) -> None
"""Sets the minimum level of the current logger."""
self.level = _checkLevel(level)
self._cache = {}
def getEffectiveLevel(self):
"""
Get the effective level for this logger.
Loop through this logger and its parents in the logger hierarchy,
looking for a non-zero logging level. Return the first one found.
"""
logger = self
while logger:
if logger.level:
return logger.level
logger = logger.parent
return NOTSET
def isEnabledFor(self, level):
"""
Is this logger enabled for level 'level'?
"""
try:
return self._cache[level]
except KeyError:
is_enabled = level >= self.getEffectiveLevel()
self._cache[level] = is_enabled
return is_enabled
def log(self, _log_level, message):
# type: (str, str|int) -> None
"""Base function used by the other logging methods to abstract the logic.
Args:
message (str): The message to log.
_log_level (str|int): The level of the message.
"""
if not self.isEnabledFor(_log_level):
return
creation_time = _time.time()
caller = _sys._getframe().f_back.f_back.f_code.co_name
if caller == "?":
caller = "__main__"
# See https://docs.python.org/3/library/logging.html#logrecord-attributes
# fmt: off
print(
self._format
% {
"asctime": _time.strftime(
self._time_format, _time.localtime(creation_time)
),
"created": creation_time,
"filename": "__NOT_IMPLEMENTED__", # TODO: LogRecord 'filename' not implemented
"funcName": caller,
"levelname": getLevelName(_log_level),
"levelno": _log_level,
"lineno": 0, # TODO: LogRecord 'lineno' not implemented
"message": str(message),
"module": "__NOT_IMPLEMENTED__", # TODO: LogRecord 'module' not implemented
"msecs": int(str("%.3f" % creation_time).split(".")[1]),
"name": self.name,
"pathname": "__NOT_IMPLEMENTED__", # TODO: LogRecord 'pathname' not implemented
"process": int(self._process_id),
"processName": "__NOT_IMPLEMENTED__", # TODO: LogRecord 'processName' not implemented
"relativeCreated": 0, # TODO: LogRecord 'relativeCreated' not implemented
"thread": 0, # TODO: LogRecord 'thread' not implemented
"threadName": "__NOT_IMPLEMENTED__", # TODO: LogRecord 'threadName' not implemented
}
)
# fmt: on
def debug(self, message=""):
self.log(DEBUG, message)
def info(self, message=""):
self.log(INFO, message)
def warning(self, message=""):
self.log(WARNING, message)
def error(self, message=""):
self.log(ERROR, message)
def critical(self, message=""):
self.log(CRITICAL, message)
def fatal(self, message=""):
self.log(FATAL, message)
class RootLogger(Logger):
"""
A root logger is not that different to any other logger, except that
it must have a logging level and there is only one instance of it in
the hierarchy.
"""
def __init__(self, level):
"""
Initialize the logger with the name "root".
"""
Logger.__init__(self, ROOT_LOGGER_NAME, level)
root = RootLogger(WARNING)
def getLogger(name=None):
# type: (str) -> Logger
"""Returns a logger object with the specified name."""
if name is None or (type(name).__class__ in ["str", "org.python.core.PyString"] and name == ROOT_LOGGER_NAME):
return root
return Logger(name)
def basicConfig(**kwargs):
"""Basic configuration for the logging system.
Args:
level (str, optional): The minimum logging level. Defaults to "info".
format (str, optional): The logging format as specified in the docs (https://docs.python.org/3/library/logging.html#logrecord-attributes). Defaults to `%(levelname)s:%(name)s:%(message)s`.
datefmt (str, optional): The human readable time format. Defaults to `%Y-%m-%d %H:%M %Z`.
Raises:
ValueError: If an unrecognised argument is passed.
Examples:
```pycon
>>> import polyfills.logging as logging
>>> logging.basicConfig(level="DEBUG")
>>> logging.info("Hello World!")
INFO:root:Hello World!
>>> logging.basicConfig(level=logging.INFO, format="[%(asctime)s] %(levelname)s - %(name)s - %(message)s", datefmt="%Y-%m-%d %H:%M")
>>> logging.info("Hello World!")
[2021-03-21 16:20] INFO - root - Hello World!
```
"""
unrecognised_keys = [
key for key in kwargs.keys() if key not in ["level", "format", "datefmt"]
]
if unrecognised_keys:
raise ValueError(
"Unrecognised argument(s): %s" % (", ".join(unrecognised_keys))
)
logger_level = kwargs.get("level", None)
logger_format = kwargs.get("format", None)
logger_datefmt = kwargs.get("datefmt", None)
if logger_level is not None:
root.setLevel(logger_level)
if logger_format is not None:
root._format = logger_format
if logger_datefmt is not None:
root._time_format = logger_datefmt
# ---------------------------------------------------------------------------
# Utility functions at module level.
# Basically delegate everything to the root logger.
# ---------------------------------------------------------------------------
def critical(msg, *args, **kwargs):
"""
Log a message with severity 'CRITICAL' on the root logger. If the logger
has no handlers, call basicConfig() to add a console handler with a
pre-defined format.
"""
# if len(root.handlers) == 0:
# basicConfig()
root.critical(msg, *args, **kwargs)
fatal = critical
def error(msg, *args, **kwargs):
"""
Log a message with severity 'ERROR' on the root logger. If the logger has
no handlers, call basicConfig() to add a console handler with a pre-defined
format.
"""
# if len(root.handlers) == 0:
# basicConfig()
root.error(msg, *args, **kwargs)
def exception(msg, *args, **kwargs):
"""
Log a message with severity 'ERROR' on the root logger, with exception
information. If the logger has no handlers, basicConfig() is called to add
a console handler with a pre-defined format.
"""
error(msg, *args, **kwargs)
def warning(msg, *args, **kwargs):
"""
Log a message with severity 'WARNING' on the root logger. If the logger has
no handlers, call basicConfig() to add a console handler with a pre-defined
format.
"""
# if len(root.handlers) == 0:
# basicConfig()
root.warning(msg, *args, **kwargs)
def warn(msg, *args, **kwargs):
try:
_warnings.warn(
"The 'warn' function is deprecated, " "use 'warning' instead",
DeprecationWarning,
2,
)
except NameError:
pass
warning(msg, *args, **kwargs)
def info(msg, *args, **kwargs):
"""
Log a message with severity 'INFO' on the root logger. If the logger has
no handlers, call basicConfig() to add a console handler with a pre-defined
format.
"""
# if len(root.handlers) == 0:
# basicConfig()
root.info(msg, *args, **kwargs)
def debug(msg, *args, **kwargs):
"""
Log a message with severity 'DEBUG' on the root logger. If the logger has
no handlers, call basicConfig() to add a console handler with a pre-defined
format.
"""
# if len(root.handlers) == 0:
# basicConfig()
root.debug(msg, *args, **kwargs)
def log(level, msg, *args, **kwargs):
"""
Log 'msg % args' with the integer severity 'level' on the root logger. If
the logger has no handlers, call basicConfig() to add a console handler
with a pre-defined format.
"""
# if len(root.handlers) == 0:
# basicConfig()
root.log(level, msg, *args, **kwargs)
class LoggingTestCase(_unittest.TestCase):
def test_getLogger_name(self):
logger = getLogger()
self.assertEqual(logger.name, ROOT_LOGGER_NAME)
logger = getLogger("test_logger")
self.assertEqual(logger.name, "test_logger")
def test_setlevel(self):
_true = 1 == 1
_false = 1 == 0
logger = getLogger()
self.assertEqual(logger.getEffectiveLevel(), WARNING)
self.assertEqual(logger.isEnabledFor(WARNING), _true)
self.assertEqual(logger.isEnabledFor(DEBUG), _false)
logger = getLogger("test_logger")
self.assertEqual(logger.getEffectiveLevel(), NOTSET)
self.assertEqual(logger.isEnabledFor(WARNING), _true)
self.assertEqual(logger.isEnabledFor(DEBUG), _true)
logger.setLevel("DEBUG")
self.assertEqual(logger.getEffectiveLevel(), DEBUG)
self.assertEqual(logger.isEnabledFor(WARNING), _true)
self.assertEqual(logger.isEnabledFor(DEBUG), _true)
logger.setLevel("INFO")
self.assertEqual(logger.getEffectiveLevel(), INFO)
self.assertEqual(logger.isEnabledFor(WARNING), _true)
self.assertEqual(logger.isEnabledFor(DEBUG), _false)
logger.setLevel("WARNING")
self.assertEqual(logger.getEffectiveLevel(), WARNING)
self.assertEqual(logger.isEnabledFor(WARNING), _true)
self.assertEqual(logger.isEnabledFor(DEBUG), _false)
logger.setLevel("ERROR")
self.assertEqual(logger.getEffectiveLevel(), ERROR)
self.assertEqual(logger.isEnabledFor(FATAL), _true)
self.assertEqual(logger.isEnabledFor(WARNING), _false)
self.assertEqual(logger.isEnabledFor(DEBUG), _false)
logger.setLevel("CRITICAL")
self.assertEqual(logger.getEffectiveLevel(), CRITICAL)
self.assertEqual(logger.isEnabledFor(FATAL), _true)
self.assertEqual(logger.isEnabledFor(WARNING), _false)
self.assertEqual(logger.isEnabledFor(DEBUG), _false)
logger.setLevel("FATAL")
self.assertEqual(logger.getEffectiveLevel(), FATAL)
self.assertEqual(logger.isEnabledFor(FATAL), _true)
self.assertEqual(logger.isEnabledFor(WARNING), _false)
self.assertEqual(logger.isEnabledFor(DEBUG), _false)
def test_logger_singleton(self):
logger = getLogger()
self.assertEqual(logger, root)
logger = getLogger("test_logger")
self.assertNotEqual(logger, root)
# BUG: Multiple calls to getLogger with the same name should return the same logger object
# self.assertEqual(getLogger("logger"), getLogger("logger"))
if __name__ == "__main__":
print("--------------- Testing root logger ---------------")
basicConfig(format="%(levelname)s: %(message)s")
info("This is a message with severity 'INFO'")
debug("This is a message with severity 'DEBUG'")
warning("This is a message with severity 'WARNING'")
error("This is a message with severity 'ERROR'")
critical("This is a message with severity 'CRITICAL'")
fatal("This is a message with severity 'FATAL'")
log(CRITICAL, "This is a custom log with severity 'CRITICAL'")
assert getLogger() == getLogger(name=None)
print("\n\n")
print("--------------- Testing custom logger ---------------")
logger = getLogger("test_logger")
print(
"Effective level for logger '%s': %d\n"
% (logger.name, logger.getEffectiveLevel())
)
for method in ["debug", "info", "warning", "error", "critical", "fatal"]:
getattr(logger, method)("This is a message with level '%s'" % method.upper())
print("\n\n")
print("--------------- Setting level to 'WARNING' ---------------")
logger.setLevel("WARNING")
print(
"Effective level for logger '%s': %d\n"
% (logger.name, logger.getEffectiveLevel())
)
for method in ["debug", "info", "warning", "error", "critical", "fatal"]:
getattr(logger, method)("This is a message with level '%s'" % method.upper())