Reputation: 11774
I'm trying to set up a regular expression for grabbing variables from a custom server log. The variables are grabbed by naming each portion of a regular expression. Here's an example:
/^(?<time>[^ ]+) (?<host>[^ ]+) (?<process>[^:]+): (?<message>((?<key>[^ :]+)[ :])? ?((to|from)=<(?<address>[^>]+)>)?.*)$/
I want to do the same for a custom format. The format is as follows:
[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)
All I care about is grabbing the various components on the first line, putting them into variables, and then putting the entire traceback in a variable.
I realize this may be too specific of a question, but I feel a basic run down of the regex that would cover this would be beneficial to many people trying to parse server logs.
Upvotes: 1
Views: 819
Reputation: 14126
You mean something along these lines?
\[(?P<timestamp>.*?)\] (?P<level>\w+) \[(?P<location>.*?)\] (?P<message>.*?)\n(?P<details>.*?)(?=\n\[|$)
See http://regex101.com/r/zS1fN5/1
Works for the given example, but depends on what quirks and exceptions there can be to this log format.
Upvotes: 2