logging.py
1 # Copyright (c) 2024-2026 Tencent Zhuque Lab. All rights reserved. 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # 15 # Requirement: Any integration or derivative work must explicitly attribute 16 # Tencent Zhuque Lab (https://github.com/Tencent/AI-Infra-Guard) in its 17 # documentation or user interface, as detailed in the NOTICE file. 18 19 import os.path 20 import time 21 import sys 22 from loguru import logger 23 24 from utils.config import base_dir 25 26 logger.remove() 27 # 1. 添加控制台输出 (Console) 28 logger.add( 29 sys.stderr, 30 level="DEBUG", 31 format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{line}</cyan> | <cyan>{message}</cyan>", 32 ) 33 34 # 2. 添加文件输出 (File) 35 logger.add( 36 os.path.join(base_dir, "logs", f"agent-scan_{time.strftime('%Y-%m-%d-%H-%M-%S')}.log"), 37 rotation="10 MB", 38 retention="10 days", 39 level="DEBUG", 40 format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {name}:{line} | {message}", 41 mode="w", # 每次运行时覆盖旧日志 42 ) 43 if __name__ == '__main__': 44 # 设置日志级别 45 # 输出日志 46 logger.error("Hello, world!") 47 logger.warning("Hello, world!") 48 logger.success("Hello, world!") 49 logger.info("Hello, world!") 50 logger.debug("Hello, world!")