A simple log monitoring script with the following functions: 1.windows environment 2. When the log keyword is matched, a sound is emitted, different keywords are matched, and different sounds are played 3. Real-time response possible
Note: It is in the win environment OH
Go directly to 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 sysimport Osimpor T getoptimport subprocessimport timeimport codecsimport 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:i f k = = '-F ': path = v elif k = = '-h ': print __doc__ return 0 if not (path an d 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.stri P () If not line:continue keyword, wav = Line.strip (). Split (': ') keywordmap[keyword] = wav While true:line = Output.stdout.readline () #process code, the processing code after getting the output information if not line: Time.sleep (0.01) Continue line = Line.strip (). Decode (' UTF8 ') print line for keyword in ke Ywordmap:if line.find (keyword) > -1:winsound. PlaySound (Keywordmap[keyword], WinSound. Snd_nodefault) #time. Sleep (0.01) return 0 if __name__ = = ' __main__ ': sys.exit (Main ())