Python日志打印双重到控制台

时间:2018-06-18 07:53:51

标签: python-3.x logging

似乎在几台机器上我得到了双倍输出:

[root@master docker]# oc describe pods pod3 -n demo
Name:         pod3
Namespace:    demo
Node:         <none>
Labels:       <none>
Annotations:  openshift.io/scc=anyuid
Status:       Pending
IP:           
Containers:
  hello:
    Image:        hello
    Port:         <none>
    Environment:  <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-87b8b (ro)
Conditions:
  Type           Status
  PodScheduled   False 
Volumes:
  default-token-87b8b:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-87b8b
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  node-role.kubernetes.io/compute=true
Tolerations:     <none>
Events:
  Type     Reason            Age                 From               Message
  ----     ------            ----                ----               -------
  Warning  FailedScheduling  1m (x141 over 41m)  default-scheduler  0/2 nodes are available: 2 MatchNodeSelector.

这是我使用的功能:

INFO LED NOTIFICATION STARTED
INFO:output_logger:LED NOTIFICATION STARTED

这就是我创建记录器的方式:

def setup_logger(name, log_file, level=logging.INFO, ContentFormat='%(asctime)s %(levelname)s %(message)s', DateTimeFormat="%Y-%m-%d %H:%M:%S", CreateConsoleLogger=False):
    """Function setup as many loggers as you want"""

    logger = logging.getLogger(name)
    logger.setLevel(level)

    if CreateConsoleLogger:
        # create console handler
        handler = logging.StreamHandler()
        handler.setLevel(level)
        formatter = logging.Formatter("%(levelname)s %(message)s")
        handler.setFormatter(formatter)
        logger.addHandler(handler)

    # create a file handler
    handler = RotatingFileHandler(log_file, maxBytes=2000000, backupCount=5)
    handler.setLevel(level)
    formatter = logging.Formatter(ContentFormat, DateTimeFormat)
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    return logger

这就是我所说的:

output_logger = setup_logger('output_logger', 'log/autofy.log', level=logging.DEBUG, CreateConsoleLogger=True)

在大多数计算机上,我只看到打印到控制台的相同消息按照预期output_logger.info("LED NOTIFICATION STARTED") 保存到文件中,但在其他计算机上它执行此奇怪的双输出操作。从一台计算机到另一台计算机,我的代码完全相同,所以任何想法可能会导致某些计算机而不是其他计算机出现这种情况?

修改

我正在使用notepad ++编写脚本并在Ubuntu 16.04计算机的终端窗口中运行它。我使用的是python3。

0 个答案:

没有答案