logback Maskpattern for Email - java

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

Related

How to print event level in lowercase using log4j12?

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.

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".

Apache Digester Exception: Property ... can't be set

I have a problem with Apache Digester 3.2 I hope you can help me with:
The XML I have to parse contains the following lines (and much more):
<CountryName
code = "GFR"
name = "Germany"
IsTerritory = "False"
ProfileURL = "germany.doc"/>
The rules for the digester are given by another XML-file:
<pattern value="CountryName">
<object-create-rule classname="model.CodeNamePair"/>
<set-properties-rule/>
<set-next-rule methodname="addCountry"/>
</pattern>
This should create an Object of CodeNamePair (which contains a String 'code' and a String 'name', just like in the XML above.
The next method 'addCountry' is (hopefully) not relevant for this problem which follows now:
The digester is not able to parse this part. It throws a NoSuchMethodException with message:
"java.lang.NoSuchMethodException: Property IsTerritory can't be set"
Although I don't want to parse the IsTerritory property.
Do you know if (and how) I will be able to ignore this property?
Already now: Thank you very much (I hope my question is not written too complicated)
Try
<set-properties-rule>
<ignore attr-name="IsTerritory" />
</set-properties-rule>
instead of
<set-properties-rule/>
(Not tested)

How do I correctly iterate through xml using Java?

I have an XML file starting:
<?xml version="1.0"?>
<results>
<result id="0001">
<hometeam>
<name>Dantooine Destroyers</name>
<score>6</score>
</hometeam>
<awayteam>
<name>Wayland Warriors</name>
<score>0</score>
</awayteam>
</result>
<result id="0002">
<hometeam>
<name>Dantooine Destroyers</name>
<score>3</score>
</hometeam>
<awayteam>...
and in a java file:
if(event.isStartElement()){
if(event.asStartElement().getName().getLocalPart().equals(HOME)){
System.out.println("In hometeam"); // for testing purposes
event = eventReader.nextEvent(); // I expect <name> element
if(event.isStartElement()){ // <------------ FALSE
if(event.asStartElement().getName().getLocalPart().equals(NAME)){....
I'd expect this if statement to be true for the <name> element but if I stick in a System.out.println(event.isStartElement()) I get FALSE....
Also event.getEventType() returns XMLEvent.CHARACTERS which I don't understand... Can anybody see why?
Feel free to make edits to tags/title and question if necessary.
Characters means that next part of XML are well - characters (in your case newline and indentation) - low level parser is unqualified to discardthem for you. it delivers just raw events. It's your work to proces structure correctly.
That .nextEvent() call is probably bringing in the whitespace between <hometeam> and <name>. Note that in XML, all character data between tags (even if it's whitespace or newlines) is also accessible from the API.
You can test this by printing the element.
You don't normally see that whitespace with DOM-based APIs (or you can easily ignore it) but with event-driven APIs (like SAX or StAX) you have to ignore it.

Categories

Resources