未找到 "deluge" 的日志处理器

2 投票
1 回答
1602 浏览
提问于 2025-04-18 11:40

我有一个Python脚本,用于定时删除超过3天的种子文件。这个脚本在我从终端运行时能正常工作,但在crontab中运行时却出现了一个错误:No handlers could be found for logger "deluge"。有人能告诉我该怎么解决这个问题吗?

这是我的Python脚本:

    #!/usr/bin/python

    from deluge.log import LOG as log
    from deluge.ui.client import client
    import deluge.component as component
    from twisted.internet import reactor, defer
    import time

    ############
    cliconnect = client.connect(host='127.0.0.1',port=58846)
    seeddir = "/home/mou/CPDownloads" # Directory to ignore for torrents to remain seeding
    timedifference = 3 # Remove torrents older than this this time (in days)
    is_interactive = False # Set this to True to allow direct output or set to False for cron
    do_remove_data = True # Set to True to delete torrent data as well, false to leave it
    ###############

    oldcount = 0
    skipcount = 0
    seedcount = 0
    errorcount = 0
    torrent_ids = []

    def printSuccess(dresult, is_success, smsg):
        global is_interactive
        if is_interactive:
            if is_success:
                print "[+]", smsg
            else:
                print "[i]", smsg

    def printError(emsg):
        global is_interactive
        if is_interactive:
            print "[e]", emsg

    def endSession(esresult):
        if esresult:
            print esresult
            reactor.stop()
        else:
            client.disconnect()
            printSuccess(None, False, "Client disconnected.")
            reactor.stop()

    def printReport(rresult):
        if errorcount > 0:
            printError(None, "Failed! Number of errors: %i" % (errorcount))
        else:
            if oldcount > 0:
                printSuccess(None, True, "Removed %i torrents -- Skipped %i torrents -- Seeding %i torrents" % (oldcount, skipcount, seedcount))
            else:
                printSuccess(None, True, "No old torrents! -- Skipped %i torrents -- Seeding %i torrents" % (skipcount, seedcount))
        endSession(None)

    def on_torrents_status(torrents):
        global filtertime
        tlist=[]
        for torrent_id, status in torrents.items():
            if status["save_path"] == seeddir:
                global seedcount
                seedcount += 1
            else:
                unixtime = "%s" % (status["time_added"])
                numunixtime = int(unixtime[:-2])
                humantime = time.ctime(numunixtime)
                if numunixtime < filtertime:
                    global do_remove_data
                    global oldcount
                    oldcount += 1
                    successmsg = " Removed %s:  %s from %s" % (humantime, status["name"], status["save_path"])
            errormsg = "Error removing %s" % (status["name"])
            tlist.append(client.core.remove_torrent(torrent_id, do_remove_data).addCallbacks(printSuccess, printError, callbackArgs = (True, successmsg), errbackArgs = (errormsg)))
                else:
                    global skipcount
                    skipcount += 1
                    printSuccess(None, False, " Skipping %s: %s from %s" % (humantime, status["name"], status["save_path"]))
        defer.DeferredList(tlist).addCallback(printReport)

    def on_session_state(result):
        client.core.get_torrents_status({"id": result}, ["name","time_added","save_path",]).addCallback(on_torrents_status)

    def on_connect_success(result):
        printSuccess(None, True, "Connection was successful!")
        global timedifference
        global filtertime
        curtime = time.time()
        filtertime = curtime - (timedifference * 24 * 60 * 60)
        printSuccess(None, False, "Current unix time is %i" % (curtime))
        printSuccess(None, False, "Filtering torrents older than %s" % (time.ctime(int(filtertime))))
        client.core.get_session_state().addCallback(on_session_state)

    cliconnect.addCallbacks(on_connect_success, endSession, errbackArgs=("Connection failed: check settings and try again."))

    reactor.run()

