Adapter Design Pattern
This file contains 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
package com.knoldus | |
import org.apache.log4j.BasicConfigurator | |
object Client extends App { | |
BasicConfigurator.configure() | |
val logger : LoggerHelper = LoggerAdapter.getLogger(this.getClass.getName) | |
logger.info("Log Contains IP address: 127.0.0.1") | |
logger.debug("UserName: jainnancy trying to sign in") | |
logger.error("Password: abxyz is wrong ") | |
} |
This file contains 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
package com.knoldus | |
object LoggerAdapter extends LoggerHelper { | |
var scrambledLogger : ScrambledLogger = _ | |
override def info(msg : String) = scrambledLogger.scrambledInfo(msg) | |
override def debug(msg : String) = scrambledLogger.scrambledDebug(msg) | |
override def error(msg : String) = scrambledLogger.scrambledError(msg) | |
def getLogger(s : String) : LoggerHelper = | |
{ | |
scrambledLogger = new ScrambledLogger(s) | |
LoggerAdapter | |
} | |
} |
This file contains 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
package com.knoldus | |
import org.apache.log4j.BasicConfigurator | |
trait LoggerHelper { | |
def info(msg : String) | |
def debug(msg : String) | |
def error(msg : String) | |
} |
This file contains 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
package com.knoldus | |
import org.apache.log4j.Logger | |
class ScrambledLogger(name : String) | |
{ | |
private val regex = "\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b" | |
private val password = "Password: " | |
private val userName = "UserName: " | |
private val logger = Logger.getLogger(name) | |
def scrambledInfo(message : String) = logger.info(scramble(message)) | |
def scrambledDebug(message : String) = logger.debug(scramble(message)) | |
def scrambledError(message : String) = logger.error(scramble(message)) | |
private def scramble(message : String) = scrambleUsername(scrambleIp((scramblePassword(message)))) | |
private def scrambleUsername(message : String) = { | |
if(message.contains(userName)) { | |
val index = message.indexOf(userName) + userName.length() | |
val textStartedPassword = message.substring(index) | |
message.substring(0, index) + "X" + textStartedPassword.substring(textStartedPassword.indexOf(" ")) | |
} | |
else { | |
message | |
} | |
} | |
private def scrambleIp(message : String) = message.replaceAll(regex, "XXX.XXX.XXX.XXX") | |
private def scramblePassword(message : String) = { | |
if(message.contains(password)) { | |
val index = message.indexOf(password) + password.length() | |
val textStartedPassword = message.substring(index) | |
message.substring(0, index) + "X" + textStartedPassword.substring(textStartedPassword.indexOf(" ")) | |
} | |
else { | |
message | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment