6

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/… Commented Dec 16, 2018 at 23:39
  • I doubt that the standard appenders would do this, but you can always write your own appender. Commented 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. Commented Dec 17, 2018 at 0:00

3 Answers 3

9

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

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

Comments

6

Try below conversion pattern

ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

Comments

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

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.