Kecenderungan konfigurasi
Hanya output ke fail
Putar mengikut masa, lalai 7d
Format log: hanya rekod maklumat yang diperlukan
import logging from logging.handlers import TimedRotatingFileHandler from os import path def init_log_config( filename: str, *, default_dir="/var/log", logformat: str = ("[%(levelname)s %(asctime)s %(process)d_%(threadName)s %(filename)s:%(lineno)s:%(funcName)s] " "%(message)s"), loglevel: str = "warn", backup_count: int = 7, encoding="utf-8", delay=True, ): """ :param filename: 文件名 :param default_dir: :param logformat: :param loglevel: :param backup_count: :param encoding: :param delay: :return: """ levelint = { "debug": logging.DEBUG, "info": logging.INFO, "warn": logging.WARN, "error": logging.ERROR }[loglevel] if not filename.startswith("/"): filename = path.join(default_dir, filename) logging.basicConfig( format=logformat, level=levelint, handlers=[ TimedRotatingFileHandler( filename, when="d", backupCount=backup_count, encoding=encoding, delay=delay, ) ] )
Pada awal program Mulakan permulaan (saya biasanya meletakkannya pada baris pertama)
init_log_config("log1.log", loglevel="debug") import os, sys
Digunakan dalam perniagaan
logging.info("Start Do something")
Atas ialah kandungan terperinci Bagaimana untuk mengkonfigurasi log Python. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!