22 lines
659 B
Python
22 lines
659 B
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
简单的日志初始化模块
|
|
使用:
|
|
from logging_setup import init_logging
|
|
init_logging()
|
|
"""
|
|
import logging
|
|
import os
|
|
|
|
def init_logging(level: str = None):
|
|
lvl = (level or os.getenv("LOG_LEVEL", "INFO")).upper()
|
|
lvl_value = getattr(logging, lvl, logging.INFO)
|
|
logging.basicConfig(
|
|
level=lvl_value,
|
|
format="%(asctime)s %(levelname)s [%(name)s] %(message)s",
|
|
datefmt="%Y-%m-%d %H:%M:%S",
|
|
)
|
|
# 降低第三方库的默认日志噪音
|
|
logging.getLogger("urllib3").setLevel(max(logging.WARNING, lvl_value))
|
|
logging.getLogger("requests").setLevel(max(logging.WARNING, lvl_value))
|