[英]Python Persist Log File Stream with PyInotify
I am experiencing an issue persisting a log file write stream through pyinotify
and it's threads. 我遇到一个问题,即通过
pyinotify
及其线程持久保存日志文件写入流。 I am using pyinotify
to monitor a directory for CLOSE_WRITE
file events. 我正在使用
pyinotify
监视目录中的CLOSE_WRITE
文件事件。 Before I initialize pyinotify
I create a log stream using the built-in logging
module like so: 在初始化
pyinotify
之前,我使用内置的logging
模块创建日志流,如下所示:
import os, logging
from logging import handlers
from logging.config import dictConfig
log_dir = './var/log'
name = 'com.sadmicrowave.tesseract'
LOG_SETTINGS = { 'version' : 1
,'handlers': { 'core': {
# make the logger a rotating file handler so the file automatically gets archived and a new one gets created, preventing files from becoming too large they are unmaintainable.
'class' : 'logging.handlers.RotatingFileHandler'
# by setting our logger to the DEBUG level (lowest level) we will include all other levels by default
,'level' : 'DEBUG'
# this references the 'core' handler located in the 'formatters' dict element below
,'formatter' : 'core'
# the path and file name of the output log file
,'filename' : os.path.join(log_dir, "%s.log" % name)
,'mode' : 'a'
# the max size we want to log file to reach before it gets archived and a new file gets created
,'maxBytes' : 100000
# the max number of files we want to keep in archive
,'backupCount' : 5 }
}
# create the formatters which are referenced in the handlers section above
,'formatters': {'core': {'format': '%(levelname)s %(asctime)s %(module)s|%(funcName)s %(lineno)d: %(message)s'
}
}
,'loggers' : {'root': {
'level' : 'DEBUG' # The most granular level of logging available in the log module
,'handlers' : ['core']
}
}
}
# use the built-in logger dict configuration tool to convert the dict to a logger config
dictConfig(LOG_SETTINGS)
# get the logger created in the config and named root in the 'loggers' section of the config
__log = logging.getLogger('root')
So, after my __log
variable get initialized it works immediately, allowing for log writes. 因此,在我的
__log
变量初始化之后,它可以立即工作,允许进行日志写入。 I want to start the pyinotify
instance next and would like to pass __log
using the following class definitions: 我想接下来启动
pyinotify
实例,并想使用以下类定义传递__log
:
import asyncore, pyinotify
class Notify (object):
def __init__ (self, log=None, verbose=True):
wm = pyinotify.WatchManager()
wm.add_watch( '/path/to/folder/to/monitor/', pyinotify.IN_CLOSE_WRITE, proc_fun=processEvent(log, verbose) )
notifier = pyinotify.AsyncNotifier(wm, None)
asyncore.loop()
class processEvent (pyinotify.ProcessEvent):
def __init__ (self, log=None, verbose=True):
log.info('logging some cool stuff')
self.__log = log
self.__verbose = verbose
def process_IN_CLOSE_WRITE (self, event):
print event
In the above implementation, my process_IN_CLOSE_WRITE
method gets triggered exactly as expected from the pyinotify.AsyncNotifier
; 在上面的实现中,我的
process_IN_CLOSE_WRITE
方法完全从pyinotify.AsyncNotifier
得到了预期的pyinotify.AsyncNotifier
; however, the log line for logging some cool stuff
never writes to the log file. 但是,用于
logging some cool stuff
的日志行永远不会写入日志文件。
I feel like it has something to do with persisting the file stream through the pyinotify threading process; 我觉得这与通过pyinotify线程过程持久化文件流有关; however, I'm not sure how to resolve this.
但是,我不确定该如何解决。
Any ideas? 有任何想法吗?
I might have found a resolution that seems to work. 我可能已经找到了一种可行的解决方案。 Not sure if it is the best approach, so I will leave the OP open for now to see if any other ideas are posted.
不确定这是否是最好的方法,因此我暂时将OP保持打开状态,以查看是否发布了其他任何想法。
I think I was handling my pyinotify.AsyncNotifier
setup wrong. 我认为我在处理
pyinotify.AsyncNotifier
设置错误。 I changed the implementation to: 我将实现更改为:
class Notify (object):
def __init__ (self, log=None, verbose=True):
notifiers = []
descriptors = []
wm = pyinotify.WatchManager()
notifiers.append ( pyinotify.AsyncNotifier(wm, processEvent(log, verbose)) )
descriptors.append( wm.add_watch( '/path/to/folder/to/monitor/', pyinotify.IN_CLOSE_WRITE, proc_fun=processEvent(log, verbose), auto_add=True )
asyncore.loop()
Now, when my wrapper class processEvents
gets triggered on instantiation of the listener, and when an event is triggered from a CLOSE_WRITE
event, the log
object is maintained and passed appropriately and can receive write events. 现在,当我的包装器类
processEvents
在侦听器实例化时触发,并且从CLOSE_WRITE
事件触发事件时, log
对象将得到适当维护和传递,并且可以接收写入事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.