日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python logging之multi-module

發(fā)布時間:2023/12/15 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python logging之multi-module 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在同一個Python的解釋器進程里面,所有的對logging.getLogger(‘someLogger’)的調用都會返回同一個對象.這個規(guī)則不僅僅在同一個module有效,而且對在在同一個Python的解釋器進程里面的多個module也有效.

而且,應用代碼可以在一個module里面定義一個父logger,而在另一個module里面繼承這個logger,所有對這個子logger的調用都會轉到父logger里面去。


下面例子, 這個是主模塊python_log_multimodule1.py的代碼:

#coding=utf-8 import loggingimport python_log_multimodule2 from python_log_multimodule2 import SubModule# create logger with "sp_app" 創(chuàng)建 logger = logging.getLogger("sp_app") logger.setLevel(logging.DEBUG)# create file handler which logs even debug messages fh = logging.FileHandler("spam.log") fh.setLevel(logging.DEBUG)# create console handler with a higher log level ch = logging.StreamHandler() ch.setLevel(logging.ERROR)# create formatter and add it to the handlers formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(thread)d - %(message)s") fh.setFormatter(formatter) ch.setFormatter(formatter)# add the handlers to the logger logger.addHandler(fh) logger.addHandler(ch) logger.info("creating an instance of subModule.SubModule") a = SubModule()logger.info("created an instance of subModule.SubModule") logger.info("calling subModule.SubModule.do_something") a.do_something()logger.info("finished subModule.SubModule.do_something") logger.info("calling subModule.some_function()") python_log_multimodule2.some_function()logger.info("done with subModule.some_function()")

這個是子模塊的代碼,

#coding=utf-8import time import logging # create logger module_logger = logging.getLogger("sp_app.subModule") class SubModule: def __init__(self): self.logger = logging.getLogger("sp_app.subModule.SubModule")self.logger.info("creating an instance of subModule")def do_something(self): self.logger.info("start doing-something.") a = 1 + 1 self.logger.info("end doing-something.") def doWork(self):self.logger.info("start working.")time.sleep(0.1)self.logger.info("end working.") def some_function():module_logger.info("received a call to \"some_function\" ")

可以看到, 我們在主模塊里面定義了一個logger ‘sp_app’, 并對他進行了配置.那么在這個解釋器進程里面的任何地方去通過getLogger(‘sp_app’)得到的對象都是一樣的, 不需要從新定義配置, 可以直接使用.

更方便的是, 你定義任意該logger的子logger, 都可以共享父logger的定義和配置.所謂的父子logger只是簡單的通過命名來識別, 任意以’sp_app.’開頭的logger都是他的子logger, 例如’sp_app.subModule’

這個在實際的開發(fā)中, 還是很方便的, 對于一個application,首先通過logging配置文件編寫好這個application所對應的log策略, 可以只生成一個根logger, 比如叫’AppName’,然后在Main函數里面, 通過fileConfig加載logging的配置.接著在appliction的任意地方, 不同的模塊中, 可以使用AppName的子logger, 如AppName.Util, AppName.Core, 來進行l(wèi)og, 并且不需要反復的定義和配置各個logger.

查看日志記錄:

2018-02-27 14:10:49,896 - sp_app - INFO - 140735520973632 - creating an instance of subModule.SubModule 2018-02-27 14:10:49,896 - sp_app.subModule.SubModule - INFO - 140735520973632 - creating an instance of subModule 2018-02-27 14:10:49,897 - sp_app - INFO - 140735520973632 - created an instance of subModule.SubModule 2018-02-27 14:10:49,897 - sp_app - INFO - 140735520973632 - calling subModule.SubModule.do_something 2018-02-27 14:10:49,897 - sp_app.subModule.SubModule - INFO - 140735520973632 - start doing-something. 2018-02-27 14:10:49,897 - sp_app.subModule.SubModule - INFO - 140735520973632 - end doing-something. 2018-02-27 14:10:49,897 - sp_app - INFO - 140735520973632 - finished subModule.SubModule.do_something 2018-02-27 14:10:49,897 - sp_app - INFO - 140735520973632 - calling subModule.some_function() 2018-02-27 14:10:49,897 - sp_app.subModule - INFO - 140735520973632 - received a call to "some_function" 2018-02-27 14:10:49,897 - sp_app - INFO - 140735520973632 - done with subModule.some_function()

總結

以上是生活随笔為你收集整理的python logging之multi-module的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。