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

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

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.

Azure adds timestamp at the beginning logs

I have a problem with the logs retrieving from my docker containers with Azure log analytics, all logs are retrieving well but Azure adds a date at the beginning of each line of the log, which means that an entry is created for each line and I can't analyze my logs correctly because they are divided...
For example on this image I have in the black rectangle an added date (by azure I think) and in the red rectangle the date appearing in my logs :
Also, if there is no date on a line of my logs, there is still an added date on all lines, even the empty ones
The problem is that azure cuts my log file line by line by adding a date on each line when I would like it to delimit with the dates already present in my logs files.
Do you have any solutions?
One of the solution I can think of is that, when you query the logs, you can use the replace() method to replace the redundant date(replace it with a empty string etc.). And you need to write the proper regular expression for your purpose.
A false query like below:
ContainerLog
| extend new_logEntry=replace(#'xxx', #'xxx', LogEntry)
Currently Azure Monitor for containers doesn’t support multi-line logging, but there are workarounds available. You can configure all the services to write in JSON format and then Docker/Moby will write them as a single line.
https://learn.microsoft.com/fr-fr/azure/azure-monitor/insights/container-insights-faq#how-do-i-enable-multi-line-logging

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)

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