Encapsulation of the Python log module

Source: Internet
Author: User

Background:

To log output to a file

To record a log file every day.


1) Package model_logger.py

#!/usr/bin/env # coding:utf-8 import logging import logging.handlersclass  Logger (logging. Logger):     def __init__ (Self, filename=none):         super (logger, self). __init__ (self)         #   Log file name         if filename is None:             filename =  ' My.Log '          self.filename = filename         #  create a handler for writing to log files   (Generate 1 daily, 10-day log)         FH  = logging.handlers.timedrotatingfilehandler (self.filename,  ' D ',  1, 10)          fh.suffix =  "%y%m%d-%h%m.log"          fh.setleVel (logging. DEBUG)          #  re-create a handler for output to the console           ch = logging. Streamhandler ()          ch.setlevel (logging. WARNING)          #  define the output format of the handler           formatter = logging. Formatter ('% (asctime) s - % (levelname) s - % (filename) s[line:% (Lineno) d] - % ( Message) s ')          fh.setformatter (formatter)           ch.setformatter (Formatter)           #  add Handler         self.addhandler (FH) to logger           self.addhandler (CH)  if __name__ ==  ' __main __ ':    &NBSp;pass 


2) How to use

From Model_logger Import Loggerlogger = Logger (' Client_logs.log ') if __name__ = = ' __main__ ': try:dostring () ex Cept Exception,e:logger.error (E)


Encapsulation of the Python log module

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.