写log的小代码

写日志的代码, 可以参考一下, 当然是最基本的用法。本人推荐使用异步的方式记录日志。 ```python import logging from concurrent.futures import ThreadPoolExecutor def log_message(message): logging.info(message) # 设置日志级别和输出格式 logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') # 使用多线程异步记录日志 with ThreadPoolExecutor(max_workers=4) as executor: for i in range(10): executor.submit(log_message, f'This is log message {i} from thread {threading.current_thread().name}') ```
mht 文件大小:496.67KB