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
3
  • see if this helps stackoverflow.com/questions/115008/…
    – newbie
    Dec 16, 2018 at 23:39
  • I doubt that the standard appenders would do this, but you can always write your own appender.
    – Stephen C
    Dec 16, 2018 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, 2018 at 0:00

3 Answers 3

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

4

Try below conversion pattern

ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.