我有一个包含像这样的位的日志:
[2012-04-16 15:16:43,827: DEBUG/PoolWorker-2] {'feed': {}, 'bozo': 1, 'bozo_exception': URLError(error(110, 'Connection timed out'),), 'entries': []} [2012-04-16 15:16:43,827: ERROR/PoolWorker-2] get_entries Traceback (most recent call last): File "/opt/myapp/app.py", line 491, in get_entries logging.getLogger(__name__).debug("Title: %s" % doc.title) File "build/bdist.linux-x86_64/egg/feedparser.py", line 423, in __getattr__ raise AttributeError, "object has no attribute '%s'" % key AttributeError: object has no attribute 'title' [2012-04-16 15:16:43,828: INFO/MainProcess] Task myapp.do_task[4fe968ff-e069-4cfe-9a81-aece0d97c289] succeeded in 21.0481028557s: None
我想从中提取部分内容如下:
我很确定这对Grep来说太过分了,所以怎么做呢?
(好吧,而不是懒惰,我已经明白了 – 会发布我的解决scheme。)
这对我有用 – 不完全如上所述,但足够接近:
awk '/ERROR|WARN/,/DEBUG|INFO/ { if ($0 !~ /(DEBUG|INFO)/) { print } }' < logfile
awk支持这个非常方便: /startpattern/,/stoppattern/ { }
。 不幸的是,如果停止模式与起始模式在同一条线上匹配,则仅打印出该线,因此需要不同的停止模式。
尝试这样的事情:
cat importantstuff.log | grep 'File .*, line .*, in .*' -B 1 -A 2
不完全回答这个问题,但我认为它完成了任务。
匹配之后或之前的grep控制行的-A
和-B
标志。
这是有效的,因为grep组相邻的匹配,所以你最终以很好的分离回溯:
Traceback (most recent call last): File "sfquest.py", line 9, in b c() File "sfquest.py", line 15, in c d() File "sfquest.py", line 20, in d raise Exception('important information') Exception: important information -- Traceback (most recent call last): File "sfquest.py", line 9, in b c() File "sfquest.py", line 15, in c d() File "sfquest.py", line 20, in d raise Exception('important information') Exception: important information -- Traceback (most recent call last): File "sfquest.py", line 9, in b c() File "sfquest.py", line 15, in c d() File "sfquest.py", line 20, in d raise Exception('important information') Exception: important information
以下是我用来生成追溯示例的示例代码:
import traceback def a(): b() def b(): for i in range(10): try: c() except Exception, e: print 'bad stuff' print traceback.format_exc(e) def c(): d() def d(): for i in range(10): print 'random junk' raise Exception('important information') a()