Last active
June 8, 2020 19:29
-
-
Save holograph/ddd6ac086a26afa77f28c81470d16761 to your computer and use it in GitHub Desktop.
Java 8 vs Scala
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
public class ClassWithLazyLogs implements LazyLogging { | |
public String getNormalizedName(Person person) { | |
info(() -> "getNormalizedName called"); | |
debug(() -> "Normalizing " + person.toString()); | |
String normalizedName = | |
person.getName().toUpperCase().trim(); | |
debug(() -> "Normalized name is: " + normalizedName); | |
return normalizedName; | |
} | |
} |
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
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class ClassWithLogs { | |
private static Logger log = LoggerFactory.getLogger(ClassWithLogs.class); | |
public String getNormalizedName(Person person) { | |
log.info("getNormalizedName called"); | |
log.debug("Normalizing " + person.toString()); | |
String normalizedName = | |
person.getName().toUpperCase().trim(); | |
log.debug("Normalized name is: " + normalizedName); | |
return normalizedName; | |
} | |
} |
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 ClassWithLogs extends Logging { | |
def getNormalizedName(person: Person): String = { | |
info("getNormalizedName called") | |
debug("Normalizing " + person.toString()) | |
val normalizedName = person.getName.toUpperCase.trim | |
debug("Normalized name is: " + normalizedName) | |
normalizedName | |
} | |
} |
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
public class IdealizedClass implements Logging { | |
public String getNormalizedName(Person person) { | |
info("getNormalizedName called"); | |
debug("Normalizing " + person.toString()); | |
String normalizedName = | |
person.getName().toUpperCase().trim(); | |
debug("Normalized name is: " + normalizedName); | |
return normalizedName; | |
} | |
} |
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
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import java.util.function.Supplier; | |
interface LazyLogging { | |
default Logger getLogger() { | |
// Can be overridden for custom logger names, delegation etc. | |
return LoggerFactory.getLogger(this.getClass()); | |
} | |
default void debug(Supplier<String> message) { | |
if (getLogger().isDebugEnabled()) | |
getLogger().debug(message.get()); | |
} | |
default void info(Supplier<String> message) { | |
if (getLogger().isInfoEnabled()) | |
getLogger().info(message.get()); | |
} | |
} | |
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
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public class LogFormatStrings { | |
private static Logger log = LoggerFactory.getLogger(ClassWithLogs.class); | |
public String getNormalizedName(Person person) { | |
log.info("getNormalizedName called"); | |
log.debug("Normalizing {}", person); | |
String normalizedName = | |
person.getName().toUpperCase().trim(); | |
log.debug("Normalized name is: {}", normalizedName); | |
return normalizedName; | |
} | |
} |
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
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
interface Logging { | |
default Logger getLogger() { | |
// Can be overridden for custom logger names, delegation etc. | |
return LoggerFactory.getLogger(this.getClass()); | |
} | |
default void debug(String message) { | |
if (getLogger().isDebugEnabled()) | |
getLogger().debug(message); | |
} | |
default void info(String message) { | |
if (getLogger().isInfoEnabled()) | |
getLogger().info(message); | |
} | |
} | |
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
import org.slf4j.LoggerFactory | |
trait Logging { | |
private val logger = LoggerFactory.getLogger(this.getClass) | |
protected def debug(message: => String): Unit = | |
if (logger.isDebugEnabled) | |
logger.debug(message) | |
protected def info(message: => String): Unit = | |
if (logger.isInfoEnabled) | |
logger.info(message) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment