Skip to content

Instantly share code, notes, and snippets.

@holograph
Last active June 8, 2020 19:29
Show Gist options
  • Save holograph/ddd6ac086a26afa77f28c81470d16761 to your computer and use it in GitHub Desktop.
Save holograph/ddd6ac086a26afa77f28c81470d16761 to your computer and use it in GitHub Desktop.
Java 8 vs Scala
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;
}
}
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;
}
}
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
}
}
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;
}
}
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());
}
}
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;
}
}
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);
}
}
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