I'm trying to sort a series of logs according to the timestamp. I am only able to sort by month given the sort() function by reading the file into a temporary array, but I would like to implement possibly a regular expression to grab a section of the string to sort by.
The regular expression I use to grab the lines is:
LOGGER_LINE = /([a-zA-Z]{3} \d{1,2}, \d{4} \d{1,2}:\d{2}:\d{2} (AM|PM) (\(SEVERE\)|\(WARNING\)).*)/
And I can produce an output like so:
[FILENAME]
Feb 16, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 16, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 16, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 16, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 16, 2014 1:00:20 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 17, 2014 1:00:00 AM (SEVERE) Thread: 18 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 17, 2014 1:00:00 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 17, 2014 1:00:00 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 17, 2014 1:00:00 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 17, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 17, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 17, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 17, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 17, 2014 1:00:20 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 18, 2014 1:00:00 AM (SEVERE) Thread: 18 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 18, 2014 1:00:00 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 18, 2014 1:00:00 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 18, 2014 1:00:00 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 18, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 18, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 18, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 18, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
Feb 18, 2014 1:00:20 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error
[FILENAME_2]
Feb 14, 2014 9:29:01 AM (WARNING) Thread: 26 [com.queue.lookupUtility.keyValueListAsMap] Failed to process standard key/value pair format.
Feb 14, 2014 9:33:50 AM (WARNING) Thread: 26 [com.queue.lookupUtility.keyValueListAsMap] Failed to process standard key/value pair format.
Feb 14, 2014 10:22:31 AM (WARNING) Thread: 27 [com.queue.lookupUtility.keyValueListAsMap] Failed to process standard key/value pair format.
Feb 14, 2014 10:39:31 AM (WARNING) Thread: 28 [com.queue.lookupUtility.keyValueListAsMap] Failed to process standard key/value pair format.
Feb 14, 2014 10:40:31 AM (WARNING) Thread: 28 [com.queue.lookupUtility.keyValueListAsMap] Failed to process standard key/value pair format.
Feb 18, 2014 8:43:45 AM (WARNING) Thread: 13 [com.nioHandler]Closing socket to endpoint Address[127.0.0.1:5703], Cause:java.io.EOFException
However my end goal is to sort the timestamps and their according error in descending order.
Might there be a way I can sort the lines according to the timestamp that is matched by the LOGGER_LINE
regex? Other suggestions on sorting the timestamp would be excellent.
You should not reinvent the wheel. The time
library parses the strings that you have. For example, given the line:
l = "Feb 16, 2014 1:00:10 AM (SEVERE) Thread: 14 [com.refresh.RefreshActionQueue.write] Could not write RefreshAction checkpoint due to error"
The time can be extracted like this:
require "time"
Time.parse(l)
# => 2014-02-16 01:00:10 +0900
So, if you have an array say, array_of_lines
of such lines, you can do:
require "time"
array_of_lines.sort_by{|l| Time.parse(l)}.reverse
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.