feat: enhance logger functionality with initialization info and code formatting improvements

This commit is contained in:
cheng zhen
2025-01-10 22:29:21 +08:00
parent a9b0b4a045
commit fde9114b27

View File

@@ -11,9 +11,10 @@ logging.basicConfig(
filename=os.path.join(log_dir, f"{datetime.now().strftime('%Y-%m-%d')}.log"), filename=os.path.join(log_dir, f"{datetime.now().strftime('%Y-%m-%d')}.log"),
level=logging.DEBUG, level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s", format="%(asctime)s - %(levelname)s - %(message)s",
encoding='utf-8', encoding="utf-8",
) )
# 创建控制台处理器 # 创建控制台处理器
console_handler = logging.StreamHandler() console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO) console_handler.setLevel(logging.INFO)
@@ -22,6 +23,10 @@ console_handler.setFormatter(logging.Formatter("%(message)s"))
# 将控制台处理器添加到日志记录器 # 将控制台处理器添加到日志记录器
logging.getLogger().addHandler(console_handler) logging.getLogger().addHandler(console_handler)
# 打印日志目录所在路径
logging.info(f"Logger initialized, log directory: {os.path.abspath(log_dir)}")
def main_task(): def main_task():
""" """
Main task execution function. Simulates a workflow and handles errors. Main task execution function. Simulates a workflow and handles errors.
@@ -42,6 +47,7 @@ def main_task():
finally: finally:
logging.info("Task execution finished.") logging.info("Task execution finished.")
def some_condition(): def some_condition():
""" """
Simulates an error condition. Returns True to trigger an error. Simulates an error condition. Returns True to trigger an error.
@@ -49,6 +55,7 @@ def some_condition():
""" """
return True return True
if __name__ == "__main__": if __name__ == "__main__":
# Application workflow # Application workflow
logging.info("Application started.") logging.info("Application started.")