-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
50 lines (41 loc) · 1.42 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
import logging
import sys
def get_logger(
level: bool = None,
log_file: bool = False,
log_location: bool = False,
disable_requests_logging: bool = True,
) -> logging.Logger:
stdout_handler = logging.StreamHandler(sys.stdout)
handlers = [stdout_handler]
if log_file:
if log_location:
file_handler = logging.FileHandler(log_location)
else:
import time
file_handler = logging.FileHandler(
f"sync2snipe-{time.strftime('%m-%d-%Y')}.log"
)
handlers.append(file_handler)
log_level = logging.INFO
if level:
level = level.upper()
if level == "DEBUG":
log_level = logging.DEBUG
elif level == "ERROR":
log_level = logging.ERROR
elif level == "CRITICAL":
log_level = logging.CRITICAL
elif level == "WARNING":
log_level = logging.WARNING
logging.basicConfig(
level=log_level,
format="[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s",
handlers=handlers,
)
if disable_requests_logging:
# requests and urllib3 make a whole lot of noise. supress that.
logging.getLogger("urllib3").setLevel(logging.WARNING)
# Disable all child loggers of urllib3, e.g. urllib3.connectionpool
logging.getLogger("urllib3").propagate = False
return logging.getLogger(__file__)