I have several logs containing lines all starting with a timestamp, so that the following works as expected to merge them:
cat myLog1.txt myLog2.txt | sort -n &g
The open source tool (Java GitHub) lets you combine log files with different formats including multilines into a merged file.
The tool allows to shift the time of records in a log file. It can be useful when files come from different time zones.
It also allows to generate additional information to a merged file, for example applications names or timestamps in a uniform format, see the example.
The tool can be used as a command line tool or Java library. Note: I'm the author.
I was struggling with the same issue and finally I think I've got it. Try do it like:
sort -nbms -k1.1,1.2 -k1.4,1.5 -k1.7,1.8 -k1.10,1.12 myLog1.txt myLog2.txt > combined.txt
It's still not fully clear to myself, I'll try to give some explanation though. According to the man pages used switches mean:
-n, --numeric-sort - compare according to string numerical value.
-b, --ignore-leading-blanks - ignore leading blanks.
-s, --stable - stabilize sort by disabling last-resort comparison
-m, --merge - merge already sorted files; do not sort
-k, --key=POS1[,POS2] - start a key at POS1 (origin 1), end it at POS2 (default end of line)
-m
. It's crucial to keep stacktraces from getting scrambled.-b
is not necessary in this case as somehow -n
and -m
combined keeps stacktrace lines from getting clustered. I left it just in case as most of stacktrace lines starts with blanks.-n
apparently stops comparing key whenever there is a non-numeric character in the key. That's the second crucial bit for keeping stacktraces in place. Important is if it was -n -k1,1
it would only sort the log files by hour as colon is non-numeric. Apart from that -n
speeds up numeric comparison so we would like to have it anyway.-k1.1,1.2
(first and second digit of hour) -k1.4,1.5
(first and second digit of minutes) and so on. The first digit before the dot is always '1' as it points to the first column of the file line (which in our case is time). Shortly it's -kA,B
where A
and B
are column positions in a given line (by default lines are delimited by blanks). Format of A and B used is .. Keep in mind that whenever there is a non-numeric character between A
and B
everything after it will be ignored in comparison if -n
used.-s
disables default behaviour which is: whenever keys by which comparison is being done are the same full string comparison of the lines is done. We don't want that to preserve original log entries order. Not sure if it's necessary with -m
though.You should use the merge
, stable
, ignore-leading-blanks
, numeric-sort
, and an easily sortable datetime format (such as yyyyMMddHHmmssSSS) in your log files.
So, I changed your log format to be more easily sortable, resulting in sort -bsnm log1 log2
:
$ cat -n log1 log2 && sort -m -b -n -s log1 log2
1 114818825 [main] INFO org.hibernate.cfg.Environment - HHH000206 hibernate.properties not found
2 114855784 [main] INFO o.h.tool.hbm2ddl.SchemaUpdate - HHH000396 Updating schema
1 114835377 [qtp1484319352-19] ERROR c.w.b.c.ControllerErrorHandler -
2 org.springframework.beans.TypeMismatchException Failed to convert value of type 'java.lang.String' to required type 'org.joda.time.LocalDate'; nested exception is org.springframework.core.convert.ConversionFailedException Failed to convert from type java.lang.String to type @org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat org.joda.time.LocalDate for value '[2013-03-26]'; nested exception is java.lang.IllegalArgumentException Invalid format " [2013-03-26]"
3 at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java68) ~[spring-beans-3.2.1.RELEASE.jar3.2.1.RELEASE]
4 at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java45) ~[spring-beans-3.2.1.RELEASE.jar3.2.1.RELEASE]
5 at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java595) ~[spring-context-3.2.1.RELEASE.jar3.2.1.RELEASE]
6 at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java98) ~[spring-web-3.2.1.RELEASE.jar3.2.1.RELEASE]
7 at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java77) ~[spring-web-3.2.1.RELEASE.jar3.2.1.RELEASE]
8 at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java162) ~[spring-web-3.2.1.RELEAS
9
114818825 [main] INFO org.hibernate.cfg.Environment - HHH000206 hibernate.properties not found
114835377 [qtp1484319352-19] ERROR c.w.b.c.ControllerErrorHandler -
org.springframework.beans.TypeMismatchException Failed to convert value of type 'java.lang.String' to required type 'org.joda.time.LocalDate'; nested exception is org.springframework.core.convert.ConversionFailedException Failed to convert from type java.lang.String to type @org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat org.joda.time.LocalDate for value '[2013-03-26]'; nested exception is java.lang.IllegalArgumentException Invalid format " [2013-03-26]"
at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java68) ~[spring-beans-3.2.1.RELEASE.jar3.2.1.RELEASE]
at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java45) ~[spring-beans-3.2.1.RELEASE.jar3.2.1.RELEASE]
at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java595) ~[spring-context-3.2.1.RELEASE.jar3.2.1.RELEASE]
at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java98) ~[spring-web-3.2.1.RELEASE.jar3.2.1.RELEASE]
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java77) ~[spring-web-3.2.1.RELEASE.jar3.2.1.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java162) ~[spring-web-3.2.1.RELEAS
114855784 [main] INFO o.h.tool.hbm2ddl.SchemaUpdate - HHH000396 Updating schema
As said in @Magoo's answer, the way your logs' datetime is currently formatted is hard to sort.
Nope - can't be done with a simple command IMMHO.
But - here's a script to do it (it was a challenge...)
@ECHO OFF
SETLOCAL
:: First log to tempfile
COPY /y mylog.txt "%temp%\combinedlogs.tmp" >NUL
(
FOR /f "delims=" %%i IN (mylog2.txt) DO (
SET line=%%i
ECHO %%i|FINDSTR /b /r "[012][0-9]:[0-5][0-9]:[0-5][0-9]\.[0-9][0-9][0-9]" >NUL
IF ERRORLEVEL 1 (
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO(!stamp:~0,12!!count!!line!
ENDLOCAL
SET /a count+=1
) ELSE (
SET /a count=100
ECHO %%i
SET stamp=%%i
)
)
)>>"%temp%\combinedlogs.tmp"
(
FOR /f "delims=" %%i IN ('SORT "%temp%\combinedlogs.tmp"') DO (
SET line=%%i
SETLOCAL enabledelayedexpansion
IF "!line:~12,1!"==" " (ECHO(%%i) ELSE (ECHO(!line:~15!)
ENDLOCAL
)
)>combinedlogs.txt
DEL "%temp%\combinedlogs.tmp" /F /Q
Copy the first log with all-timestamped entries to a tempfile
Process the second file by
Tempfile thus is
Timestamp1 line1 from file1
..
Timestampn linen from file1
timestampA line1 from file2 with timestamp
timestampA100 UNtimestamped line2from file2
timestampA101 UNtimestamped line3from file2
timestampB line4 from file2 with timestamp
timestampB100 UNtimestamped line5from file2
timestampB101 UNtimestamped line6from file2
...
Sorting the result and reprocessing
A line with a non-space in the 13th character is an untimestamped line from the second file, so
Done!
Here's one way to do it in a bash shell with simple merging of the files (rather than expensive resorting - as log files are already sorted). This is important for huge files in the hundreds of megabytes or more, as often is the case with real world log files.
This solution assumes that there are no NUL bytes in your logs, which is true for every log file that I've come across, with various character sets.
The basic idea:
sort -m
on the replaced files to merge themAs the first step is done multiple times, I've given it an alias:
alias a="awk '{ if (match(\$0, /^[0-9]{2}:[0-9]{2}:[0-9]{2}\\./, _))\
{ if (NR == 1) printf \"%s\", \$0; else printf \"\\n%s\", \$0 }\
else printf \"\\0%s\", \$0 } END { print \"\" }'"
Here's the command that performs all 3 steps:
sort -m <(a myLog1.txt) <(a myLog2.txt) | tr '\0' '\n'
For more, see https://superuser.com/a/838446/125379