与Python pid软件包冲突的日志记录

问题描述 投票:1回答:1

我一直在使用pid开发python守护程序,并将初始日志直接记录到控制台后,我想使用python的logging模块切换到文件日志记录。这是我遇到问题的时候。

我具有start / stop函数来管理守护程序:

import os
import sys
import time
import signal
import lockfile
import logging
import logging.config

import daemon
from pid import PidFile

from mpmonitor.monitor import MempoolMonitor

# logging.config.fileConfig(fname="logging.conf", disable_existing_loggers=False)
# log = logging.getLogger("mpmonitor")


def start():
    print("Starting Mempool Monitor")

    _pid_file = PidFile(pidname="mpmonitor.pid", piddir=curr_dir)

    with daemon.DaemonContext(stdout=sys.stdout,
                              stderr=sys.stderr,
                              stdin=sys.stdin,
                              pidfile=_pid_file):

        # Start the monitor:
        mpmonitor = MempoolMonitor()
        mpmonitor.run()


def stop():
    print("\n{}\n".format(pid_file))

    try:
        with open(pid_file, "r") as f:
            content = f.read()
        f.close()

    except FileNotFoundError as fnf_err:
        print("WARNING - PID file not found, cannot stop daemon.\n({})".format(pid_file))
        sys.exit()


    print("Stopping Mempool Monitor")
    # log.info("Stopping Mempool Monitor")
    pid = int(content)
    os.kill(pid, signal.SIGTERM)

    sys.exit()

可以按照您期望的那样工作。 (请注意,日志记录代码已注释。)

取消对日志记录代码的注释会破坏所有内容,并且会发生一些随机的事情。错误消息(修剪后,完整的回溯“看起来像垃圾邮件”):

--- Logging error ---
OSError: [Errno 9] Bad file descriptor
Call stack:
  File "/home/leilerg/.local/lib/python3.6/site-packages/pid/__init__.py", line 77, in setup
    self.logger.debug("%r entering setup", self)
Message: '%r entering setup'
Arguments: (<pid.PidFile object at 0x7fc8faa479e8>,)

--- Logging error ---
OSError: [Errno 9] Bad file descriptor
Call stack:
  File "/home/leilerg/.local/lib/python3.6/site-packages/pid/__init__.py", line 170, in create
    self.logger.debug("%r create pidfile: %s", self, self.filename)
Message: '%r create pidfile: %s'
Arguments: (<pid.PidFile object at 0x7fc8faa479e8>, '/home/leilerg/python/mempoolmon/mpmonitor.pid')

Traceback (most recent call last):
  File "/home/leilerg/.local/lib/python3.6/site-packages/pid/__init__.py", line 136, in inner_check
    pid = int(pid_str)
ValueError: invalid literal for int() with base 10: 'DEBUG - 2020-04-'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/leilerg/.local/lib/python3.6/site-packages/pid/__init__.py", line 139, in inner_check
    raise PidFileUnreadableError(exc)
pid.PidFileUnreadableError: invalid literal for int() with base 10: 'DEBUG - 2020-04-'

--- Logging error ---
Traceback (most recent call last):
OSError: [Errno 9] Bad file descriptor
Call stack:
  File "/home/leilerg/.local/lib/python3.6/site-packages/pid/__init__.py", line 197, in close
    self.logger.debug("%r closing pidfile: %s", self, self.filename)
Message: '%r closing pidfile: %s'
Arguments: (<pid.PidFile object at 0x7fc8faa479e8>, '/home/leilerg/python/mempoolmon/mpmonitor.pid')

我指的是随机的东西,现在文件mpmonitor.pid不再包含PID,而是一些尝试的日志/错误消息

user@mylaptor:mempoolmon: cat mpmonitor.pid

DEBUG - 2020-04-05 10:52:55,676 - PidFile: <pid.PidFile object at 0x7fc8faa479e8> entering setup
DEBUG - 2020-04-05 10:52:55,678 - PidFile: <pid.PidFile object at 0x7fc8faa479e8> create pidfile: /home/leilerg/python/mempoolmon/mpmonitor.pid
DEBUG - 2020-04-05 10:52:55,678 - PidFile: <pid.PidFile object at 0x7fc8faa479e8> check pidfile: /home/leilerg/python/mempoolmon/mpmonitor.pid
DEBUG - 2020-04-05 10:52:55,678 - PidFile: <pid.PidFile object at 0x7fc8faa479e8> closing pidfile: /home/leilerg/python/mempoolmon/mpmonitor.pid

对我来说,这似乎使pid日志文件与PID文件混淆。当我显式设置disable_existing_loggers=False时,这很奇怪。

有什么想法吗?

[如果相关,我正在使用最新的Linux Mint。我也在posted the question项目GitHub上的pid,因为我怀疑这是一个错误。

我一直在使用pid开发python守护程序,将初始日志直接登录到控制台后,我想使用python的日志记录模块切换到文件日志记录。这是我遇到问题的时候。我...

python-3.x linux logging pid conflicting-libraries
1个回答
0
投票

此问题已在GitHub页面issue 31上解决。

© www.soinside.com 2019 - 2024. All rights reserved.