私は次のlogging
ようなPythonのモジュールを使用しています:
class InitLogging():
def LogModule(self):
self.Logger = logging.getLogger("_TCP_Logger")
self.Logger.LogLevel = logging.DEBUG
self.Logger.setLevel(self.Logger.LogLevel)
self.fh = logging.FileHandler("Log.txt")
self.fh.level = logging.DEBUG
self.formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)8s')
self.Logger.addHandler(self.fh)
self.Logger.log(logging.INFO,"=====================================")
self.Logger.log(logging.INFO,"Start Logging Module!!")
self.Logger.log(logging.INFO,"=====================================")
self.fh .setFormatter(self.formatter)
self.Logger.log(logging.INFO,"Enrty to Logging Module!!")
class LogMsg(InitLogging):
def Logit(self):
self.LogModule()
self.Logger.log(logging.DEBUG,'This is Debugging Message!!')
self.Logger.log(logging.INFO,"This is Info Message!!")
self.Logger.log(logging.WARNING,"This is WARNING Message!!")
LoggingInstance = LogMsg()
LoggingInstance.Logit()
コードを実行すると、以下の出力が得られますLog.txt
。
=====================================
Start Logging Module!!
=====================================
2013-03-20 17:51:35,135 INFO Enrty to Logging Module!!
2013-03-20 17:51:35,135 DEBUG This is Debugging Message!!
2013-03-20 17:51:35,135 INFO This is Info Message!
2013-03-20 17:51:35,135 WARNING This is WARNING Message!!
しかし、さまざまなログ レベルのメッセージをまとめて、以下のような出力を生成したいと考えています。
=====================================
Start Logging Module!!
=====================================
2013-03-20 17:51:35,135 INFO Enrty to Logging Module!!
2013-03-20 17:51:35,135 DEBUG This is Debugging Message!!
2013-03-20 17:51:35,135 INFO This is Info Message!
2013-03-20 17:51:35,135 WARNING This is WARNING Message!!
これどうやってするの?