log4j pattern %X and what property to assign to it - log4j

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

Related

Use custom parameters in JSON Layout [Log4j 2]

I'm confused about the meaning of property substitution, lookups and layout parameters in Log4j 2. The documentation mentions that JSON layout supports custom fields. However it doesn't seem to support conversion patterns like %d{ISO8601}, %m, %l and the like. it does however support Lookups.
Thus when I define in the xml:
<JsonLayout complete="false" compact="false">
<KeyValuePair key="#timestamp" value="%d{ISO8601}" />
<KeyValuePair key="message" value="%message" />
<KeyValuePair key="process.thread.name" value="%tn" />
</JsonLayout >
As output I simply get the strings %d{ISO8601}, %message... instead of the values.
What I'm trying to achieve is a JSON layout where I can include parameters similar to Pattern Layout where I simply write <pattern>%d %p %C{1.} [%t] %m%n</pattern> to get what I want. Or, alternatively, should I use the Pattern layout and stitch together a string in JSON Format, making use of the Pattern Layout's JSON encoding %enc{%m}{JSON}?
The GelfLayout currently supports a messagePattern attribute that will format just the message field in the JSON using the patternLayout. I have planned to add this to the JSONLayout as well but have not done it yet. There is a new JsonTemplateLayout that is in the final stages of being merged into Log4j 2 that will also support this. You could either work from the current pull request to get the Layout or wait for the Log4j 2.14.0 release when likely both options will be available.

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

Limit message length in Log4J

I'm working on an application that uses Log4J for logging. Occasionally, the application creates log messages that are very long, > 10000 characters. This causes performance problems when the log is displayed in a Console window in Eclipse.
I want to limit the maximum message length, truncating long messages to the maximum length. How can I do that with Log4J?
You can use a PatternLayout with a ConversionPattern that has a 'maximum width' format modifier on the message conversion character.
Example:
%r [%t] %-5p %c %x - %.10000m%n
limits message size to 10000 characters.
If someone is looking for a maximum width, that is removing the last characters and not the first (like i did) here is an example:
%r [%t] %-5p %c %x - %-0.-10000m%n
limits the message size to 10000 characters and removes everything on the end that is longer.
source: https://issues.apache.org/jira/browse/LOG4J2-926

log4j first line was not shown in proper alignment

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

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)

Resources