1

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)
[
1
  • use grep. See man grep Commented Apr 24, 2017 at 22:56

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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'.

Comments

0

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}'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.