/ mcp-scan / utils / loging.py
loging.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 sys
20  import time
21  
22  from loguru import logger
23  
24  logger.remove()
25  # 1. 添加控制台输出 (Console)
26  logger.add(
27      sys.stderr,
28      level="INFO",
29      format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{message}</cyan>",
30  )
31  
32  # 2. 添加文件输出 (File)
33  logger.add(
34      f"./logs/mcp-scan_{time.strftime('%Y-%m-%d-%H-%M-%S')}.log",
35      rotation="10 MB",
36      retention="10 days",
37      level="DEBUG",
38      format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {message}",
39      mode="w",  # 每次运行时覆盖旧日志
40  )
41  if __name__ == "__main__":
42      # 设置日志级别
43      # 输出日志
44      logger.error("Hello, world!")
45      logger.warning("Hello, world!")
46      logger.success("Hello, world!")
47      logger.info("Hello, world!")
48      logger.debug("Hello, world!")