python的log使用

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
import logging
# create logger
logger_name = os.path.basename(__file__)
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG)
# create_file
log_path = os.path.basename(__file__)+".log"
fh = logging.FileHandler(log_path)
fh.setLevel(logging.DEBUG)
#打印到屏幕
sh = logging.StreamHandler()
sh.setLevel(logging.DEBUG)

# create formatter
fmt = "[%(asctime)-15s] [%(levelname)s] [%(filename)s - %(lineno)d %(process)d] %(message)s"
date_fmt = "%a %d %b %Y %H:%M:%S"
formatter = logging.Formatter(fmt, date_fmt)

# add handler and formatter to logger
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(sh)

logger.debug(sys.version)
logger.debug(os.getcwd())