logging 提供了标准的日志接口,可以通过它来存储各种格式的日志;
logging 优势: 可以控制消息的级别(debug, info, warning, error, critial),过滤掉那些不重要的信息;通过赋予logger或handle不同的级别,保存不同的日志信息;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import logging
logging.basicConfig(filename='./segnet.log', filemode='a', level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# 避免缓存中有logger,重复输出
logger.handlers.clear()
logger.setLevel(level = logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
console.setFormatter(formatter)
logger.addHandler(console)
logger.info('How should i do?')
modular logging
./utils/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
import logging
class TextLogger():
def __init__(self, name= None, filename= None, filemode='a', level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'):
super(TextLogger, self).__init__()
self.name = name
self.filename = filename
self.filemode = filemode
self.level = level
self.format = format
def get_logger(self):
logging.basicConfig(filename=self.filename, filemode=self.filemode, level=self.level, format=self.format)
logger = logging.getLogger(self.name)
# 避免缓存中有logger,重复输出
logger.handlers.clear()
logger.setLevel(level = self.level)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console = logging.StreamHandler()
console.setLevel(logging.INFO)
console.setFormatter(formatter)
logger.addHandler(console)
logger.info('How should i do?')
return logger
class Write():
def __init__(self, logger, **args):
super(Write, self).__init__()
self.logger = logger
self.args = args
def write(self):
for k, v in self.args.items():
self.logger.info(str(k) + ': ' + str(v))
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import os
import time
from utils.logger import TextLogger, Write
# ===> name: log名称, work_dir: log和weights的保存路径
name = 'Unet'
work_dir = './work_dirs/' + name
if not os.path.exists(work_dir):
os.mkdir(work_dir)
logger = TextLogger(name= name, filename= work_dir + '/' + time.asctime( time.localtime(time.time()) ) + '.log').get_logger()
...
Write(logger, name=name, work_dir=work_dir, inputs_size=inputs_size, num_classes=NUM_CLASSES,lr=lr, Epochs=Epoches, Batch_size=Batch_size).write()