5

I have a pattern to output to console and log file in following format:

[INFO]  17:31:12 - Checking Bla Bla Bla 
[INFO]  17:31:12 - ************* Starting *********************
[INFO]  17:31:12 - Testing Begins

is there any way to append line count so that it looks like:

[001][INFO] 17:31:12 - Checking Bla Bla Bla 
[002][INFO] 17:31:12 - ************* Starting *********************
[003][INFO] 17:31:12 - Testing Begins
Nodir Nasirov
  • 1,488
  • 3
  • 26
  • 44
  • see if this helps https://stackoverflow.com/questions/115008/how-can-we-print-line-numbers-to-the-log-in-java – newbie Dec 16 '18 at 23:39
  • I doubt that the standard appenders would do this, but you can always write your own appender. – Stephen C Dec 16 '18 at 23:56
  • You want to append the count of lines in the log file? Like literally how many lines the log file is up to that point? I'm sure there's a standard unix command that could do it, and at least one Java class that will track lines as they are read. – markspace Dec 17 '18 at 00:00

3 Answers3

7

In Log4j 1, the symbols can be seen here http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html

Line Number is %L.

In Log4j 2, the symbol is also %L

https://logging.apache.org/log4j/2.0/manual/layouts.html#Pattern_Layout

ProgrammersBlock
  • 5,974
  • 4
  • 17
  • 21
5

Try below conversion pattern

ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
MukeshKoshyM
  • 514
  • 1
  • 8
  • 16
2
Is it line number or count of log lines ordered in sequence ?

if it is line number you can use %L (Used to output the line number 
from where the logging request was issued.)

https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html