-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathlog.py
More file actions
36 lines (30 loc) · 1.17 KB
/
log.py
File metadata and controls
36 lines (30 loc) · 1.17 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
# Copyright (c) 2022 Graphcore Ltd. All rights reserved.
import logging
from logging import handlers
class Logger(object):
level_relations = {
"debug": logging.DEBUG,
"info": logging.INFO,
"warning": logging.WARNING,
"error": logging.ERROR,
"crit": logging.CRITICAL,
}
def __init__(self, filename, level="info", when="D", backCount=3, fmt="[%(asctime)s] %(message)s"):
self.filename = filename
self.logger = logging.getLogger(filename)
format_str = logging.Formatter(fmt)
self.logger.setLevel(self.level_relations.get(level))
sh = logging.StreamHandler()
sh.setFormatter(format_str)
th = handlers.TimedRotatingFileHandler(filename=filename, when=when, backupCount=backCount, encoding="utf-8")
th.setFormatter(format_str)
self.logger.addHandler(sh)
self.logger.addHandler(th)
if __name__ == "__main__":
log = Logger("all.log", level="debug")
log.logger.debug("debug")
log.logger.info("info")
log.logger.warning("warning")
log.logger.error("error")
log.logger.critical("critical")
Logger("error.log", level="error").logger.error("error")