当前位置:网站首页>V01 - XX, record a good life from the log
V01 - XX, record a good life from the log
2022-07-26 12:47:00 【Ah, fat Penguin】
List of articles
V1.0 Automation basic module construction
Tools\vlog.py
Logging tools
This function is mainly for the files that need to be used during local operation
Python edition :3.10.5
Code checker 、 style :PEP 8、pylint
Editor :Vscode
Annotation copy : Here is a special reminder , I prefer uncommented code , therefore , A lot of times , Variable name 、 Method name 、 file name 、 Class names are comments , Of course, because the English level is not very good , What verb 、 Noun 、 Adjectives are easy to confuse , Just understand .
Usage mode
The way to use the log function is relatively simple
Vlog = Vlog().vlog.info(" What needs to be output ")The benefits of logging
In fact, for the tool log , Many people find it troublesome , Sometimes I feel more likely print More convenient , In fact, it's not in my eyes
I think log tool , Its role is not limited to the output and storage of logs , Here is a brief list of :
- It can be used as a comment to describe the code behavior
- Because it's part of the code , Not like comments , The code has changed , The notes have not been changed , This leads to a gap between comments and code logic
- You can see the time consumed from code execution to the end of the process , To put it another way , The performance of the code can be recorded
- Save the content , Historical reasons are the most difficult to trace , So the contents that can be recorded can assist you in archaeology
- wait ...
Of course , Finally, it depends on whether you need logs , At least I think the log is a base Tools , Still very real
Now I'll go straight to the code , Don't talk more
""" Logging tools :
This tool is mainly used in the process of running local files , Log output
According to your own needs , You can adjust the log level
"""
import os
import time
import logging
from logging import handlers
class Vlog():
"""_summary_ Log function , After instantiation , call "vlog" You can choose different print types
The current output format is : Time ( Accurate to milliseconds ) - file name < Row number > - Module name -> Log details
Use the format to take a chestnut : Vlog = Vlog().vlog.info(" What needs to be output ")
"""
DIR_LOGS = "_logs"
LOG_FOLDERS_NAME = "%Y_%m_%d"
LOG_FILES_NAME = "%Y_%m_%d-%H_%M_%S"
THIS_FILE_PATH = os.path.dirname(__file__)
PROJECT_FOLDER_PATH = os.path.dirname(THIS_FILE_PATH)
LOGS_FOLDER_PATH = os.sep.join([PROJECT_FOLDER_PATH, DIR_LOGS])
LEVEL_RELAIONS = {'debug': logging.DEBUG,
'info': logging.INFO,
'warning': logging.WARNING,
'error': logging.ERROR,
'critical': logging.CRITICAL}
def __init__(self, level: str = 'info') -> None:
name_for_per_min_log_files = time.strftime(self.LOG_FILES_NAME, time.localtime())
name_for_per_day_log_folders = name_for_per_min_log_files.split("-")[0]
per_day_log_folders_path = os.sep.join([self.LOGS_FOLDER_PATH,
name_for_per_day_log_folders])
if not os.path.exists(per_day_log_folders_path):
os.makedirs(per_day_log_folders_path)
else:
pass
per_min_logs_save_name = os.sep.join([per_day_log_folders_path,
f"{ name_for_per_min_log_files }.log"])
self.vlog = logging.getLogger(per_min_logs_save_name)
formatter = "[%(levelname)s] - [%(asctime)s] - [%(filename)s <%(lineno)d>] -> %(message)s"
set_formatter_to_logging = logging.Formatter(formatter)
self.vlog.setLevel(self.LEVEL_RELAIONS.get(level))
terminal_visitor = logging.StreamHandler()
terminal_visitor.setFormatter(set_formatter_to_logging)
self.vlog.addHandler(terminal_visitor)
log_saver = handlers.RotatingFileHandler(filename=per_min_logs_save_name,
mode="a", maxBytes=1024*1000,
backupCount=5, encoding='utf-8')
log_saver.setFormatter(set_formatter_to_logging)
self.vlog.addHandler(log_saver)
if __name__ == '__main__':
vlog = Vlog().vlog
vlog.debug(" This is a vlog Module test content 1")
vlog.info(" This is a vlog Module test content 2")
vlog.warning(" This is a vlog Module test content 3")
vlog.error(" This is a vlog Module test content 4")
vlog.critical(" This is a vlog Module test content 5")
边栏推荐
- Where is safe to open an account when buying stocks on mobile phones?
- 数据查询函数
- LCD笔记(7)LCD驱动程序框架_配置时钟
- Transactional transaction propagation behavior?
- Sim900a based on STM32 sends short messages in Chinese and English
- The.Net webapi uses groupname to group controllers to render the swagger UI
- Analysis of Wireshark data package of network security B module of national vocational college skills competition Wireshark 0051.pcap
- Sword finger offer 24. reverse the linked list
- 代码报错解决问题经验之二:YOLOv5中的test报错
- HTAP comes at a price
猜你喜欢

.NET WebAPI 使用 GroupName 对 Controller 分组呈现 Swagger UI

How do children's playgrounds operate?

LCD笔记(4)分析内核自带的LCD驱动程序

扫雷小游戏——轻松玩上瘾(C语言版)

全国职业院校技能大赛网络安全B模块wirshark数据包分析 wireshark0051.pcap

The map function counts the number of occurrences of characters

Food safety | is self-made food purchased online healthy food? Don't fall into these misunderstandings

Map函数统计字符出现的次数

Interview JD T5, was pressed on the ground friction, who knows what I experienced?

Various extensions of hcip-9.ospf
随机推荐
Can the Windows version of Oracle be used in Linux?
LCD notes (7) LCD driver framework_ Configure clock
How much do you know about the two infrastructures of the badminton stadium?
Kuzaobao: summary of Web3 encryption industry news on July 25
Redis主从复制原理
LCD笔记(7)LCD驱动程序框架_配置时钟
LCD notes (4) analyze the LCD driver of the kernel
The database consists of stored procedures and functions
Ds-24c/dc220v time relay
HTAP comes at a price
2、 Container_
Implementation of dynamic and static libraries (packaging dynamic and static libraries for others to use)
Beauty salon management system unified management system?
实体映射最强工具类:MapStruct真香
Transactional transaction propagation behavior?
[wechat applet] read the article, data request
可移动表空间
Food safety | are sterile eggs really completely sterile?
笔记。。。。
Kubernetes----高级存储之PV和PVC简介