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