본문 바로가기

Develop/Python

Python 로그파일 읽어서 원하는 내용 따로 파일 만들기

출처 : http://napsis.tistory.com/252



import os.path

import gzip

import re




prompt = '> '



############ DEFINE ##################

#start_str = '========================== Controller START =========================='

#finish_str = '========================== Controller FINISH =========================='

#"\d{1,3}?\.\d{1,3}?\.\d{1,3}?\.\d{1,3}",

ip_reg = re.compile('^\[ip-[0-9]+-[0-9]+-[0-9]+-[0-9]+]')

thread_reg = re.compile('\[Thread-[0-9]{5}]')

#timestamp_reg = re.compile('[0-9]{4}:[0-9]{2}:[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}')

timestamp_reg = re.compile('[0-9]{4}:[0-9]{2}:[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}[.][0-9]{3}')

######################################


current_directory = os.getcwd()

output_directory = "ResultParseLogs"


print "Input the Log script name \n"

print "With check current directory : %s" %current_directory

org_log_file = raw_input(prompt)

print "Input the KeyWord for search"

searchFor = raw_input(prompt)




if not os.path.exists(output_directory):

    os.makedirs(output_directory)



# existence of the key

def check_the_key(dict, key):

    return dict.has_key(key)



# find str in value -> return key

def search(dict, key):

    value_from_key = ''.join(dict[key])

    return value_from_key.find(searchFor)




# append dictionary

def refresh_dict(dict, key, value):

    if check_the_key(dict, key) is True:

        temp_value = dict[key]

        result = ''.join(temp_value +value)

        dict[key] = result

    else:

        dict[key] = value




def make_file_from_dict(dict, key):

    if search(dict, key) > 0:

        result = ''.join(dict[key])

        filename = "output_"+key

        filename = os.path.join(output_directory, filename)

        with open(filename,"a+") as w:

            w.write(result)




def make_dict_from_file(d):

    if org_log_file[-2:] == "gz":

        with gzip.open(org_log_file, 'r') as f:

            for line in f:

                if not line:

                    break


                if ip_reg.search(line) is not None and thread_reg.search(line) is not None:

                    server = ip_reg.search(line).group()

                    thread = thread_reg.search(line).group()

                    key = server + thread

                    d[key] = line


                #    refresh_dict(d, key, line)

                    make_file_from_dict(d, key)

                else:

                    pass


            print "GZ End"

            f.close()

    else:

        print "else"

        with open(org_log_file) as f:

            for line in f:

                if not line:

                    break


                if ip_reg.search(line) is not None and thread_reg.search(line) is not None:

                    server = ip_reg.search(line).group()

                    thread = thread_reg.search(line).group()

                    key = server + thread

                    d[key] = line


                #    refresh_dict(d, key, line)

                    make_file_from_dict(d, key)

                else:

                    pass


            print "Log End"

            f.close()


# dictionary

dict = {}

make_dict_from_file(dict)