chandan
chandan

Reputation: 19

Finding pattern in log file

How can I pick all the ERROR and WARN and FATAL line from a log file with today's date? Thanks in advance.

My log file are like below.

[2017-04-24 17:26:48,385] WARN ******* GOODBYE /10.170.208.1:35084 ******** (org.apache.zookeeper.server.quorum.LearnerHandler)
[2017-04-24 17:26:48,385] WARN Ignoring unexpected exception (org.apache.zookeeper.server.quorum.LearnerHandler)
java.lang.InterruptedException
        at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(AbstractQueuedSynchronizer.java:1220)
        at java.util.concurrent.locks.ReentrantLock.lockInterruptibly(ReentrantLock.java:335)
        at java.util.concurrent.LinkedBlockingQueue.put(LinkedBlockingQueue.java:339)
        at org.apache.zookeeper.server.quorum.LearnerHandler.shutdown(LearnerHandler.java:654)
        at org.apache.zookeeper.server.quorum.LearnerHandler.run(LearnerHandler.java:647)
[2017-04-24 17:26:48,385] INFO Reading snapshot /bnsf/kafka/zookeeper/version-2/snapshot.900000000 (org.apache.zookeeper.server.persistence.FileSnap)
[2017-04-24 17:26:48,476] INFO Notification: 1 (message format version), 1 (n.leader), 0x243970000003b (n.zxid), 0x243aa (n.round), LOOKING (n.state), 1 (n.sid), 0x243a5 (n.peerEpoch) LOOKING (my state) (org.apache.zookeeper.server.quorum.FastLeaderElection)
[2017-04-24 17:26:48,496] INFO Notification: 1 (message format version), 2 (n.leader), 0x243970000003b (n.zxid), 0x243aa (n.round), LOOKING (n.state), 2 (n.sid), 0x243a5 (n.peerEpoch) LOOKING (my state) (org.apache.zookeeper.server.quorum.FastLeaderElection)
[

Upvotes: 1

Views: 116

Answers (3)

James K. Lowden
James K. Lowden

Reputation: 7837

First, you want to have "today's date" in the format your file has.

$ date +%Y-%m-%d
2017-04-24

You can assign that to variable:

$ today=$(date +%Y-%m-%d)

Then I would use awk because ERROR, WARN, and FATAL appear in the 3rd field:

$ awk -v today="[$today" '$1 == today && $3 ~ /ERROR|WARN|FATAL/ {print}'

Upvotes: 0

Ryan McCleary
Ryan McCleary

Reputation: 371

The best option would probably be to use grep. You could use date +%F to get the current date, using it as part of the regex. For example:

grep "$(date +%F).*\(WARN\|ERROR\|FATAL\)" log_file.log

This should grab all of the lines from the current day in log_file.log containing either 'WARN', 'ERROR', or 'FATAL'.

Upvotes: 0

codeforester
codeforester

Reputation: 42999

Use grep:

grep -E "^\[$(date +%Y-%m-%d).*(FATAL|ERROR|WARN)" logfile

For your example, we get this output:

[2017-04-24 17:26:48,385] WARN ******* GOODBYE /10.170.208.1:35084 ******** (org.apache.zookeeper.server.quorum.LearnerHandler)
[2017-04-24 17:26:48,385] WARN Ignoring unexpected exception (org.apache.zookeeper.server.quorum.LearnerHandler)

Use -A n option if you need to include a few lines after the match to cover multi-line logs.

Upvotes: 1

Related Questions