2024-10-24 09:04:38 +08:00

138 lines
5.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding:utf-8 -*-
"""
@File : logger
@Author : xuxingchen
@Version : 1.0
@Contact : xuxingchen@sinochem.com
@Desc : None
"""
import time
DEBUG = None
LOGGER_PATH = None
def log(text: str, log_path: str = None):
"""打印日志"""
log_path = log_path if log_path else LOGGER_PATH
log_line = '[{}] {}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), text)
if log_path:
log = open(log_path, 'a', encoding='utf8')
log.write("{}\n".format(log_line))
log.close()
print(log_line)
def log_plus(text: str, log_path: str = None, prefix_text: str = None, suffix_text: str = None):
"""加强版打印日志,预置了不同文字颜色"""
log_path = log_path if log_path else LOGGER_PATH
if prefix_text:
log_line_start = f"[{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}] {prefix_text}"
else:
log_line_start = f"[{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}]"
if suffix_text:
log_line = f"{log_line_start} {text} {suffix_text}"
else:
log_line = f"{log_line_start} {text}"
if log_path:
with open(log_path, 'a', encoding='utf8') as log:
log.write("{}\n".format(log_line))
log.close()
print(log_line)
def new_dc(msgs: str | float | int, fore_color: str = "", back_color: str = "") -> str:
"""给文本上色
fore_color格式如下
[{显示方式};{前景色};{背景色}m
显示方式
0默认值、1高亮、22非粗体、4下划线、24非下划线、 5闪烁、25非闪烁、7反显、27非反显
前景色
30黑色、31红色、32绿色、 33黄色、34蓝色、35洋 红、36青色、37白色
背景色
40黑色、41红色、42绿色、 43黄色、44蓝色、45洋 红、46青色、47白色
如:
高亮绿色红色背景 [1;32;41m
默认-绿字体 [32m
"""
if fore_color == "":
fore_color = '[32m' # 默认绿色
return "\033{}{}{}\033[0m".format(back_color, fore_color, str(msgs))
def log_speed_ms(start_time: float, suffix: str = "", prefix: str = "", number_color: str = "", decimal: int = 4):
"""控制台打印消耗的毫秒时间"""
log(f"{suffix}用时:{new_dc(str(round((time.time() - start_time) * 1000, decimal)), number_color)}ms{prefix}")
def speed_ms(start_time: float, decimal: int = 4):
"""消耗的毫秒时间"""
return round((time.time() - start_time) * 1000, decimal)
class Logger:
"""对控制台日志输出的二次封装,对部分常用日志进行预置"""
@staticmethod
def debug(text: str, log_path: str = None):
"""预置的debug形式的log"""
log_path = log_path if log_path else LOGGER_PATH
if isinstance(DEBUG, bool) and DEBUG:
log_plus(text, log_path, f"[{new_dc('INFO-DEBUG', '[34m')}]")
@staticmethod
def info(text: str, log_path: str = None):
"""预置的info形式的log"""
log_path = log_path if log_path else LOGGER_PATH
log_plus(text, log_path, f"[{new_dc('INFO', '[34m')}]")
@staticmethod
def error(text: str, log_path: str = None):
"""预置的error形式的log"""
log_path = log_path if log_path else LOGGER_PATH
log_plus(text, log_path, f"[{new_dc('ERROR', '[1;31m')}]")
@staticmethod
def warn(text: str, log_path: str = None):
"""预置的warn形式的log"""
log_path = log_path if log_path else LOGGER_PATH
log_plus(text, log_path, f"[{new_dc('WARN', '[1;33m')}]")
@staticmethod
def init(text: str, log_path: str = None):
"""预置的error形式的log"""
log_path = log_path if log_path else LOGGER_PATH
log_plus(text, log_path, f"[{new_dc('INIT', '[35m')}]")
@staticmethod
def title(text: str, log_path: str = None):
"""预置title形式的显目log"""
log_path = log_path if log_path else LOGGER_PATH
log_plus(new_dc(text, '[1m'), log_path, "🚀", "🚀")
@staticmethod
def complete(text: str, log_path: str = None):
"""预置complete形式的显目log"""
log_path = log_path if log_path else LOGGER_PATH
log_plus(new_dc(text, '[1;32m'), log_path, "", "")
@staticmethod
def remove(text: str, log_path: str = None):
"""预置remove形式的显目log"""
log_path = log_path if log_path else LOGGER_PATH
log_plus(new_dc(text, '[1m'), log_path, "🚮", "🚮")
@staticmethod
def connect(text: str, log_path: str = None):
"""预置connect形式的显目log"""
log_path = log_path if log_path else LOGGER_PATH
log_plus(new_dc(text, '[1;32m'), log_path, "🔗", "🔗")
@staticmethod
def disconnect(text: str, log_path: str = None):
"""预置disconnect形式的显目log"""
log_path = log_path if log_path else LOGGER_PATH
log_plus(new_dc(text, '[1m'), log_path, "🚫", "🚫")