Python日志模块
# 日志模块的应用
# 代码
# 日志模块
import os
import platform
import logging
# os.getenv()获取一个环境变量,如果没有返回none
# os.path.join(): 将多个路径组合后返回
if platform.platform().startswith('Windows'):
logging_file = os.path.join(os.getenv('HOMEDRIVE'),
os.getenv('HOMEPATH'),
'test.log')
else:
logging_file = os.path.join(os.getenv('HOME'),
'test.log')
print("Logging to", logging_file)
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s: %(levelname)s :%(message)s',
filename=logging_file,
filemode='w',
)
# 日志文件将存储在电脑上
logging.debug("Start of the program")
logging.info("Doing something")
logging.warning("Dying now")
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
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
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
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
# 运行结果
Logging to C:\Users\dylink\test.log
电脑查看该目录文件为
2018-07-23 20:03:55,441: DEBUG :Start of the program
2018-07-23 20:03:55,441: INFO :Doing something
2018-07-23 20:03:55,441: WARNING :Dying now
1
2
3
4
5
2
3
4
5
1
2
3
4
5
2
3
4
5
编辑 (opens new window)
上次更新: 2022/12/31, 16:52:27
- 01
- SpringCache基本配置类05-16
- 03
- Rpamis-security-原理解析12-13