Python merge Day Tomcat Exception Log Third edition

Source: Internet
Author: User

Written in general, will look.

The wording of the parameter

If len (SYS.ARGV)  == 3 and sys.argv[2] in accepted_modes:     java_name = sys.argv[1]    mode = sys.argv[2]else:     usage ()      #Check  if process is running / get  pidjproc = jprocess (Java_name)  pid = jproc.chk_proc ()     if  pid !=  ""  and  mode ==  ' All ':    jproc.get_jstats ()   # print jproc.zdict   jproc.compute_jstats ()                    fnull = open ( os.devnull,  ' W ')    for key in jproc.zdict:         #print  key,jproc.zdict[key]       jproc.send_to_zabbix (key )    fnull.close ()    print pid  else:   print 0 
    1. Generate date formats such as Error_list20170104.txt

# !/usr/bin/env python#-*-coding:utf-8-*-import osimport sysimport timeimport  Datetimeimport stringfrom smtplib import smtpfrom email import mimetextfrom  email import Headerfrom os.path import getsizefrom sys import  exitfrom re import compile, ignorecasetomcat_log =  '/home/logs/' Last_position_ logfile =  '/home/back/test/last_position.txt ' #error_list_logfile  =  '/home/back/test/error_ List.txt ' error_list_logfile1 =  '/home/back/test/error_list ' Now = datetime.datetime.now () Strdatetime = now.strftime ("%y/%m/%d") strdatetime1 = now.strftime ("%Y%m%d") Date= Now.strftime ("%d") error_list_logfile = error_list_logfile1 + strdatetime1 +  ". TXT "print error_list_logfilepattern = compile (R ' exception|^\t+\bat\b ',  IGNORECASE) fp  = open (error_list_logfile,  "w+") def writetotxt (List_name, file_path):    try:         fp = open (file_path,  "A +")          for item in list_name:             fp.write (str (item)  +  "\ n")         fp.close ()     except ioerror:        print ("fail  To open file ") #  parse file to find the exception line Def analysis_log (file): #parent_file  = os.path.dirname ( File) Error_list = []try:data = open (file,  ' R ') except:exit () for line in  data:if pattern.search (line): error_list.append (file)                           #print  error_list                          #errors_list  += 1                           #print   "%s"  %file                         break         Data.close ()         if len (error_list) ==1:              #return   ". Join (Error_list)              writetotxt (Error_list,error_list_logfile)           #return  error_list         #if   Errors_list == 1:             #print   "%s num is %d"  % (file, errors_list) #print   ". Join (Errors_list) Def walk (local_dir): For root, dirs, files  in os.walk (local_dir): For filespath in files:# return filespathlocal_file  = os.path.join (Root, filespath) # print error_info2error_info = analysis_ Log (Local_file) def walk_dir (home_dir):homedir=[]        for  Root, dirs, files in os.walk (Home_dir):             for dirname in dirs:                         local_dir  = os.path.join (Root, dirname)                           #print   "date is %s"  %Date                          result = string.find (local_dir,strdatetime)!=-1                          if result:                              #print  local_ Dir.count ('/')                              if local_dir.count ('/') ==8:                          &Nbsp;       homedir.append (Local_dir)     return  homedirif __name__ ==  ' __main__ ':     if not argv1:         tomcat_last_dir=walk_dir (Tomcat_log)          for i in tomcat_last_dir:        walk (i)

2. Notation with parameters, such as Python test.py 20170103, to generate Error_list20170103.txt

# !/usr/bin/env python#-*-coding:utf-8-*-import osimport sysimport timeimport  Datetimeimport stringfrom smtplib import smtpfrom email import mimetextfrom  email import Headerfrom os.path import getsizefrom sys import  exitfrom re import compile, ignorecasetomcat_log =  '/home/logs/' Last_position_ logfile =  '/home/back/test/last_position.txt ' #error_list_logfile  =  '/home/back/test/error_ List.txt ' error_list_logfile1 =  '/home/back/test/error_list ' Now = datetime.datetime.now () Strdatetime = now.strftime ("%y/%m/%d") strdatetime1 = now.strftime ("%Y%m%d") Date= Now.strftime ("%d") error_list_logfile = error_list_logfile1 + strdatetime1 +  ". TXT "print error_list_logfilepattern = compile (R ' exception|^\t+\bat\b ',  IGNORECASE) fp  = open (error_list_logfile,  "w+") def writetotxt (List_name, file_path):    try:         fp = open (file_path,  "A +")          for item in list_name:             fp.write (str (item)  +  "\ n")         fp.close ()     except ioerror:        print ("fail  To open file ") #  parse file to find the exception line Def analysis_log (file): #parent_file  = os.path.dirname ( File) Error_list = []try:data = open (file,  ' R ') except:exit () for line in  data:if pattern.search (line): error_list.append (file)                           #print  error_list                          #errors_list  += 1                           #print   "%s"  %file                         break         Data.close ()         if len (error_list) ==1:              #return   ". Join (Error_list)              writetotxt (Error_list,error_list_logfile)           #return  error_list         #if   Errors_list == 1:             #print   "%s num is %d"  % (file, errors_list) #print   ". Join (Errors_list) Def walk (local_dir): For root, dirs, files  in os.walk (local_dir): For filespath in files:# return filespathlocal_file  = os.path.join (Root, filespath) # print error_info2error_info = analysis_ Log (Local_file) def walk_dir (home_dir):homedir=[]        for  Root, dirs, files in os.walk (Home_dir):             for dirname in dirs:                         local_dir  = os.path.join (Root, dirname)                           #print   "date is %s"  %Date                          result = string.find (local_dir,strdatetime)!=-1                          if result:                              #print  local_ Dir.count ('/')                              if local_dir.count ('/') ==8:                          &Nbsp;       homedir.append (Local_dir)          RETURN HOMEDIRDEF WALK_DIR_ARGV (Home_dir,strdatetime):     homedir=[]     for root, dirs, files in os.walk (Home_dir):         for dirname in dirs:                     local_dir =  Os.path.join (Root, dirname)                       #print   "date is %s"  %Date                     result  = string.find (local_dir,strdatetime)!=-1                     if result:                          #print  local_dir.count ('/')                          if local_dir.count ('/') ==8:                              homedir.append (Local_dir)     return homedirif __name__ ==   ' __main__ ':      argv1=sys.argv[1]    if not argv1 :         tomcat_last_dir=walk_dir (Tomcat_log)          for i in tomcat_last_dir:         walk (i)     else:        error_list_logfile = error_ list_logfile1 + argv1 +  ". txt"         tomcat_last_ DIR=WALK_DIR_ARGV (tomcat_log,strdatetime)         for i in  tomcat_last_dir:            walk (i)

The location of parameters and variables also requires optimized processing.

This article from "people, to have their own ideas" blog, declined to reprint!

Python merge Day Tomcat Exception Log Third edition

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.