For details about how to implement the Logger printing function in Python, pythonlogger

Source: Internet
Author: User

For details about how to implement the Logger printing function in Python, pythonlogger

Preface

It is well known that there is a logging kit specifically used for logger printing in Python, but the logger of this kit only receives the printing information of a string type logger. Therefore, we need to splice the information to be printed into a string in advance, which is not good for code purity.

On the basis of logging, I implemented a logger printing tool similar to Java logback, which is easy to implement and can meet some simple logger printing requirements, hoping to help you. Let's take a look at the details below:

LoggerFactory

This class is used to generate logger instances of other call classes and save these instances.

'''Created on the Logger factory in July 20, 2017, saving Logger instances of each class ''' from slient. bigdata. common. logger import Loggerimport loggingclass LoggerFactory: LOG_FILENAME = 'bigdata _ python. log' # TYPE = "CONSOLE" # logger Printing TYPE # TYPE = "FILE" LEVEL = logging. DEBUG # logger LEVEL # LEVEL = logging. INFO loggerDict ={}# external open Logger call method @ staticmethod def getLogger (className)-> Logger: if className in LoggerFactory. loggerDict. keys (): logger = LoggerFactory. loggerDict [className] if not logger: logger = LoggerFactory. _ initLogger (className) else: logger = LoggerFactory. _ initLogger (className) return logger # generate a Logger instance @ staticmethod def _ initLogger (className)-> Logger: logger = logging. getLogger (className) # Set the logger level to DEBUG logger. setLevel (LoggerFactory. LEVEL) # Set the Logger format formatter = logging. formatter ('[% (asctime) s] [% (name) s] [% (levelname) s]: % (message) s') if LoggerFactory. TYPE = 'console': # create StreamHandler handler = logging. streamHandler () else: # create the output log to the file Handler handler = logging. fileHandler (LoggerFactory. LOG_FILENAME) # Add the handler Format. setFormatter (formatter) # Add handler logger to logger. addHandler (handler) localLogger = Logger (logger) LoggerFactory. loggerDict [className] = localLogger return localLogger

Logger

This class encapsulates some methods of logging, which is relatively simple.

'''Created on July 5, 2017 log processing ''' import loggingimport stringclass Logger: def _ init _ (self, logger: logging): self. logger = logger def info (self, formatStr: string, * objs): self.logger.info (formatStr. format (* objs) def debug (self, formatStr: string, * objs): self. logger. debug (formatStr. format (* objs) def error (self, formatStr: string, * objs): self. logger. error (formatStr. format (* objs ))

Test

Logger = LoggerFactory. getLogger ("Test") logger.info ("Print log1 :{}, print log2 :{}", 666, "I am log2 ")

Test results:

[16:43:00, 821] [Test] [INFO]: Print log1: 666, print log2: I am log2

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

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.