当我从终端运行它时,输出是:

    mou@mou-lanister:~/scripts$ ./cpmanagertest.py 
    [+] Connection was successful!
    [i] Current unix time is 1404169193
    [i] Filtering torrents older than Sun Jun 29 18:59:53 2014
    [i]  Skipping Mon Jun 30 03:57:52 2014: War.Horse.2011.1080p.MKV.x264.AC3.DTS.NL.Subs from /home/mou/CPDownloads/
    [i]  Skipping Mon Jun 30 04:00:00 2014: The Legend Of Hercules 2014 1080p BluRay DTS x264 PublicHD from /home/mou/CPDownloads/
    [+]  Removed Sun Jun 29 16:52:16 2014:  Paranormal.Activity.The.Marked.Ones.2014.EXTENDED.1080p.BluRay.x264-SPARKS [PublicHD] from /home/mou/CPDownloads/
    [+]  Removed Sun Jun 29 17:07:12 2014:  The.Hobbit.The.Desolation.Of.Smaug.2013.1080p.BluRay.DTS.x264-PublicHD from /home/mou/CPDownloads/
    [+]  Removed Sun Jun 29 17:22:08 2014:  thor the dark world 2013 1080p bluray x264 sparks publichd from /home/mou/CPDownloads/
    [+]  Removed Sun Jun 29 16:24:32 2014:  The.Conjuring.2013.1080p.BluRay.x264-ALLiANCE [PublicHD] from /home/mou/CPDownloads/
    [+]  Removed Sun Jun 29 17:00:48 2014:  Her.2013.1080p.BluRay.x264-SPARKS [PublicHD] from /home/mou/CPDownloads/
    [+]  Removed Sun Jun 29 17:20:00 2014:  Rush 2013 1080p 60fps BluRay x264-zologne from /home/mou/CPDownloads/
    [+]  Removed Sun Jun 29 17:22:08 2014:  Escape.Plan.2013.1080p.BluRay.DTS-HD.MA.7.1.x264-PublicHD from /home/mou/CPDownloads/
    [+]  Removed Sun Jun 29 17:05:04 2014:  The.Fifth.Estate.2013.1080p.BluRay.x264-SPARKS [PublicHD] from /home/mou/CPDownloads/
    [+] Removed 8 torrents -- Skipped 2 torrents -- Seeding 0 torrents
    [i] Client disconnected.

这是crontab作业的日志:

No handlers could be found for logger "deluge"
No handlers could be found for logger "deluge"
No handlers could be found for logger "deluge"
No handlers could be found for logger "deluge"
No handlers could be found for logger "deluge"
No handlers could be found for logger "deluge"
No handlers could be found for logger "deluge"
No handlers could be found for logger "deluge"
No handlers could be found for logger "deluge"
No handlers could be found for logger "deluge"
No handlers could be found for logger "deluge"

在我添加了调试代码后,我得到了以下错误:

DEBUG:deluge:ConfigManager started..
INFO:deluge:Connecting to daemon at 127.0.0.1:58846..
INFO:deluge:Connected to daemon at 127.0.0.1:58846..
ERROR:deluge:RPCError Message Received!
--------------------------------------------------------------------------------
RPCRequest: daemon.login(localclient, f4f86361c7b9443464d0078f8d7c012e2ed63ce9)
--------------------------------------------------------------------------------
  File "/usr/lib/python2.7/dist-packages/deluge/core/rpcserver.py", line 259, in dispatch
    ret = component.get("AuthManager").authorize(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/deluge/core/authmanager.py", line 93, in authorize
    raise BadLoginError("Password does not match")

BadLoginError: Password does not match
--------------------------------------------------------------------------------
DEBUG:deluge:_on_login_fail(): [Failure instance: Traceback (failure with no frames): <class 'deluge.ui.client.DelugeRPCError'>: <deluge.ui.client.DelugeRPCError object at 0x7f58263f6150>
]
DEBUG:deluge:on_connect_fail: [Failure instance: Traceback (failure with no frames): <class 'deluge.ui.client.DelugeRPCError'>: <deluge.ui.client.DelugeRPCError object at 0x7f58263f6150>
]
INFO:deluge:Connection lost to daemon at 127.0.0.1:58846 reason: Connection was closed cleanly.

1 个回答

3

啊,这是Python的一种“缺陷”。

你可以加上这个来隐藏这些信息:

import logging 
logging.getLogger('deluge').addHandler(logging.NullHandler())

或者把所有信息都写到错误输出里:

import logging, sys
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)

撰写回答