-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
46 lines (40 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
"""Logger for BESS."""
import logging
from dataclasses import dataclass
@dataclass
class Logger():
"""Handles logging."""
logfile = str
debug = str
loglevel = str
def __attrs_post_init__(self):
"""Initialization."""
# File logger
if self.debug:
loglevel_root = logging.DEBUG
else:
loglevel_root = logging.INFO
root_logger = logging.getLogger()
root_logger.setLevel(loglevel_root)
handler = logging.FileHandler(self.logfile, 'w', 'utf-8')
handler.setFormatter(logging.Formatter(("%(asctime)s: %(levelname)s:"
"%(name)s: %(message)s")))
root_logger.addHandler(handler)
# Logger for console.
console = logging.StreamHandler()
if self.loglevel == "q":
self.loglevel = logging.ERROR
elif self.loglevel == "v":
self.loglevel = logging.INFO
else:
self.loglevel = logging.WARNING
console.setLevel(self.loglevel)
console.setFormatter(logging.Formatter(
"%(name)-15s: %(levelname)-7s: %(message)s"))
logging.getLogger("").addHandler(console)
def abbr_log(self, minimal, full):
"""Log minimal when not set to VERBOSE, otherwise log full."""
if self.loglevel > logging.INFO:
print(minimal)
else:
logging.info(full)