Python Loggeradpater Class

Source: Internet
Author: User

Logger sub-class:

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 function:

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 () ')

Output to File 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 Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.