python logger filter_python Logger 高级教程

高级教程

logging library 四个模块

lLoggers暴露的接口,给代码使用

Handlers  把log 记录送到合适的destination

Filter  过滤器,决定哪个log记录被输出

Formatters  ,最终输出的log记录格式

Logevent information 在loggers ,handlers ,filters 和formatters 中传递。

获得Logger:

我们通过Logger 类的实例来调用方法来记录日志。每个实例有不同的名字,并且被记录在namesapce里。

如果用于模块级别的日志记录,一个惯例是用以下模式来命名Logger:

ger=logging.getLogger(__name__)

可用来追踪包/模块 ,从logger名字直观上找到事件发生处。

Log的Destination:

日志记录可以被写入 files,HTTPGET/POST ,通过SMTP写入email,socket,或者系统级别日志,比如Window NT的事件日志。

Destinationsare served by handler classes. 如果内置的Handler 无法满足你的需求,可以定义自己的handlerClass。

下图是log 记录的传递过程

0818b9ca8b590ca3270a3433284dd417.png

Logger类

三个作用:

1.        给代码暴露接口

2.        基于filter Object决定展示哪些log messages 被展示

3.        Logger object 传递相关logmessages 给相关handler

Loggerl类有两类方法:

用来配置的方法

1.Logger.setLevel(lvl):说明会被处理的最低Level的log message

下面是每个logging level 对应的值

Level

Numeric value

CRITICAL

50

ERROR

40

WARNING

30

INFO

20

DEBUG

10

NOTSET

0

2.       Logger.addHandler(hdlr)¶和Logger.removeHandler(hdlr)

Logger 中增加或者删除Handler

3.Logger.addFilter(filt) 和Logger.removeFilter(filt )

在Logger中增加或者删除 Filter

2.记录message的方法:

Logger.debug(msg,*args,**kwargs)

Logger.info(msg, *args, **kwargs)¶

Logger.warning(msg, *args, **kwargs)

Logger.error(msg, *args, **kwargs)

Logger.critical(msg, *args, **kwargs)

Logger.exception(msg,*args,**kwargs)¶在Logger.error层级

Logger.log(lvl,msg,*args,**kwargs)

Handlers

负责将Log message 分派到指定的destination

Handler.setLevel(lvl)

Handler.setFormatter(form) 他使用的Formatter

标准库包括比如StreamHandler(像sys.stout咿呀将logging输出)和FileHandler(将logging 输出于硬盘文件中)

Formatters

负责配置log message 的最终顺序,结构,及内容

多模块记录

多次调用logging.getLogger('someLogger')会返回同一个logger object的引用。不仅在一个module里这样,多个module也是这样。除此之外还可以在一个Module里生成父模块,在另一Module生成子logger

main module:

importlogging

importauxiliary_module

# createlogger with 'spam_application'

logger=logging.getLogger('spam_application')

logger.setLevel(logging.DEBUG)

# create file handler whichlogs even debug messages

fh=logging.FileHandler('spam.log')

fh.setLevel(logging.DEBUG)

# create console handler with a higherloglevel

ch=logging.StreamHandler()

ch.setLevel(logging.ERROR)

# create formatter and add it to the handlers

formatter=logging.Formatter('%(asctime)s-%(name)s-%(levelname)s-%(message)s')

fh.setFormatter(formatter)

ch.setFormatter(formatter)

# add the handlers to thelogger

logger.addHandler(fh)

logger.addHandler(ch)

logger.info('creating an instance of auxiliary_module.Auxiliary')

a=auxiliary_module.Auxiliary()

logger.info('created an instance of auxiliary_module.Auxiliary')

logger.info('calling auxiliary_module.Auxiliary.do_something')

a.do_something()

logger.info('finished auxiliary_module.Auxiliary.do_something')

logger.info('calling auxiliary_module.some_function()')

auxiliary_module.some_function()

logger.info('done with auxiliary_module.some_function()')

auxiliary module:

importlogging

# createlogger

module_logger=logging.getLogger('spam_application.auxiliary')

classAuxiliary:

def__init__(self):

self.logger=logging.getLogger('spam_application.auxiliary.Auxiliary')

self.logger.info('creating an instance of Auxiliary')

defdo_something(self):

self.logger.info('doing something')

a =1+1

self.logger.info('done doing something')

defsome_function():

module_logger.info('received a call to "some_function"')

输出

The output looks like this:

2005-03-23 23:47:11,663 - spam_application - INFO -

creating an instance of auxiliary_module.Auxiliary

2005-03-23 23:47:11,665 - spam_application.auxiliary.Auxiliary - INFO -

creating an instance of Auxiliary

2005-03-23 23:47:11,665 - spam_application - INFO -

created an instance of auxiliary_module.Auxiliary

2005-03-23 23:47:11,668 - spam_application - INFO -

calling auxiliary_module.Auxiliary.do_something

2005-03-23 23:47:11,668 - spam_application.auxiliary.Auxiliary - INFO -

doing something

2005-03-23 23:47:11,669 - spam_application.auxiliary.Auxiliary - INFO -

done doing something

2005-03-23 23:47:11,670 - spam_application - INFO -

finished auxiliary_module.Auxiliary.do_something

2005-03-23 23:47:11,671 - spam_application - INFO -

calling auxiliary_module.some_function()

2005-03-23 23:47:11,672 - spam_application.auxiliary - INFO -

received a call to 'some_function'

2005-03-23 23:47:11,673 - spam_application - INFO -

done with auxiliary_module.some_function()

链接:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值