Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to operate logging logs in python

Shulou Source: shulou.com Published: 2022-06-02 08:18:10 09月19日 Update

Today, I will talk to you about how to operate the logging log in python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Create a new python file named py3_adv_logging.py, and write the operation code in this file:

Advanced use of import logging#logging module # Loggers, Handlers Formatters# sets the logger name of the log logger = logging.getLogger ('Employee') # sets the log level logger.setLevel (logging.INFO) # defines the log formatting output formatter = logging.Formatter ('% (levelname) name) slug% (message) s') # specifies the log processor of the file file_handler = logging.FileHandler ('employee.log') # sets the log formatting file_handler.setFormatter (formatter) for the log processor # add file processor logger.addHandler (file_handler)

# define class class Employee: "A sample Employee class"

Def _ init__ (self, first, last): self.first = first self.last = last

Logger.info ('Created Employee: {}-{}' .format (self.fullname, self.email))

@ property def email (self): return'{}. @ email.com'.format (self.first, self.last)

@ property def fullname (self): return'{} {} '.format (self.first, self.last)

# when we create an object # will automatically record the log information in the # employee.log file emp_1 = Employee ('John',' Smith') emp_2 = Employee ('Corey',' Schafer') emp_3 = Employee ('Jane',' Doe')

The result of running is to generate a file employee.log with the contents of the file:

After reading the above, do you have any further understanding of how to operate logging logs in python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Log file content processor processing format advanced code information name object time more module knowledge article level result industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei OPPO Reno Microsoft Redmi MariaDB