python实现log日志的示例代码


本文摘自php中文网,作者不言,侵删。

下面为大家分享一篇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

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

# coding=utf-8

import logging

import os

import time

LEVELS={'debug':logging.DEBUG,\

  'info':logging.INFO,\

  'warning':logging.WARNING,\

  'error':logging.ERROR,\

  'critical':logging.CRITICAL,}

logger=logging.getLogger()

level='default'

def createFile(filename):

 path=filename[0:filename.rfind('/')]

 if not os.path.isdir(path):

  os.makedirs(path)

 if not os.path.isfile(filename):

#创建并打开一个新文件

  fd = open(filename,mode='w',encoding='utf-8')

  fd.close()

class MyLog:

 log_filename='E:/quality/it/pyrequest-master/log/itest.log'

 err_filename='E:/quality/it/pyrequest-master/log/err.log'

 dateformat='%Y-%m-%d %H:%M:%S'

 logger.setLevel(LEVELS.get(level,logging.NOTSET))

 createFile(log_filename)

 createFile(err_filename)

#注意文件内容写入时编码格式指定

 handler=logging.FileHandler(log_filename,encoding='utf-8')

 errhandler=logging.FileHandler(err_filename,encoding='utf-8')

 @staticmethod

 #静态方法

 def debug(log_message):

  setHandler('debug')

  logger.debug("[DEBUG "+getCurrentTime()+"]"+log_message)

  removerhandler('debug')

 @staticmethod

 def info(log_message):

  setHandler('info')

  logger.info("[INFO "+getCurrentTime()+"]"+log_message)

  removerhandler('info')

 @staticmethod

 def warning(log_message):

  setHandler('warning')

  logger.warning("[WARNING "+getCurrentTime()+"]"+log_message)

  removerhandler('warning')

 @staticmethod

 def error(log_message):

  setHandler('error')

  logger.error("[ERROR "+getCurrentTime()+"]"+log_message)

  removerhandler('error')

 @staticmethod

 def critical(log_message):

  setHandler('critical')

  logger.critical("[CRITICAL "+getCurrentTime()+"]"+log_message)

  removerhandler('critical')

# logger可以看做是一个记录日志的人,对于记录的每个日志,他需要有一套规则,比如记录的格式(formatter),

# 等级(level)等等,这个规则就是handler。使用logger.addHandler(handler)添加多个规则,

# 就可以让一个logger记录多个日志。

def setHandler(level):

 if level=='error':

  logger.addHandler(MyLog.errhandler)

 #handler=logging.FileHandler(log_filename)

 #把logger添加上handler

 logger.addHandler(MyLog.handler)

def removerhandler(level):

 if level=='error':

  logger.removeHandler(MyLog.errhandler)

 logger.removeHandler(MyLog.handler)

def getCurrentTime():

 return time.strftime(MyLog.dateformat,time.localtime(time.time()))

if __name__=="__main__":

 MyLog.debug("This is debug message")

 MyLog.info("This is info message")

 MyLog.warning("This is warning message")

 MyLog.error("This is error message")

 MyLog.critical("This is critical message")

相关推荐:

python 日志增量抓取实现方法

浅谈python日志的配置文件路径问题

以上就是python实现log日志的示例代码的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Pythonn如何访问本地html

Python3如何使用pil

Python如何判断一个字符串是否包含指定子字符串

Python代码缩进和测试模块示例详解

Python实现有序字典方法示例

Python如何设置曲线样式

Python正则表达式之中的findall函数是什么?

Python基础教程系列目录,最全的Python入门系列教程!

Python如何计算平方和

Python如何将字符串常量转化为变量?(附示例)

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...