-
-
Save monkey-codes/d57ed9131e929b1e18aa50d03b3d953a to your computer and use it in GitHub Desktop.
PII Masking Logger decorator and usage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object Logging { | |
| inline fun <reified T : Any> loggerFor(): Logger = PIIMaskingLogger(LoggerFactory.getLogger(T::class.java)) | |
| fun loggerForCompanion(c: Any): Logger = PIIMaskingLogger(LoggerFactory.getLogger(c.javaClass.enclosingClass)) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class PIIMaskingLogger(val logger: Logger) : Logger by logger { | |
| override fun error(format: String?, arg: Any?) = logger.error(format, mask(arg)) | |
| //... | |
| override fun warn(format: String?, arg: Any?) = logger.error(format, mask(arg)) | |
| // ... | |
| override fun info(format: String?, arg: Any?) = logger.error(format, mask(arg)) | |
| // ... | |
| private fun mask(arg: Any?): String? = when { | |
| arg == null -> arg | |
| Configuration.accept(arg::class.java) -> PIIMaskingToStringBuilder.toString(arg) | |
| else -> arg.toString() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ... | |
| val LOGGER = Logging.loggerFor<Application>() | |
| ... | |
| val customer: Customer = Customer(...) | |
| LOGGER.info("a template message with PII {}", customer) | |
| ... | |
| //will produce a log message like: | |
| //a template message with PII Customer(id=c91d4517-72d8-498d-a089-98a341195410, name=***, passportNumber=*** |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment