-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.13_python_logging.py
45 lines (33 loc) · 1.23 KB
/
1.13_python_logging.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
import logging
import sys
import os
import socket
from logging.handlers import RotatingFileHandler
from config import LOG_DIR
class Logger:
_logger = None
def __new__(cls, *args, **kwargs):
if cls._logger is None:
cls._logger = logging.getLogger("root")
if not cls._logger.handlers:
formatter = logging.Formatter("%(levelname)9s | %(asctime)s [%(filename)s:%(lineno)d] %(message)s")
streamHandler = logging.StreamHandler()
fileHandler = RotatingFileHandler(
os.path.join(LOG_DIR, "widget-host-%s.log" % socket.gethostname()),
mode="a",
maxBytes=5 * 1024 * 1024,
backupCount=2,
encoding=None,
delay=0,
)
cls._logger.setLevel(logging.DEBUG)
fileHandler.setFormatter(formatter)
streamHandler.setFormatter(formatter)
cls._logger.addHandler(fileHandler)
cls._logger.addHandler(streamHandler)
return cls._logger
if __name__ == "__main__":
log = Logger()
log.info("info ...")
log.debug("debug ...")
log.warning("warning ...")