发现python里面扩展了日志打印功能。感觉比java自带的还好,和log4j很类似。
下面总结下其用法。先说一种不用配置文件的:
<log.py>
1
# -*- coding: gb2312 -*-
2
import logging
3
4
logging.basicConfig(level=logging.INFO,
5
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
6
datefmt='%m-%d %H:%M',
7
filename='./AutoUpdate.log',
8
filemode='w')
9
10
console = logging.StreamHandler()
11
console.setLevel(logging.INFO)
12
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
13
console.setFormatter(formatter)
14
logging.getLogger('').addHandler(console)
15
16
17
##
18
# console = logging.StreamHandler()
19
# console = setLevel(logging.DEBUG)
20
# formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
21
# console.setFormatter(formatter)
22
# logging.getLogger('').addHandler(console)
23
24
25
def getLogging(name):
26
return logging.getLogger(name)

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

<test.py>






用过log4j的人不会陌生这种用法。如果你对log4j有疑惑,或者想了解请在本人blog内查找关于log4j的文章。有详细介绍。这里不再赘述。
第二种使用配置文件的:



































<test2.py>
















