0%

【Python】日志 logging

记录 logging 简单用法。

constants.py

1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env python
# coding=utf-8

import os
import platform

class FilePath:
HOME = os.environ['HOME'] if platform.system() == 'Linux' else os.environ['USERPROFILE']
CFG_DIR = f'{HOME}/.cfg'
LOG_PATH = f'{CFG_DIR}/log.txt'

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
#!/usr/bin/env python
# coding=utf-8

import os
import logging
from common.constants import FilePath

log = logging.getLogger(__name__)
formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] [%(filename)s] [%(threadName)s] %(message)s')
log.setLevel(logging.DEBUG)

def init_path():
for path_name, path in FilePath.__dict__.items():
if not path_name.endswith('DIR') or os.path.exists(path):
continue
os.system(f'mkdir -p {path}')

def init_log():
sh = logging.StreamHandler()
sh.setFormatter(formatter)
sh.setLevel(logging.DEBUG)
fh = logging.FileHandler(FilePath.LOG_PATH, 'w', encoding='utf-8')
fh.setFormatter(formatter)
fh.setLevel(logging.DEBUG)
log.addHandler(sh)
log.addHandler(fh)

init_path()
init_log()