feat: replcae logger

This commit is contained in:
colorfulgray0 2024-01-12 14:29:33 +08:00
parent 089498fcfa
commit 728150dc2f
4 changed files with 12 additions and 39 deletions

9
logger.py Normal file
View File

@ -0,0 +1,9 @@
import os
from loguru import logger
log_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "logs/DouyinLiveRecorder.log"
)
# 每天日志自动分文件
logger.add(log_path, rotation="12:00")

20
main.py
View File

@ -137,7 +137,6 @@ def display_info():
else:
start_display_time = now_time
except Exception as e:
print(f"错误信息:{e}\r\n发生错误的行数: {e.__traceback__.tb_lineno}")
logger.warning(f"错误信息: {e} 发生错误的行数: {e.__traceback__.tb_lineno}")
@ -684,12 +683,9 @@ def start_record(url_tuple, count_variable=-1):
if not os.path.exists(full_path):
os.makedirs(full_path)
except Exception as e:
print(f"路径错误信息: {e} 发生错误的行数: {e.__traceback__.tb_lineno}")
logger.warning(f"错误信息: {e} 发生错误的行数: {e.__traceback__.tb_lineno}")
if not os.path.exists(full_path):
print(
"保存路径不存在,不能生成录制.请避免把本程序放在c盘,桌面,下载文件夹,qq默认传输目录.请重新检查设置")
logger.warning(
"错误信息: 保存路径不存在,不能生成录制.请避免把本程序放在c盘,桌面,下载文件夹,qq默认传输目录.请重新检查设置")
@ -754,7 +750,6 @@ def start_record(url_tuple, count_variable=-1):
raise Exception('该直播无flv直播流请切换视频保存类型')
except Exception as e:
print(f"\r{time.strftime('%Y-%m-%d %H:%M:%S')} {anchor_name} 未开播")
logger.warning(f"错误信息: {e} 发生错误的行数: {e.__traceback__.tb_lineno}")
warning_count += 1
no_error = False
@ -800,8 +795,6 @@ def start_record(url_tuple, count_variable=-1):
_output = subprocess.check_output(ffmpeg_command, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
# logging.warning(str(e.output))
print(f"{e.output} 发生错误的行数: {e.__traceback__.tb_lineno}")
logger.warning(f"错误信息: {e} 发生错误的行数: {e.__traceback__.tb_lineno}")
warning_count += 1
no_error = False
@ -847,8 +840,6 @@ def start_record(url_tuple, count_variable=-1):
_output = subprocess.check_output(ffmpeg_command, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
# logging.warning(str(e.output))
print(f"{e.output} 发生错误的行数: {e.__traceback__.tb_lineno}")
logger.warning(f"错误信息: {e} 发生错误的行数: {e.__traceback__.tb_lineno}")
warning_count += 1
no_error = False
@ -871,8 +862,6 @@ def start_record(url_tuple, count_variable=-1):
if tsconvert_to_m4a:
threading.Thread(target=converts_m4a, args=(save_file_path,)).start()
except subprocess.CalledProcessError as e:
# logging.warning(str(e.output))
print(f"{e.output} 发生错误的行数: {e.__traceback__.tb_lineno}")
logger.warning(f"错误信息: {e} 发生错误的行数: {e.__traceback__.tb_lineno}")
warning_count += 1
no_error = False
@ -895,8 +884,6 @@ def start_record(url_tuple, count_variable=-1):
if tsconvert_to_m4a:
threading.Thread(target=converts_m4a, args=(save_file_path,)).start()
except subprocess.CalledProcessError as e:
# logging.warning(str(e.output))
print(f"{e.output} 发生错误的行数: {e.__traceback__.tb_lineno}")
logger.warning(f"错误信息: {e} 发生错误的行数: {e.__traceback__.tb_lineno}")
warning_count += 1
no_error = False
@ -933,7 +920,6 @@ def start_record(url_tuple, count_variable=-1):
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
logging.warning(str(e.output))
logger.warning(
f"错误信息: {e} 发生错误的行数: {e.__traceback__.tb_lineno}")
warning_count += 1
@ -970,8 +956,6 @@ def start_record(url_tuple, count_variable=-1):
threading.Thread(target=converts_m4a, args=(save_file_path,)).start()
except subprocess.CalledProcessError as e:
# logging.warning(str(e.output))
print(f"{e.output} 发生错误的行数: {e.__traceback__.tb_lineno}")
logger.warning(f"错误信息: {e} 发生错误的行数: {e.__traceback__.tb_lineno}")
warning_count += 1
no_error = False
@ -1003,7 +987,6 @@ def start_record(url_tuple, count_variable=-1):
dingtalk(dingtalk_api_url, content, dingtalk_phone_num)
except Exception as e:
print(f"错误信息:{e}\r\n读取的地址为: {record_url} 发生错误的行数: {e.__traceback__.tb_lineno}")
logger.warning(f"错误信息: {e} 发生错误的行数: {e.__traceback__.tb_lineno}")
warning_count += 1
@ -1037,9 +1020,7 @@ def start_record(url_tuple, count_variable=-1):
if loop_time:
print('\r检测直播间中...', end="")
except Exception as e:
print(f"错误信息:{e}\r\n发生错误的行数: {e.__traceback__.tb_lineno}")
logger.warning(f"错误信息: {e} 发生错误的行数: {e.__traceback__.tb_lineno}")
print(f"线程崩溃2秒后重试.错误信息: {e} 发生错误的行数: {e.__traceback__.tb_lineno}")
warning_count += 1
time.sleep(2)
@ -1314,7 +1295,6 @@ while True:
first_start = False
except Exception as e:
print(f"错误信息:{e}\r\n发生错误的行数: {e.__traceback__.tb_lineno}")
logger.warning(f"错误信息: {e} 发生错误的行数: {e.__traceback__.tb_lineno}")
if first_run:

View File

@ -1,2 +1,3 @@
requests
PyExecJS
PyExecJS
loguru==0.7.2

View File

@ -2,24 +2,8 @@
import functools
import hashlib
import logging
import os
import traceback
# --------------------------log日志-------------------------------------
# 创建一个logger
logger = logging.getLogger('record_logger')
logger.setLevel(logging.INFO)
# 创建一个handler用于写入日志文件
if not os.path.exists("./log"):
os.makedirs("./log")
fh = logging.FileHandler("./log/错误日志文件.log", encoding="utf-8-sig", mode="a")
fh.setLevel(logging.WARNING)
# 定义handler的输出格式
formatter = logging.Formatter('%(asctime)s - %(message)s')
fh.setFormatter(formatter)
# 给logger添加handler
logger.addHandler(fh)
from logger import logger
def trace_error_decorator(func):
@ -30,7 +14,6 @@ def trace_error_decorator(func):
except Exception as e:
error_line = traceback.extract_tb(e.__traceback__)[-1].lineno
error_info = f"错误信息: type: {type(e).__name__}, {str(e)} in function {func.__name__} at line: {error_line}"
print(error_info)
logger.warning(error_info)
return []