Skip to content
Python

日志记录

配置和使用 logging 模块。

#logging#logging

Code

python
import logging

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
    handlers=[
        logging.FileHandler("app.log", encoding="utf-8"),
        logging.StreamHandler()
    ]
)
logger = logging.getLogger(__name__)

logger.debug("Debug info")
logger.info("Info")
logger.warning("Warning")
logger.error("error")
logger.exception("Exception (with stacktrace)")  # Only used in except