Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python日志模块始终使用相同的文件_Python_Logging - Fatal编程技术网

Python日志模块始终使用相同的文件

Python日志模块始终使用相同的文件,python,logging,Python,Logging,我有两个日志类,可以在两个不同的文件上打印日志 头等舱 import logging class firstLog: def __init__(self): self.logger = logging.getLogger("my_logger1") logging.basicConfig(format="%(asctime)s.%(msecs)06d:\n%(message)s\n", level=log

我有两个日志类,可以在两个不同的文件上打印日志

头等舱

import logging

class firstLog:
    
    def __init__(self):
        self.logger = logging.getLogger("my_logger1")
        logging.basicConfig(format="%(asctime)s.%(msecs)06d:\n%(message)s\n", level=logging.DEBUG,
                            datefmt="%d/%m/%Y %H:%M:%S",
                            filename="filename1.log")

    def printlog(self, msg, *args, **kwargs):
        self.logger.debug(msg, *args, **kwargs)
import logging

class secondLog:
    
    def __init__(self):
        self.logger = logging.getLogger("my_logger2")
        logging.basicConfig(format="%(asctime)s.%(msecs)06d:\n%(message)s\n", level=logging.DEBUG,
                            datefmt="%d/%m/%Y %H:%M:%S",
                            filename="filename2.log")

    def printlog(self, msg, *args, **kwargs):
        self.logger.debug(msg, *args, **kwargs)
二等舱

import logging

class firstLog:
    
    def __init__(self):
        self.logger = logging.getLogger("my_logger1")
        logging.basicConfig(format="%(asctime)s.%(msecs)06d:\n%(message)s\n", level=logging.DEBUG,
                            datefmt="%d/%m/%Y %H:%M:%S",
                            filename="filename1.log")

    def printlog(self, msg, *args, **kwargs):
        self.logger.debug(msg, *args, **kwargs)
import logging

class secondLog:
    
    def __init__(self):
        self.logger = logging.getLogger("my_logger2")
        logging.basicConfig(format="%(asctime)s.%(msecs)06d:\n%(message)s\n", level=logging.DEBUG,
                            datefmt="%d/%m/%Y %H:%M:%S",
                            filename="filename2.log")

    def printlog(self, msg, *args, **kwargs):
        self.logger.debug(msg, *args, **kwargs)
我实例化了这两个类,但只编写了filename1.log

log1 = firstLog()
log2 = secondLog()

log1.printlog("text1")
log2.printlog("text2")
我得到的是创建了一个名为filename1.log的新文件,其中包含

04/06/2021 08:57:04.000988:
text1
04/06/2021 08:57:04.000990:
text2
但是我需要两个独立的文件

日志记录。basicConfig()
将在应用程序启动时运行一次,并将配置根日志记录程序

如果需要将其他两个记录器的输出转到其他位置,则需要手动配置它们(或者在根记录器上配置“路由”处理程序)。

logging.basicConfig()
仅应用一次

如果已经配置了
logging
basicConfig()
将不会执行任何操作,除非设置了
force
。()


在您的情况下,应该使用
logging.getLogger(name)
和不同的名称来分别设置记录器。然后手动配置记录器。

我如何手动配置它们?您应该查看。它解释了很多关于日志记录包的概念以及如何配置它。您还将看到许多开发人员使用
getLogger(\uuu name\uuuu)
而不是手动创建记录器的方式。非常感谢!