当前位置:网站首页>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")
边栏推荐
- 今日睡眠质量记录75分
- QT入门引导 及其 案例讲解
- 全国职业院校技能大赛网络安全B模块wirshark数据包分析 wireshark0051.pcap
- Backtracking - 131. Split palindrome string
- What is the Internet of things? The most comprehensive explanation of common IOT protocols
- 回溯——第51题. N皇后——必须攻克的经典回溯难题
- Microsoft has shut down two attack methods: Office macro and RDP brute force cracking
- Access数据库无法连接
- 【2243】module_ param.m
- The "2022 Huawei developer competition eastern China division opening ceremony" was successfully held in Fuzhou
猜你喜欢

【Map】万能的Map使用方法 & 模糊查询的两种方式

Food safety | are sterile eggs really completely sterile?

Hit the blackboard and draw the key points: a detailed explanation of seven common "distributed transactions"

Problems encountered in byte stream exercises and Solutions

Vs code set the method of ctrl+s saving and automatic formatting

食品安全 | 微波炉什么食品都能加热?这些安全隐患要知道

动静态库的实现(打包动静态库供他人使用)

Redis主从复制原理

STM32 drives hc05 Bluetooth serial port communication module

Emerging security providers to learn about in 2022
随机推荐
PXE原理与配置
The.Net webapi uses groupname to group controllers to render the swagger UI
Backtracking - 491. Incremental subsequence
编程式导航路由跳转到当前路由(参数不变), 多次执行会抛出NavigationDuplicated的警告错误?
Problems encountered in byte stream exercises and Solutions
Oracle AWR report script: SQL ordered by elapsed time
Data query function
Azure Synapse Analytics 性能优化指南(2)——使用具体化视图优化性能(上)
JDBC从连接池获取连接(Druid连接池)
How to optimize a large number of if else [email protected] Detailed explanation of valib
Database composition view
今日睡眠质量记录75分
uniapp h5、app引用外部在线js
Flutter's learning path
The "2022 Huawei developer competition eastern China division opening ceremony" was successfully held in Fuzhou
如何组装一个注册中心?
JDBC gets connections from the connection pool (Druid connection pool)
Industry case | how does the index help the sustainable development of Inclusive Finance in the banking industry
Microsoft has shut down two attack methods: Office macro and RDP brute force cracking
.eslintrc.js配置说明