log4j first line was not shown in proper alignment - log4j

I am facing some problem regarding log message.
When I generate log report, the first line was not shown in proper alignment but rest of the log message was in proper order.
I checked log patterns, but didn't find any clue to the issue.
Can anybody suggest me the how I can resolve this issue?
Output:
INFO|------------------------------- Start Control Information --------------------------
INFO|***********
INFO|**********************
INFO|***** ************************
INFO|Doc***** Version : 6.7.0004.0217 Win64.SQLServer
INFO|------------------------------ End Control Information -----------------------------
INFO|
INFO|******************
INFO|*************** :
INFO|Version identifiers :
INFO|***********
here , first line(INFO|------------------------------- Start Control Information --------------------------
Only the first line is not properly aligned.

Logger messages are generated based on the pattern layout of log4j.properties. If you wanted to do alignmnet of your logger message, you have use pattern layout as follows.
log4j.rootLogger=DEBUG, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

Related

How to display Just D, I, E, F instead of DEBUG, INFO, ERRO, FATAL in log4j

We are trying to optimize the size of files generated by log4j2. So I was thinking if we can reduce the number of characters that get printed on the final .log file. Below is the current pattern layout
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss,SSS} %p [%F] %m%n
Which generates the below logs
07:37:31,416 DEBUG [FileUtils.java] bla bla bla
What I want is the below to get displayed instead
07:37:31,416 D [FileUtils.java] bla bla bla
What can I try to resolve this?
Have a look at the log4j documentation: https://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout
It mentions two ways to achieve your desired output. Include one of the following strings in you pattern configuration instead of the %p:
%level{WARN=W, DEBUG=D, ERROR=E, TRACE=T, INFO=I}
%level{length=1}
The first one will map the levels explicitly, while the second will cut the label length to 1. Either one will work. Note, that %p is just short-hand notation for %level.
Your full configuration property will look as follow using the second option with the abbreviation %p instead of %level:
log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss,SSS} %p{length=1} [%F] %m%n

Is it possible to have a java object in log4j Conversion pattern?

Its multi tenancy application and generates lots of logs.
I want to see tenant information in an individual log statement.
I have the tenant information in my thread context.
How can i configure log4j to add tenant information to log statements by default.
I saw Conversion pattern says the pattern of log4j messages like %d [%t] %-5p %c - %m%n.
It didnt helped, not able to print thread context in it.
Say CurrentThread.getTenantName() gives me the current tenant, how could add it to log4j.
In log4j , patterns parsed by PatternParser
You can write your own parser by overriding it and parse custom literal like %i where "i" will denote tenant id in your case.
Please refer below blog for creating custom literal and parser
http://fw-geekycoder.blogspot.in/2010/07/creating-log4j-custom-patternlayout.html

What are the format characters using in log4j and explain their usage and give me some example?

I have the below entry in the log4j config xml. What is the significant of '%p', '%C;%L', '%m'. What is stands for those characters? What other characters, we can use in log4j? Explain their usage..
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="INSERT INTO LOGS (user_id, dated, msg_level, class, message)
VALUES ( '%X{MESSAGE_ID}','%d{ISO8601}','%p', '%C;%L', '%m' )" />
</layout>
Some of the main conversion characters are
p --- Used to output the priority of the logging event.
C --- Used to output the fully qualified class name of the caller issuing the logging request.
L --- Used to output the line number from where the logging request was issued.
m --- Used to output the application supplied message associated with the logging event.
Using number with any conversion character, for example %4p means
the priority of the logging event should be left justified to a width of four characters.
Besides this conversions , there are other patterns also .
You can see detail about them here.

Why ChainSaw cannot display logger name?

I am using the latest ChainSaw developer snapshot
I'm trying get logs from a file with logs in pattern :
[%d] [%t] [%c] [%m]%n
and I don't know why it doesn't work - whole log is read as %m. When I use pattern without %c every log is displayed correctly.
Try this pattern instead
[TIMESTAMP] [THREAD] [LOGGER] [MESSAGE]
(I'm assuming your logs also have those "[]", otherwise remove them)

log4j pattern %X and what property to assign to it

i am trying to use a log viewer (doesn't matter which one) to parse my log files.
my log4j pattern is this.
%p [%t] (%C{1}:%M():%L) %d{dd/MM/yyyy-HH:mm:ss,SSS} S:%X{serviceType} N:%X{requestID}- %m%n
the log viewers (at least the open source ones) need you to implement a pattern so they will be able to read the file.
for example:
for the log4j pattern: %p [%t] (%C{1}:%M():%L) %d{dd/MM/yyyy-HH:mm:ss,SSS} - %m%n
the log viewer pattern would be:
pattern= pattern=LEVEL [THREAD] (CLASS:METHOD():LINE) TIMESTAMP - MESSAGE
the example works well.
but i have not been able to parse the %X property in any way. i have seen there are property types NDC and PROP(key) but i seem to either miss use them or they are not related to the %X
so the question is how to implement the pattern so it will read the %X parameter.
thanks.
Ok, i think i see the problem.
Your application use the log4J MDC since it use the %X in the pattern layout. Your log viewer seems to support only NDC.
log4j pattern layout for NDC is %x (lowercase).
If you have control on the application, you have to change MDC -> NDC and modifiy the log4j.xml to use %x instead of %X. That may be a big task if the app is huge...
Another solution would be to find a log viewer that support MDC(%X)
I tried to look around for the PROP(key), but there is not much doc on it ;-(
Good luck

Resources