diff --git a/src/main/java/org/takes/facets/slf4j/BkLogged.java b/src/main/java/org/takes/facets/slf4j/BkLogged.java index f3e29cfba..367ca8fca 100644 --- a/src/main/java/org/takes/facets/slf4j/BkLogged.java +++ b/src/main/java/org/takes/facets/slf4j/BkLogged.java @@ -48,7 +48,7 @@ public final class BkLogged extends LogWrap implements Back { * @param back Original */ public BkLogged(final Back back) { - this(back, LogWrap.Level.TRACE); + this(back, LogWrap.DEFAULT_LEVEL); } /** @@ -66,7 +66,7 @@ public void accept(final Socket socket) throws IOException { final long started = System.currentTimeMillis(); this.origin.accept(socket); this.log( - "[%s] #accept(%s) in [%d] ms", + "[{}] #accept([{}]) in [{}] ms", this.origin, socket, System.currentTimeMillis() - started diff --git a/src/main/java/org/takes/facets/slf4j/FtLogged.java b/src/main/java/org/takes/facets/slf4j/FtLogged.java index c60ebfb25..0c35a531a 100644 --- a/src/main/java/org/takes/facets/slf4j/FtLogged.java +++ b/src/main/java/org/takes/facets/slf4j/FtLogged.java @@ -48,7 +48,7 @@ public final class FtLogged extends LogWrap implements Front { * @param front Original */ public FtLogged(final Front front) { - this(front, LogWrap.Level.TRACE); + this(front, LogWrap.DEFAULT_LEVEL); } /** @@ -64,7 +64,7 @@ public FtLogged(final Front front, final LogWrap.Level lvl) { @Override public void start(final Exit exit) throws IOException { this.log( - "[%s] #start(%s)", + "[{}] #start([{}])", this.origin, exit ); diff --git a/src/main/java/org/takes/facets/slf4j/LogWrap.java b/src/main/java/org/takes/facets/slf4j/LogWrap.java index 0e3dec834..1827e3c0e 100644 --- a/src/main/java/org/takes/facets/slf4j/LogWrap.java +++ b/src/main/java/org/takes/facets/slf4j/LogWrap.java @@ -35,8 +35,12 @@ * @version $Id$ * @since 0.11 */ -@SuppressWarnings({ "PMD.CyclomaticComplexity", "PMD.LoggerIsNotStaticFinal" }) +@SuppressWarnings("PMD.LoggerIsNotStaticFinal") public class LogWrap { + /** + * Default log level. + */ + public static final LogWrap.Level DEFAULT_LEVEL = LogWrap.Level.DEBUG; /** * Log levels. */ @@ -58,9 +62,9 @@ public enum Level { */ WARN, /** - * Severe level. + * Error level. */ - SEVERE + ERROR }; /** @@ -73,6 +77,14 @@ public enum Level { */ private final transient Logger logger; + /** + * Ctor. + * @param clazz Logger class + */ + public LogWrap(final Class clazz) { + this(clazz, LogWrap.Level.DEBUG); + } + /** * Ctor. * @param clazz Logger class @@ -87,28 +99,26 @@ public LogWrap(final Class clazz, final LogWrap.Level lvl) { * Log message. * @param format Format string * @param param Parameters - * @checkstyle CyclomaticComplexityCheck (2 line) */ public final void log(final String format, final Object... param) { - if (this.level == LogWrap.Level.TRACE - && this.logger.isTraceEnabled()) { - this.logger.trace(String.format(format, param)); - } else if ( - this.level == LogWrap.Level.DEBUG - && this.logger.isDebugEnabled()) { - this.logger.debug(String.format(format, param)); - } else if ( - this.level == LogWrap.Level.INFO - && this.logger.isInfoEnabled()) { - this.logger.trace(String.format(format, param)); - } else if ( - this.level == LogWrap.Level.WARN - && this.logger.isWarnEnabled()) { - this.logger.warn(String.format(format, param)); - } else if ( - this.level == LogWrap.Level.SEVERE - && this.logger.isErrorEnabled()) { - this.logger.error(String.format(format, param)); + switch (this.level) { + case TRACE : + this.logger.trace(format, param); + break; + case DEBUG: + this.logger.debug(format, param); + break; + case INFO: + this.logger.info(format, param); + break; + case WARN: + this.logger.warn(format, param); + break; + case ERROR: + this.logger.error(format, param); + break; + default: + throw new IllegalArgumentException("Unknown log level"); } } } diff --git a/src/main/java/org/takes/facets/slf4j/PsLogged.java b/src/main/java/org/takes/facets/slf4j/PsLogged.java index 045fad87e..b625c6714 100644 --- a/src/main/java/org/takes/facets/slf4j/PsLogged.java +++ b/src/main/java/org/takes/facets/slf4j/PsLogged.java @@ -53,7 +53,7 @@ public final class PsLogged extends LogWrap implements Pass { * @param pass Original */ public PsLogged(final Pass pass) { - this(pass, LogWrap.Level.TRACE); + this(pass, LogWrap.DEFAULT_LEVEL); } /** @@ -80,7 +80,7 @@ public Iterator enter(final Request request) throws IOException { } sbr.append(']'); this.log( - "[%s] #enter(%s) return [%s] in [%d] ms", + "[{}] #enter({}) return [{}] in [{}] ms", this.origin, request, sbr.toString(), @@ -95,7 +95,7 @@ public Response exit(final Response response, final Identity identity) final long started = System.currentTimeMillis(); final Response resp = this.origin.exit(response, identity); this.log( - "[%s] #exit(%s,%s) return [%s] in [%d] ms", + "[{}] #exit([{}],[{}]) return [{}] in [{}] ms", this.origin, response, identity, diff --git a/src/main/java/org/takes/facets/slf4j/TkLogged.java b/src/main/java/org/takes/facets/slf4j/TkLogged.java index b7de82df9..58bf62c65 100644 --- a/src/main/java/org/takes/facets/slf4j/TkLogged.java +++ b/src/main/java/org/takes/facets/slf4j/TkLogged.java @@ -48,7 +48,7 @@ public final class TkLogged extends LogWrap implements Take { * @param take Original */ public TkLogged(final Take take) { - this(take, LogWrap.Level.TRACE); + this(take, LogWrap.DEFAULT_LEVEL); } /** @@ -66,7 +66,7 @@ public Response act() throws IOException { final long started = System.currentTimeMillis(); final Response resp = this.origin.act(); this.log( - "[%s] #act() return [%s] in [%d] ms", + "[{}] #act() return [{}] in [{}] ms", this.origin, resp, System.currentTimeMillis() - started diff --git a/src/main/java/org/takes/facets/slf4j/TsLogged.java b/src/main/java/org/takes/facets/slf4j/TsLogged.java index d0adc171c..c28edc5e5 100644 --- a/src/main/java/org/takes/facets/slf4j/TsLogged.java +++ b/src/main/java/org/takes/facets/slf4j/TsLogged.java @@ -50,7 +50,7 @@ public final class TsLogged extends LogWrap implements Takes { * @param takes Original */ public TsLogged(final Takes takes) { - this(takes, LogWrap.Level.TRACE); + this(takes, LogWrap.DEFAULT_LEVEL); } /** @@ -68,7 +68,7 @@ public Take route(final Request request) throws IOException { final long started = System.currentTimeMillis(); final Take resp = this.origin.route(request); this.log( - "[%s] #route(%s) return [%s] in [%d] ms", + "[{}] #route([{}]) return [{}] in [{}] ms", this.origin, request, resp, diff --git a/src/test/java/org/takes/facets/slf4j/BkLoggedTest.java b/src/test/java/org/takes/facets/slf4j/BkLoggedTest.java index cf980e13e..eab280829 100644 --- a/src/test/java/org/takes/facets/slf4j/BkLoggedTest.java +++ b/src/test/java/org/takes/facets/slf4j/BkLoggedTest.java @@ -45,6 +45,10 @@ public final class BkLoggedTest { /** * BkLogged can log message. * @throws IOException If some problem inside + * @todo #90 we should change mocked Log4j appender to + * custom Log4jAppeneder that implemented as JUnit Rule. + * See details in CR comment here + * https://github.com/yegor256/takes/pull/89#discussion_r27354036 */ @Test public void logsMessage() throws IOException { @@ -67,7 +71,7 @@ public void accept(final Socket socket) throws IOException { public boolean matches(final Object argument) { return ((LoggingEvent) argument) .getRenderedMessage() - .matches("^.*accept.*$"); + .contains("accept"); } } ) diff --git a/src/test/java/org/takes/facets/slf4j/FtLoggedTest.java b/src/test/java/org/takes/facets/slf4j/FtLoggedTest.java index 8508147e7..330f18943 100644 --- a/src/test/java/org/takes/facets/slf4j/FtLoggedTest.java +++ b/src/test/java/org/takes/facets/slf4j/FtLoggedTest.java @@ -45,6 +45,10 @@ public final class FtLoggedTest { /** * FtLogged can log message. * @throws IOException If some problem inside + * @todo #90 we should change mocked Log4j appender to + * custom Log4jAppeneder that implemented as JUnit Rule. + * See details in CR comment here + * https://github.com/yegor256/takes/pull/89#discussion_r27354036 */ @Test public void logsMessage() throws IOException { @@ -75,7 +79,7 @@ public boolean ready() { public boolean matches(final Object argument) { return ((LoggingEvent) argument) .getRenderedMessage() - .matches("^.*start.*$"); + .contains("start"); } } ) diff --git a/src/test/java/org/takes/facets/slf4j/PsLoggedTest.java b/src/test/java/org/takes/facets/slf4j/PsLoggedTest.java index 8b359ec6f..61320a087 100644 --- a/src/test/java/org/takes/facets/slf4j/PsLoggedTest.java +++ b/src/test/java/org/takes/facets/slf4j/PsLoggedTest.java @@ -51,6 +51,10 @@ public final class PsLoggedTest { /** * PsLogged can log message. * @throws IOException If some problem inside + * @todo #90 we should change mocked Log4j appender to + * custom Log4jAppeneder that implemented as JUnit Rule. + * See details in CR comment here + * https://github.com/yegor256/takes/pull/89#discussion_r27354036 */ @Test public void logsMessage() throws IOException { diff --git a/src/test/java/org/takes/facets/slf4j/TkLoggedTest.java b/src/test/java/org/takes/facets/slf4j/TkLoggedTest.java index ea67289b6..a6bff8d4f 100644 --- a/src/test/java/org/takes/facets/slf4j/TkLoggedTest.java +++ b/src/test/java/org/takes/facets/slf4j/TkLoggedTest.java @@ -44,6 +44,10 @@ public final class TkLoggedTest { /** * TkLogged can log message. * @throws IOException If some problem inside + * @todo #90 we should change mocked Log4j appender to + * custom Log4jAppeneder that implemented as JUnit Rule. + * See details in CR comment here + * https://github.com/yegor256/takes/pull/89#discussion_r27354036 */ @Test public void logsMessage() throws IOException { @@ -59,7 +63,7 @@ public void logsMessage() throws IOException { public boolean matches(final Object argument) { return ((LoggingEvent) argument) .getRenderedMessage() - .matches("^.*act().*$"); + .contains("act()"); } } ) diff --git a/src/test/java/org/takes/facets/slf4j/TsLoggedTest.java b/src/test/java/org/takes/facets/slf4j/TsLoggedTest.java index 515542361..555f853df 100644 --- a/src/test/java/org/takes/facets/slf4j/TsLoggedTest.java +++ b/src/test/java/org/takes/facets/slf4j/TsLoggedTest.java @@ -47,6 +47,10 @@ public final class TsLoggedTest { /** * TsLogged can log message. * @throws IOException If some problem inside + * @todo #90 we should change mocked Log4j appender to + * custom Log4jAppeneder that implemented as JUnit Rule. + * See details in CR comment here + * https://github.com/yegor256/takes/pull/89#discussion_r27354036 */ @Test public void logsMessage() throws IOException { @@ -74,7 +78,7 @@ public String toString() { public boolean matches(final Object argument) { return ((LoggingEvent) argument) .getRenderedMessage() - .matches("^.*FakeTakes.*$"); + .contains("[FakeTakes]"); } } )