Correctly aligning compound / compound expression in Log4j template template?
I want my log to look like this:
ClassName.methodName() - just did something
ClassName.methodNameLonger() - just did something else
I know you can properly justify the method name with %-17M
, but I get this:
ClassName.methodName () - just did something
ClassName.methodNameLonger() - just did something else
Is there a way to justify multiple elements as one block so that the padding only appears at the very beginning?
I'm not sure if it's possible to look at the link: https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html
Additional Information
If I try to justify the class name correctly, I get:
ClassName . methodName() - just did something
ClassName .methodNameLonger() - just did something else
ClassNameLonger.methodNameLonger() - just did something else
which is okay, but nevertheless it is not capable of treating multiple fields (including the literal ()
) as a contiguous unit that should be justified.
source to share
As mentioned in another answer, Log4j doesn't support this.
However, logback does.
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender" scan="true">
<encoder>
<pattern>%-5level %20(%logger{0}.%method\(\)) - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Full documentation here:
http://logback.qos.ch/manual/layouts.html#Parentheses
Brackets are special
In logback, the parentheses in the template line are treated as markers for grouping. Thus, it is possible to group a sub-task and apply formatting directives to that sub-matrix. Since release 0.9.27
, logback supports conversion compound words such as %replace
which can convert submatrices.
For example, the template
%-30(%d{HH:mm:ss.SSS} [%thread]) %-5level %logger{32} - %msg%n
will group the output generated by the subcategory %d{HH:mm:ss.SSS} [%thread]
so that it will be properly padded if less than 30 characters.
If without grouping the output was
13:09:30 [main] DEBUG c.q.logback.demo.ContextListener - Classload hashcode is 13995234
13:09:30 [main] DEBUG c.q.logback.demo.ContextListener - Initializing for ServletContext
13:09:30 [main] DEBUG c.q.logback.demo.ContextListener - Trying platform Mbean server
13:09:30 [pool-1-thread-1] INFO ch.qos.logback.demo.LoggingTask - Howdydy-diddly-ho - 0
13:09:38 [btpool0-7] INFO c.q.l.demo.lottery.LotteryAction - Number: 50 was tried.
13:09:40 [btpool0-7] INFO c.q.l.d.prime.NumberCruncherImpl - Beginning to factor.
13:09:40 [btpool0-7] DEBUG c.q.l.d.prime.NumberCruncherImpl - Trying 2 as a factor.
13:09:40 [btpool0-7] INFO c.q.l.d.prime.NumberCruncherImpl - Found factor 2
with grouping "%-30()"
it will be
13:09:30 [main] DEBUG c.q.logback.demo.ContextListener - Classload hashcode is 13995234
13:09:30 [main] DEBUG c.q.logback.demo.ContextListener - Initializing for ServletContext
13:09:30 [main] DEBUG c.q.logback.demo.ContextListener - Trying platform Mbean server
13:09:30 [pool-1-thread-1] INFO ch.qos.logback.demo.LoggingTask - Howdydy-diddly-ho - 0
13:09:38 [btpool0-7] INFO c.q.l.demo.lottery.LotteryAction - Number: 50 was tried.
13:09:40 [btpool0-7] INFO c.q.l.d.prime.NumberCruncherImpl - Beginning to factor.
13:09:40 [btpool0-7] DEBUG c.q.l.d.prime.NumberCruncherImpl - Trying 2 as a factor.
13:09:40 [btpool0-7] INFO c.q.l.d.prime.NumberCruncherImpl - Found factor 2
The last form is easier to read.
If you need to treat a parenthesis character as a literal, it must be escaped by preceding each parenthesis with a backslash. As in \(%d{HH:mm:ss.SSS} [%thread]\)
,.
source to share