Package: logdogs
A daemon to monitor keywords in any log files specified by glob pattern.In the background log files are checked periodically by dogs and user
defined handlers are called when error lines are detected according the
keyword regex.featuresglob pathregex keywordscompatible with logrotatecustmize handler function or callable objectlog files don’t have to exist before watcha dog can watch multiple logs and a log can be watched by multiple
dogsusageinstall:pip install logdogsstart:logdogs -c conf.pystop:kill <pid>pid file will be removed automatically.conf.py is your config file which contains upper case module variables
as configuration. Here is an example:importosimportloggingLOG_FILE='logdogs.log'LOG_LEVEL='INFO'# you can even call basicConfig to customize the log insteadINTEVAL=10# secondsDAEMONIZE=TrueDIR=os.path.abspath('.')PID_FILE='logdogs.pid'STDOUT='logdogs.out'STDERR='logdogs.err'# the above 4 configurations only work when DAEMONIZE is Truelogger=logging.getLogger(__name__)classMyHandler(object):def__init__(self):self.count=0def__call__(self,file,lines):self.count+=1logger.info('...')# Do whatever you want here...DOGS={"test":{"paths":["a.log","b.log"],"handler":MyHandler(),"includes":[r"wrong"],"excludes":[r"nothing"]},"glob":{"paths":["**/*.log"],"handler":MyHandler(),"includes":[r"(?!)wrong"],}}In this case, logdogs will run as a daemon process in current directory
and check log files every 10 seconds. a.log and b.log will be watched
both by dog test and glob. When a line containingwrongbut notnothingis written to a.log, both dogs’ handler will be called.The effective variables in config file are described as below.configDOGSA Dog consists of:a group of log files specified by glob patterna filter defined by includes and excludesa handler function or a callable objectDOG is a dict in the form of{name: attribute}wherenameis not
important andattributeis a dict containing the following keys:handlera handler is a function which has the following signature:def handler(file, lines):
    """
    file is the absolute path of the log file.
    lines is a list of the lines includes newline characters(\n)
    """
    passthe default handler is a callable object of:class Handler(object):
    """
    default handler for log event
    """
    def __call__(self, file, lines):
        print(lines)It’s up to you to deal with the log line in this handler such as
mailing, send to wechat and etc.includes & excludesThey are regular expressions and both are optional. The handler is
called if any regex in includes is found in the line and any regex in
excludes is not found in the line. That is to say,orlogic is
applied in the includes andandlogic is applied in the excludes.pathpath is a list, it supports the following forms:single file:['/var/logs/a.log']multiple files:['/var/logs/a.log','/var/logs/b.log']glob pattern:['/var/logs/*.log']recursive glob (similar as globstar on bash):['/var/logs/**/*.log']In the last 2 cases, a log file is not required to exist when monitor
startsThe same log file can overlap in multiple dog blockINTEVALseconds for sleep between checkslogLOG_FILE: specify log file. logs are printed to stdout if not
specifiedLOG_LEVEL(WARNING): which log level to usedaemonizeDAEMONIZE(False): whether to start a daemon process running in the
backgroup,the following configs only take effect when DAEMONIZE is
TrueDIR: set the working directory,default is /PID_FILE: pid file pathSTDOUT: where to redirect stdout(print exception traceback for
example)STDERR: where to redirect sterrDevelopmentpython setup.py developtestpython -m unittest -v test_function.TestFunctiontodomore handlersthreading