Python LoggerAdpater類

來源:互聯網
上載者:User

標籤:

Logger子類:

import logging

# create logger
module_logger = logging.getLogger(‘spam_application.auxiliary‘)

class Auxiliary:
def __init__(self):
self.logger = logging.getLogger(‘spam_application.auxiliary.Auxiliary‘)
self.logger.info(‘creating an instance of Auxiliary‘)
def do_something(self):
self.logger.info(‘doing something‘)
a = 1 + 1
self.logger.info(‘done doing something‘)

def some_function():
module_logger.info(‘received a call to "some_function"‘)

 

Main函數:

import logging
import auxiliary_module

# create logger with ‘spam_application‘
logger = logging.getLogger(‘spam_application‘)
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 - %(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 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()‘)

 

輸出到檔案spam.log

2015-06-30 20:08:37,335 - spam_application - INFO - creating an instance of auxiliary_module.Auxiliary
2015-06-30 20:08:37,335 - spam_application.auxiliary.Auxiliary - INFO - creating an instance of Auxiliary
2015-06-30 20:08:37,336 - spam_application - INFO - created an instance of auxiliary_module.Auxiliary
2015-06-30 20:08:37,336 - spam_application - INFO - calling auxiliary_module.Auxiliary.do_something
2015-06-30 20:08:37,336 - spam_application.auxiliary.Auxiliary - INFO - doing something
2015-06-30 20:08:37,337 - spam_application.auxiliary.Auxiliary - INFO - done doing something
2015-06-30 20:08:37,337 - spam_application - INFO - finished auxiliary_module.Auxiliary.do_something
2015-06-30 20:08:37,337 - spam_application - INFO - calling auxiliary_module.some_function()
2015-06-30 20:08:37,337 - spam_application.auxiliary - INFO - received a call to "some_function"
2015-06-30 20:08:37,338 - spam_application - INFO - done with auxiliary_module.some_function()

Python LoggerAdpater類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.