How to print event level in lowercase using log4j12? - java

Is there any possibility to print event levels to log in lower case in log4j 2?
Like %level{lowerCase=true} in pattern in Log4j 2.
Can I process result string somehow, or use custom renderer?

You have to set a pattern to the appropriate appender in the log4j.properties file.
For instance, with ConsoleAppender:
log4j.appender.ConsoleAppender.layout.ConversionPattern=%d [%p{lowerCase=true}] %c{1} - %m%n
You can check the output here.

Related

logback Maskpattern for Email

Currently i am using the logback framework and patterns in my logs , but I need to mask the PII information for email-id. I need The regular expression which helps to Mask my email Address from the logs, my requirement was to mask only 30% of the email id.
Since i am using the Logback we can use the logback and masking pattern
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<maskPattern>(?<=.{3}).(?=[^#]*?#)</maskPattern> <!--mask email-->
</encoder>
</appender>
For example : testing#test.com
Expected Result:tes***#test.com
I tried Using Different Regular expressions from google and none of them works with the logback framework. For example when i use the below regular expression in logback.xml was not able to parse it or compile it. i have taken reference as :
https://howtodoinjava.com/logback/masking-sensitive-data/
<maskPattern><![CDATA[/(?<=.{3}).(?=[^#]*?#)]]></maskPattern>
<maskPattern><(?<=.{3}).(?=[^#]*?#)></maskPattern>
Examples: Can any body please provide me the correct regular expression or pattern, below are the regular expression or pattern i tried none of them works.
1) (?<=.{3}).(?=.*#)
2) (?<=.{2}).(?=[^#]*?#)
3) ([^#]{4})[^#]*(.+)
4) \\b(\\w)[^#]+#\\S+(\\.[^\\s.]+)
please check the screen shot below.
enter image description here
ref: https://stackoverflow.com/questions/33100298/masking-of-email-address-in-java

Log4j2 Different color per logger

In my project I use Log4j 2.17.1.
I would like to format the output so that the class names have different colors.
public class MyClass {
Logger LOGGER = LogManager.getLogger(MyClass.class.getSimpleName());
//...
LOGGER.debug("Some Information");
}
public class MySecondClass {
Logger LOGGER = LogManager.getLogger(MySecondClass.class.getSimpleName());
//...
LOGGER.debug("Another Output");
}
In my example, I want the two classes to appear in different (perhaps random) colors (instead of yellow as in the example).
My current pattern layout:
%highlight{${LOG_LEVEL_PATTERN:-%5p}}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=green, DEBUG=green bold, TRACE=blue} : %style{%logger{36}:}{blue} %msg%n
Is this possible with Log4j?
Per defeault log4j writes simple text files with no fancy formatting applied (FileAppender, ConsoleAppender).
I have never done this, but I believe you can add ANSI escape sequences using a suitable formatting pattern, then enjoy the colours when the log output is sent to a terminal capable of understanding these sequences.

log4j2: custom formating for pattern parameter

Usually logging looks like this:
Duration duration = ...;
log.info("Duration: {}", duration);
But what do if you need custom formatting?
if (log.isInfoEnabled()) {
String value = duration.toSeconds() + "." + duration.toMillis();
log.info("Duration: {}", value);
}
In this case code is complicated. I'm looking for a way to do simple logging as in first sample but to be able to customize value formatting using some mechanism, e.g, define a format function for given type:
LogSystem.install(Duration.class, duration -> {
return String.format("%d.%d", duration.toSeconds(), duration.toMillis();
});
Questions:
does log4j 2.x have such or similar feature?
may be it exists in other logging libraries?
if no, can if (log.isInfoEnabled()) { ... } be simplified somehow?
does log4j 2.x have such or similar feature?
Yes, you can use
log.info("Duration: {}.{}", duration.toSeconds(), duration.toMillis());
You can pass as many parameters as you like. If you use parameterized logging, you don't need to check log.isInfoEnabled().
may be it exists in other logging libraries?
SLF4j also supports parameterized logging.
Yes, you can do it using the properties file.Set policies pattern in your log4j2.properties file like:
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ABClog/ABCapplication.log
appender.rolling.filePattern = ABClog/ABCapplication.%d{dd-MMM}.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = { [%-5level %d{dd-MM-yyyy hh:mm:ss.SSS } ] [%msg] }%n
After setting the above configuration in your log4j2.proerties file, you will get your desired output, even using properties file concept you can add extra info too e.g. Correlation-ID
log.info("hello")
Log4j2 document for more pattern style: https://logging.apache.org/log4j/2.x/manual/layouts.html

Multiple regular expression replacements log4j2 xml pattern tag

Hi I am configuring log4j2 via an xml file. I have set up an appender and its is logging properly. I am having issue configuring two regular expressions to replace text in the %message variable of my log.
I am logging messages in my java code like this:
logger.info("{ 'name':'person', age:'42' }");
I am sending these logs to Kafka and want to replace all { or } with "" and all ' with ".
The current pattern I am using looks like this:
<pattern>{ "logTimestamp":"%date{ISO8601}", %replace{%replace{%message{nolookups}}{\\"|\\'|"}{'}}{{|}}{},"host":"${hostname}" }</pattern>
However this is not working and I am getting the following parsed message as a result of the replacement:
{ "logTimestamp":"2017-03-27T11:11:17,247", %replace}{"}{'},"host":"hostname" }
What is the proper way to match and replace two patterns with log4j2 %replace?
You need to nest one %replace statement inside another.
Try this:
%replace{ %replace{%msg}{&apos;}{"} }{\{|\}}{""}
The result is:
"" "name":"person", age:"42" ""
I hope it helps.

Using log4j2, how to log key value pairs

I need to create logs with key value pairs as below. Is there any support in PatternLayout to do this for the static fields in a thread like log_level, class_name, event_id etc with the log4j2.xml.
Sample log:
2014-06-18 11:57:46,719 log_level="INFO"
class_name="com.abc.dgl.App:main(158)" name="Application start event" event_id="b88f7ea0-4cb1-438f-a728-ac7c2bdac578" app="Test App" severity="info" action="loaded sfor file processing" desc="props was read and loaded" result="success" reason="abc" transaction_id="b88f7ea0-4cb1-438f-a728-ac7c2bdac578"
Yes, this is possible.
You can either use a MapMessage, which is supported by the map (or K) conversion pattern of PatternLayout: an example layout pattern would be "%-5p [%t]: %m %map%n".
Logging a MapMessage looks like this: Map<String,String> myMap = getMyMap(); Logger.debug(new MapMessage(myMap));
Another way to do this is to use the ThreadContext map. This is supported by the mdc (or X) conversion pattern of PatternLayout. Example pattern: "%-5p [%t]: %m %mdc%n".
A common usage is putting a user ID in the Thread Context Map when a user logs in, and having this user ID shown in all log messages emitted by that thread until the user logs out.
Instead of logging the whole map you can also log specific keys only by specifying the key in the layout pattern: e.g. "%-5p [%t]: %m %mdc{userID}%n".

Categories

Resources