Files
pig-house-controller/app/logs/logger.py
2025-10-17 10:32:52 +08:00

29 lines
830 B
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.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
一个简单的、可配置的日志记录器模块。
"""
import _thread
from app.config.config import *
# 创建一个锁用于在多线程环境中同步对print的调用
log_lock = _thread.allocate_lock()
def log(message: str):
"""
打印一条日志消息,是否实际输出取决于配置文件。
使用锁来确保多线程环境下的输出不会混乱。
Args:
message (str): 要打印的日志消息。
"""
# 从配置文件中获取调试开关的状态
# .get()方法可以安全地获取值如果键不存在则返回默认值False
if SYSTEM_PARAMS.get('debug_enabled', False):
with log_lock:
print(message)
# 如果开关为False此函数会立即返回不执行任何操作。