3
我正在使用Pyramid和rq,並且我想將rq worker作業記錄在與我的金字塔應用程序相同的文件中。寫入與金字塔和rq相同的日誌文件
我的設置是這樣的:
development.ini
# regular pyramid setup here...
[loggers]
keys = root, main
[handlers]
keys = console, debuglog
[formatters]
keys = generic
[logger_root]
level = DEBUG
handlers = console, debuglog
[logger_main]
level = DEBUG
handlers =
qualname = main
[handler_debuglog]
class = handlers.RotatingFileHandler
args = ('%(here)s/__logs/pyramid_debug.log','a')
level = DEBUG
formatter = generic
[formatter_generic]
format = %(asctime)s %(levelname)-8s [%(name)s][%(threadName)s] %(message)s
main.py
from bunch import Bunch
from redis import Redis
from rq import Queue, Worker
from helpers.rqworker import run_worker
from helpers.log import write_log
write_log('info', 'APP STARTED.')
q = Queue(connection=Redis())
req_obj = Bunch()
req_obj.remote_addr = self.request.remote_addr
job = q.enqueue(run_worker, req_obj, {'some_other': 'data'})
write_log('info', 'APP ENDED.')
傭工/ rqworker.py
from helpers.log import write_log
def run_worker(req, data):
write_log(req, 'debug', 'WORKER STARTED', separator=True)
傭工/ log.py
import logging
log = logging.getLogger(__name__)
def write_log(request, loglevel, message=None, **kwargs):
msg = '[{0}] {1}'.format(request.remote_addr, message)
level = logging.getLevelName(loglevel.upper())
if 'separator' in kwargs:
log.log(level, '='*40)
if message:
log.log(level, msg)
if 'stack' in kwargs:
log.log(level, '{0} EXCEPTION {0}'.format('*'*15))
log.log(level, kwargs['stack'])
log.log(level, '{0} EXCEPTION END {0}'.format('*'*13))
當我運行我的應用程序,我不能看到rqworker.py
日誌(可能是因爲它是一個單獨的線程),所以我不知道我怎麼可以(和有可能)實現寫入相同的日誌文件?