-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcm_logging.py
More file actions
72 lines (57 loc) · 1.76 KB
/
Copy pathpcm_logging.py
File metadata and controls
72 lines (57 loc) · 1.76 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
import os
import logging
from logging.handlers import RotatingFileHandler
_logger_registry = {}
_initialized = False
def init_from_env():
global _initialized
if _initialized:
return
_initialized = True
livello_nome = os.environ.get("PCM_LOG_LEVEL", None)
livello = logging.INFO
if livello_nome:
try:
livello = getattr(logging, livello_nome.upper())
except AttributeError:
livello = logging.INFO
root = logging.getLogger("pcm")
root.setLevel(livello)
root.handlers.clear()
fmt = logging.Formatter(
"%(asctime)s [%(levelname)s] %(name)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
log_dir = os.path.join(
os.path.expanduser("~"), ".local", "share", "pcm"
)
os.makedirs(log_dir, mode=0o700, exist_ok=True)
log_file = os.path.join(log_dir, "pcm.log")
fh = RotatingFileHandler(log_file, maxBytes=1_000_000, backupCount=3)
fh.setFormatter(fmt)
fh.setLevel(livello)
root.addHandler(fh)
sh = logging.StreamHandler()
sh.setFormatter(fmt)
sh.setLevel(livello)
root.addHandler(sh)
def init_from_settings(settings):
livello_nome = (
os.environ.get("PCM_LOG_LEVEL")
or settings.get("general", {}).get("log_level", "INFO")
)
livello = logging.INFO
if livello_nome:
try:
livello = getattr(logging, livello_nome.upper())
except AttributeError:
livello = logging.INFO
root = logging.getLogger("pcm")
root.setLevel(livello)
for h in root.handlers:
h.setLevel(livello)
def get_logger(name):
init_from_env()
return logging.getLogger(f"pcm.{name}")
def get_log_dir():
return os.path.join(os.path.expanduser("~"), ".local", "share", "pcm")