foyoux / aligo

🔥简单、易用、可扩展的阿里云盘 API 接口库🚀
GNU General Public License v3.0
867 stars 158 forks source link

【需求】屏蔽控制台输出,并将运行日志写到文件 #139

Closed tensor-tech closed 1 year ago

tensor-tech commented 1 year ago

尝试过的代码无效:

        logger = logging.getLogger('aligo')

        formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')

        fh = logging.FileHandler(os.path.join(str(pathlib.Path.home()), ".aligo", "aligo.log"),
                                 encoding='utf-8')
        fh.setFormatter(formatter)
        fh.setLevel(level=logging.DEBUG)
        logger.addHandler(fh)

        ali = Aligo(level=logging.ERROR)
tensor-tech commented 1 year ago
    logger = logging.getLogger("aligo")
    logPath = os.path.join(str(pathlib.Path.home()), ".aligo", "aligo-gui.log")
    logger.addHandler(logging.FileHandler(logPath,encoding="utf-8"))
    logger.setLevel(logging.INFO)
    ali = Aligo(level=logging.ERROR)

问题解决了,setLevel 应该是logger.setLevel

yleafcc commented 1 year ago
    logger = logging.getLogger("aligo")
    logPath = os.path.join(str(pathlib.Path.home()), ".aligo", "aligo-gui.log")
    logger.addHandler(logging.FileHandler(logPath,encoding="utf-8"))
    logger.setLevel(logging.INFO)
    ali = Aligo(level=logging.ERROR)

问题解决了,setLevel 应该是logger.setLevel

我按这个设置, 终端和文件的日志级别没办法分开呢?

lemisky commented 1 year ago

addHandler

yleafcc commented 1 year ago

addHandler 我是如下做的,我当前遇到的问题是 我在实例化Aligo时用的ERROR级别,这个文件日志用的DEBUG级别,但是 实际的日志文件里面,没有日志记录(因为我程序没有发生错误),如果实例化时是INFO级别,文件日志也只INFO日志,没有DEBUG日志。 不能分不同的级别记录吗?


log_date = datetime.now().strftime("%m-%d")
log_path = Path(".", "Log", log_date + ".log")
log_hand = logging.FileHandler(log_path, "a", encoding="UTF-8")
log_hand.setFormatter(logging.Formatter("%(asctime)s %(filename)12s :%(lineno)3d - %(levelname)5s: %(message)s"))
log_hand.setLevel(logging.DEBUG)
    logger = logging.getLogger("aligo")
    logger.addHandler(log_hand)
lemisky commented 1 year ago

log handler 里面有个 filter 的东西,可以达到你的目的