Giter Site home page Giter Site logo

demo-log-masking-spring-boot's Introduction

Log masking using logback appender with PatternLayout

Use cases

  1. masks the entire value for some fields
  2. masks all except last 4 characters for some other fields

I came up with this pattern

(?<=(customerName)=).*?(?=(,|\))) | (?<=(contactNumber|customerIdentifier)=).*?(?=[\S\s]{0,4}(,|\))). This pattern consists of two parts mirroring the two use cases described above,

  1. (?<=(customerName)=).*?(?=(,|\))) matches the entire values for customerName
  2. (?<=(contactNumber|customerIdentifier)=).*?(?=[\S\s]{0,4}(,|\))) matches the values except last four characters for contactNumber and customerIdentifier

Specify the fields in logback.xml. MaskingPatternLayout will fill in the fields from logback.xml and compile the pattern. Then for each log, it will match the pattern on the log and replace all the matches with ***.

Notes

  • No whitespace after the field name, before =
    • e.g. contactNumber =99998888 -> contactNumber =99998888
    • how to add? (customerName)=) -> (customerName)\s?=) or (customerName)=\s{0,5) for up to 5 whitespaces
    • watch out for performance penalty if the upper limit is too large
  • Leading and trailing whitespace are included in the matched result
    • e.g. customerIdentifier=S1234567D<whitespace> -> customerIdentifier=***67D<whitespace>
    • excluding them might impose some performance penalty
  • ) and , identifies where the value ends. Pattern matching will trip if the value contains any of these characters.
    • e.g. customerName=Ah, Boon Choo -> customerName=***, Boon Choo
    • how to add other identifiers? (,|\)) -> (,|\)|:|$) adds : and end-of-line ($)

Demo using vanilla Java

String patternString = "(?<=(customerName)=).*?(?=(,|\\)))|(?<=(contactNumber|customerIdentifier)=).*?(?=[\\S\\s]{0,4}(,|\\)))";
Pattern pattern = Pattern.compile(patternString, Pattern.MULTILINE);

String message = "Customer(id=1, customerIdentifier=S1234567D, customerName=Ah Beng Choo, gender=Male, contactNumber=99998888)";
Matcher matcher = pattern.matcher(message);
System.out.println(matcher.replaceAll("***"));
// prints Customer(id=1, customerIdentifier=***567D, customerName=***, gender=Male, contactNumber=***8888)

message = "Customer(id=1, customerIdentifier=S1234567D , customerName=Ah, Beng Choo, gender=Male, contactNumber =99998888";
matcher = pattern.matcher(message);
System.out.println(matcher.replaceAll("***"));
// prints Customer(id=1, customerIdentifier=***67D , customerName=***, Beng Choo, gender=Male, contactNumber =99998888)

Reference

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.