-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging
More file actions
executable file
·57 lines (43 loc) · 1.47 KB
/
Copy pathlogging
File metadata and controls
executable file
·57 lines (43 loc) · 1.47 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
#!/usr/bin/env python
# Also see loguru
import logging
import os
import sys
from functools import wraps
def log_decorator(f):
@wraps(f)
def wrapper(*args, **kwargs):
print(f"wrapped by decorator {args}: ", end='')
return f(*args, **kwargs)
return wrapper
class LogWrapper(logging.Logger):
def __init__(self, name, level=logging.NOTSET):
return super(LogWrapper, self).__init__(name, level)
def info(self, msg, *args, **kwargs):
print(f"log wrapper called with {msg}: ", end='')
return super(LogWrapper, self).info(msg, *args, **kwargs)
logging.basicConfig(
stream=sys.stdout,
# level can be string or integer. eg level=logging.INFO
level=os.getenv('LOGLEVEL', 'INFO')
)
logger = logging.getLogger('default')
class Foo(Exception): pass
try:
raise Foo('oops')
except:
logger.debug('test debug')
logger.info('test info')
logging.info('test logging.info to logging')
logger.warning('test warning')
logger.error('test error')
logger.fatal('test fatal', exc_info=1) # Show the stacktrace
logger.critical('test critical')
logger.exception('exception') # Show the stacktrace
logging.setLoggerClass(LogWrapper)
logging.info('foo') # Calls root logger, not wrapper
logging.info = log_decorator(logging.info)
logging.info('bar') # Root logger with decorated info
logger = logging.getLogger('log')
logger.info('baz') # Calls LogWrapper.info
logger.warning('qux') # Calls unwrapped warning