当前位置:网站首页>Packaging of logging logs
Packaging of logging logs
2022-07-27 06:38:00 【Leoon123】
1、looging Four major organizations of
Loggers Logger entrance
processor Handler practitioners , Determine the output of logs at different ends , Log files . Console
format Formatter Log entries
filter Filter Enter the log of interest
2、 Encapsulation of log collector , And steps
The creation process of console log and file log
First step : Create a logger
logger=logging.getLogger("logger")
The second step : Set the output level of the log
logger.setLevel(logging.INFO)
The third step : Set the output of log format
%(asctime)s Current time in string form . The default format is “2003-07-08 16:49:45,896”. After the comma is milliseconds
%(levelname)s Log level in text form
%(message)s User output message
'''
formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s","%Y/%m/%d-%H:%M:%S")
The third step : Add the controller to the log object
# Create console processor -- Output console
SH=logging.StreamHandler()
# Console
logger.addHandler(SH)
SH.setFormatter(formatter)
# Create file processor The output file
# The file output is one step more than the console , create a file
FH=logging.FileHandler(f'{time.strftime("%Y-%m-%d %H-%M-%S", time.localtime())}_log.txt')
logger.addHandler(FH)
FH.setFormatter(formatter)
The overall code
import logging
import time
# Create a logger
logger=logging.getLogger("logger")
# Log output and current level , Global role
logger.setLevel(logging.INFO)
# Action console , The level of logging
# SH.setLevel()
# format
# What does the log contain Time , file , The level of logging , What did you do
# %(asctime)s Current time in string form . The default format is “2003-07-08 16:49:45,896”. After the comma is milliseconds
# %(levelname)s Log level in text form
# %(message)s User output message
formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s","%Y/%m/%d-%H:%M:%S")
# Create console processor -- Output console
SH=logging.StreamHandler()
# Console
logger.addHandler(SH)
SH.setFormatter(formatter)
# Create file processor The output file
# The file output is one step more than the console , create a file
FH=logging.FileHandler(f'{time.strftime("%Y-%m-%d %H-%M-%S", time.localtime())}_log.txt')
logger.addHandler(FH)
FH.setFormatter(formatter)
try:
score=int(input(" Please enter your score :"))
if score>60:
print(" You passed ")
else:
print(" unfortunately , You failed ")
logger.info(" The query is successful !!!")
except Exception as error:
logger.error(" Wrong information :"+str(error))
Use
logger.info(" End of query !!!!!!!!!")
simplify
self.logger=logging.getLogger(filename)
# Console log
self.console_handler=logging.StreamHandler()
# self.logger.setLevel(level)
# Format the log
self.console_handler.setFormatter(logging.Formatter("[%(asctime)s]%(filename)s->%(funcName)s line:%(lineno)d [%(levelname)s] %(message)s"))
# Add the log to the log object
self.logger.addHandler(self.console_handler)

边栏推荐
猜你喜欢

If conditional statement of shell

PXE高效批量网络装机

远程访问及控制

Function call of shell script

Geonode GeoServer win10 installation tutorial (personal test)

Briefly remember the top ten orders

Random points in non overlapping rectangle (force deduction daily question)

Introduction to JMeter

Robot navigation implementation

Basic operation of database on terminal
随机推荐
Li Kou daily question (linked list simulation)
Unit integration (grounding) test
DNS fault analysis optimization
Compatibility test knowledge points
IP core summary
Detailed explanation of thread safety problems
Programming learning record -- recursively solving the tower of Hanoi problem
TF coordinate transformation
关于testng相关标签的使用
单元集成(接⼝)测试
Markdown文档常用字体及颜色设置
Basic knowledge of English: modifying sentence elements - Part 1
shell--循环语句(for、while、until)
Shell语句判断练习题
Index and transaction of database (emphasis)
Basic knowledge of English: Rules for the use of non predicates Part 1
ArcGIS for JS API (2) get the ID set of element services
2021-06-26
Linu性能调优:面对DDOS攻击,我们如何缓解局面?
C language - program compilation