-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.py
56 lines (41 loc) · 1.5 KB
/
log.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
51
52
53
54
55
56
import logging
import settings
LOGGER_NAME = 'honeybee'
class Logger:
"""
Should get instance only get_instance. \n
--------------------------------------- \n
USE CASE: \n
log = Loggger.get_instance() \n
log.info("Message what you want to record.") \n
"""
_instance = None
def __init__(self, log_level=logging.INFO) -> None:
self.logger = logging.getLogger(LOGGER_NAME) # named logger 설정.
self.logger.setLevel(log_level)
formatter = logging.Formatter(
fmt='%(asctime)s - %(name)s - %(levelname)s - %(filename)s: %(funcName)s \n%(message)s',
datefmt='%m/%d/%Y %I:%M:%S %p'
)
stream_handler = logging.StreamHandler() #그냥 handler는 ABC임.
stream_handler.setFormatter(formatter)
self.logger.addHandler(stream_handler)
file_handler = logging.FileHandler('log.log')
file_handler.setFormatter(formatter)
self.logger.addHandler(file_handler)
def get_logger(self):
return self.logger
# singleton approach.
@classmethod
def get_instance(cls):
if not cls._instance:
cls._instance = cls(log_level=settings.log_level).get_logger()
return cls._instance
def info(self, msg):
self.logger.info(msg)
def warning(self, msg):
self.logger.info(msg)
def error(self, msg):
self.logger.error(msg)
def critical(self, msg):
self.logger.critical(msg)