2014-07-14 3 views
1

사용자 지정 서버 로그에서 변수를 가져 오기위한 정규식을 설정하려고합니다. 변수는 정규 표현식의 각 부분의 이름을 지정하여 움 직입니다. 다음은 그 예입니다.구문 분석 서버 추적에 대한 정규식

/^(?<time>[^ ]+) (?<host>[^ ]+) (?<process>[^:]+): (?<message>((?<key>[^ :]+)[ :])? ?((to|from)=<(?<address>[^>]+)>)?.*)$/ 

사용자 정의 형식에 대해 동일한 작업을 수행하려고합니다. 형식은 다음과 같습니다

[11/Jul/2014 19:35:38] ERROR [django.request.tastypie:273] Internal Server Error: /v1/notes/ 
Traceback (most recent call last): 
    File "/opt/client-api/venv/local/lib/python2.7/site-packages/tastypie/resources.py", line 195, in wrapper 
    response = callback(request, *args, **kwargs) 
    File "/opt/client-api/venv/local/lib/python2.7/site-packages/tastypie/resources.py", line 426, in dispatch_list 
    return self.dispatch('list', request, **kwargs) 
    File "/opt/client-api/venv/local/lib/python2.7/site-packages/tastypie/resources.py", line 458, in dispatch 
    response = method(request, **kwargs) 
    File "/opt/client-api/venv/local/lib/python2.7/site-packages/tastypie/resources.py", line 1320, in post_list 
    updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kwargs)) 
    File "/opt/client-api/venv/local/lib/python2.7/site-packages/tastypie/resources.py", line 2084, in obj_create 
    return self.save(bundle) 
    File "/opt/client-api/venv/local/lib/python2.7/site-packages/tastypie/resources.py", line 2230, in save 
    bundle.obj.save() 
    File "./notification/models.py", line 193, in save 
    handler.handle_notification() 
    File "./notification/handler.py", line 31, in handle 
    getattr(self, '_set_{}_payload'.format(preference))() 
    File "./notification/notification_handler.py", line 89, in _set_payload 
    raise Exception(error) 

내가 첫 번째 줄에 다양한 구성 요소를 잡는 변수에 넣어, 다음 변수에 전체 역 추적을 가하고 신경 모든.

이 질문에 너무 구체적 일 수 있습니다 실현하지만,이 기본 서버 로그를 구문 분석하는 많은 사람들에게 도움이 될 것이라고 커버 할 정규식을 실행 느낌.

+0

여기에 변수가 무엇입니까? – Braj

+0

코드를 표시하십시오. – furas

답변

2

이 줄에 뭔가 의미가 있습니까?

\[(?P<timestamp>.*?)\] (?P<level>\w+) \[(?P<location>.*?)\] (?P<message>.*?)\n(?P<details>.*?)(?=\n\[|$) 

는 주어진 예를 들어 http://regex101.com/r/zS1fN5/1

작품을 볼 수 있지만,이 로그 형식으로있을 수있는 단점 및 예외에 따라 달라집니다.