Home Backend Development Python Tutorial Python monitoring logger

Python monitoring logger

Oct 18, 2016 am 09:22 AM

A simple log monitoring script with the following functions: 1. Windows environment 2. When the log keyword is matched, a sound will be emitted. Different matching keywords will play different sounds 3. Able to respond in real time

Note: Yes In the win environment

Just upload the code

#!/usr/bin/env python
# encoding: utf-8
   
"""
MonitorLog.py
   
Usage: MonitorLog.py ...
Monitor the log file
   
-f  log file
-h  help info
   
python MonitorLog.py -f C:\monitor.log
   
"""
   
import sys
import os
import getopt
import subprocess
import time
import codecs
import winsound
   
ABSPATH = os.path.dirname(os.path.abspath(__file__))
MONITERCONF = 'moniter_keyword.txt' #utf8 file
   
def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'hf:')
    except getopt.GetoptError, err:
        print str(err)
        print __doc__
        return 1
   
    path = ''
    for k, v in opts:
        if k == '-f':
            path = v
        elif k == '-h':
            print __doc__
            return 0
   
    if not (path and os.path.exists(path)):
        print 'Invalid path: %s' % path 
        print __doc__
        return 2
   
    #命令行元组
    cmd = ('tail', '-f', path)
    print ' '.join(cmd)
    output = subprocess.Popen(cmd, stdout=subprocess.PIPE)
   
    keywordMap = {}
    #加载监控的关键字信息
    with codecs.open(os.path.join(ABSPATH, MONITERCONF), 'r', 'utf8') as f:
        lines = f.readlines()
    for line in lines:
        line = line.strip()
        if not line:
            continue
        keyword, wav = line.strip().split(':')
        keywordMap[keyword] = wav
   
    while True:
        line = output.stdout.readline()
        #process code,得到输出信息后的处理代码
        if not line:
            time.sleep(0.01)
            continue
        line = line.strip().decode('utf8')
        print line
        for keyword in keywordMap:
            if line.find(keyword) > -1:
                winsound.PlaySound(keywordMap[keyword], 
                                   winsound.SND_NODEFAULT)
        #time.sleep(0.01)
    return 0
   
if __name__ == '__main__':
    sys.exit(main())
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How Do I Use Beautiful Soup to Parse HTML? How Do I Use Beautiful Soup to Parse HTML? Mar 10, 2025 pm 06:54 PM

How Do I Use Beautiful Soup to Parse HTML?

Image Filtering in Python Image Filtering in Python Mar 03, 2025 am 09:44 AM

Image Filtering in Python

How to Download Files in Python How to Download Files in Python Mar 01, 2025 am 10:03 AM

How to Download Files in Python

How to Use Python to Find the Zipf Distribution of a Text File How to Use Python to Find the Zipf Distribution of a Text File Mar 05, 2025 am 09:58 AM

How to Use Python to Find the Zipf Distribution of a Text File

How to Work With PDF Documents Using Python How to Work With PDF Documents Using Python Mar 02, 2025 am 09:54 AM

How to Work With PDF Documents Using Python

How to Cache Using Redis in Django Applications How to Cache Using Redis in Django Applications Mar 02, 2025 am 10:10 AM

How to Cache Using Redis in Django Applications

How to Perform Deep Learning with TensorFlow or PyTorch? How to Perform Deep Learning with TensorFlow or PyTorch? Mar 10, 2025 pm 06:52 PM

How to Perform Deep Learning with TensorFlow or PyTorch?

Introducing the Natural Language Toolkit (NLTK) Introducing the Natural Language Toolkit (NLTK) Mar 01, 2025 am 10:05 AM

Introducing the Natural Language Toolkit (NLTK)

See all articles