logging
- 日志分5种级别
import logging logging .basicConfig( level= logging .DEBUG, filename= "logger.log", filemode= "w", format= "%(asctime)s,%(lineno)d,%(filename)s %(message)s" #asctime表示时间lineno表示在哪行写的日志 ) logging.debug("1") logging .info("2") logging .warning("3") logging.error("4") logging .critical("5")
-
logger = logging .getLogger() ()内加的是子对象,不能重复,否则会覆盖。如果创建一个子对象还创建一个根对象,子对象会多打印一次 fh = logging .FileHandler ("test_log") ch = logging .StreamHandler () fm = logging .Formatter ("%(asctime)s %(message)s") fh.setFormatter(fm) ch.setFormatter(fm) logger .addHandler(fh) logger .addHandler(ch) logger .setLevel("DEBUG") logging.debug("1") logging .info("2") logging .warning("3") logging.error("4") logging .critical("5") #屏幕和文件同时显示