How to display Just D, I, E, F instead of DEBUG, INFO, ERRO, FATAL in log4j - 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

Related

How to filter out/ommit specific pattern in linux logs

I'm looking through massive log files on linux server. Is there any command to filter out (not to show) specific pattern?
For example if my log messages begin with A-1111, B-1111, C-1111 etc. Could I somehow filter so that logs with pattern A-XXXX wouldn't be shown, but all other patterns still would be present?
I' not really advanced with linux commands, thanks.
This is one of the reasons why grep has been invented. However, from your description I can't find out how the pattern looks like:
[A-C]-1111 : letters from A to C, followed by 1111
[A-C]-[0-9][0-9][0-9][0-9] : letters from A to C, followed by four digits
[A-Z]... : letters from A to Z, ...
...
Your first reaction: "This is not what I want: grep filters those expressions and shows them, and I want the opposite, I want not to show them!"
This can be solved by the -v flag in grep:
I have created a file with following content:
A-1111
B-1111
C-1111
A-2222
Whatever
This is the result of grep -v "[A-C]-1111" logfile.log:
A-2222
Whatever

Unexpected ellipsis in syslog or splunk message

I am logging data to Splunk via log4j and a SyslogAppender. Sometimes the information shows up in Splunk with ellipsis (...) instead of the actual text with some odd spacing. When I log the same event to a RollingFileAppender, it logs normally. Would anyone know why this is happening and how to resolve it?
Thank you!
Example:
InboundTxnDate: 20130926 16:53:14:475
Out...
...boundTxnTypeCode: UNK
This is how syslog appenders work, they split log message, if it is bigger than 1019 bytes. When log message is split, this message will end with ellipsis and next message starts with ellipsis. Limit of message (1019 bytes) is hardcoded and cannot be changed by no configuration in log4j. Look at RFC 3164 and see source code of SyslogAppender
Would anyone know why this is happening
Ondřej Benkovský answered that very well and I have nothing to add.
how to resolve it?
If the split lines are part of the same event in Splunk, you can join them by editing $SPLUNK_HOME/etc/system/local/props.conf and adding a stanza like this:
[<spec>]
SEDCMD-join_log4j_syslog_lines=s/\.\.\.[\r\n]+\.\.\.//g
That will join the lines at index time and remove the ellipses.
Note that you'll need to change <spec> as detailed in the document for props.conf.
You can use the regex search command to make sure it's working:
| regex "\.\.\.[\r\n]+\.\.\."

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

Log4J print empty line to logfile

In java you could use System.out.println() to print a blank line but how does it work with log4j in a logfile?
There I also want to have one or more blank lines inside the logfile.
I already read the follwing:
log4j how to append blank line
But this is not really helpful because with
logger.debug("\n");
logger.debug("");
you do not print a message but other information like time and so on (the layout of the logger) are still stored inside the logfile. But I just want to have a total empty line.
Can anyone help me please?
Log4j is not designed to write blank lines.
You cannot find this option in the logger, because the logger is independent of his appenders who write to a file or the console or something different.
I think you need to create your custom FileAppender which checks the Logging Message before the writing. If your message.equals("\n"); you append a blank line without the layout by accessing the file by your own and skip the normal logging with the layout. then you can use Logger.debug("\n");
Also it is a bad practice to add blank lines. It is similar to split your Log-Message over several lines. Your want all your Logmessage in one line each, so they are easy to parse for LogViewer-Tools like chainsaw or OtrosLogViewer One exception are Stacktraces.
Create your own appender with empty layout and a logger that uses that appender. Then write the blank line using that logger. Make certain to set the additivity attribute for logger to false so that the root logger doesn't write the message using the formatting of its appenders.

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