From 9b9162e52f4bb1a609bf1257a417b8915c407856 Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Mon, 18 Sep 2023 23:23:46 +0200 Subject: [PATCH 1/4] Added exceptionLevel to allow different log level for exceptions --- src/main/java/com/jcabi/aspects/Loggable.java | 6 ++++ .../com/jcabi/aspects/aj/MethodLogger.java | 1 + .../java/com/jcabi/aspects/LoggableTest.java | 35 ++++++++++++++++++- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/jcabi/aspects/Loggable.java b/src/main/java/com/jcabi/aspects/Loggable.java index 61e68a84..45ae1eac 100644 --- a/src/main/java/com/jcabi/aspects/Loggable.java +++ b/src/main/java/com/jcabi/aspects/Loggable.java @@ -132,6 +132,12 @@ */ int value() default Loggable.INFO; + /** + * Log level for exceptions. If not defined then the log level of value is used. + * @return The log level + */ + int exceptionLevel() default -1; + /** * Maximum amount allowed for this method (a warning will be * issued if it takes longer). diff --git a/src/main/java/com/jcabi/aspects/aj/MethodLogger.java b/src/main/java/com/jcabi/aspects/aj/MethodLogger.java index 768700ae..1bf4de6e 100644 --- a/src/main/java/com/jcabi/aspects/aj/MethodLogger.java +++ b/src/main/java/com/jcabi/aspects/aj/MethodLogger.java @@ -240,6 +240,7 @@ private Object wrap(final ProceedingJoinPoint point, final Method method, } else { origin = "somewhere"; } + level = annotation.exceptionLevel() == -1 ? level : annotation.exceptionLevel(); if (LogHelper.enabled(level, method.getDeclaringClass())) { LogHelper.log( level, diff --git a/src/test/java/com/jcabi/aspects/LoggableTest.java b/src/test/java/com/jcabi/aspects/LoggableTest.java index f5b6fabb..76ad5b51 100644 --- a/src/test/java/com/jcabi/aspects/LoggableTest.java +++ b/src/test/java/com/jcabi/aspects/LoggableTest.java @@ -57,6 +57,8 @@ final class LoggableTest { * Foo toString result. */ private static final transient String RESULT = "some text"; + private static final transient String DEBUG_LOG = "DEBUG"; + private static final transient String ERROR_LOG = "ERROR"; @Test void logsSimpleCall() { @@ -161,7 +163,29 @@ void logsShortArray() { Matchers.stringContainsInOrder(shorts) ); } - + + @Test + void logsWithErrorExceptionLevel() throws Exception { + final StringWriter writer = new StringWriter(); + Logger.getRootLogger().addAppender( + new WriterAppender(new SimpleLayout(), writer) + ); + try { + LoggableTest.Foo.errorExceptionLogging(); + } catch(Exception e) { + // Assert prepend DEBUG + MatcherAssert.assertThat( + writer.toString(), + new LoggableTest.RegexContainsMatcher(DEBUG_LOG) + ); + // Assert exception ERROR + MatcherAssert.assertThat( + writer.toString(), + new LoggableTest.RegexContainsMatcher(ERROR_LOG) + ); + } + } + @Test void logsWithExplicitLoggerName() throws Exception { final StringWriter writer = new StringWriter(); @@ -235,6 +259,15 @@ public static String text() throws Exception { public static String explicitLoggerName() { return LoggableTest.Foo.hiddenText(); } + + /** + * Method annotated with Loggable specifying exceptionLevel. + * @return A String + */ + @Loggable(value = Loggable.DEBUG, exceptionLevel = Loggable.ERROR, prepend = true) + public static String errorExceptionLogging() { + throw new RuntimeException(); + } /** * Revert string. From ac31c7274c41ad14cda7637b75f6717c17e4f2a3 Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Tue, 19 Sep 2023 21:39:42 +0200 Subject: [PATCH 2/4] Replaced ternary operation --- src/main/java/com/jcabi/aspects/aj/MethodLogger.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/jcabi/aspects/aj/MethodLogger.java b/src/main/java/com/jcabi/aspects/aj/MethodLogger.java index 1bf4de6e..e926289e 100644 --- a/src/main/java/com/jcabi/aspects/aj/MethodLogger.java +++ b/src/main/java/com/jcabi/aspects/aj/MethodLogger.java @@ -66,6 +66,11 @@ ) public final class MethodLogger { + /** + * constant used to disregard logging. + */ + private static final int IGNORE_EXCEPTION_LEVEL = -1; + /** * Currently running methods. */ @@ -240,7 +245,9 @@ private Object wrap(final ProceedingJoinPoint point, final Method method, } else { origin = "somewhere"; } - level = annotation.exceptionLevel() == -1 ? level : annotation.exceptionLevel(); + if(annotation.exceptionLevel() != IGNORE_EXCEPTION_LEVEL) { + level = annotation.exceptionLevel(); + } if (LogHelper.enabled(level, method.getDeclaringClass())) { LogHelper.log( level, From e1f547294379a8bda6d96024b05e51a6f2af0d9a Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Tue, 19 Sep 2023 22:05:30 +0200 Subject: [PATCH 3/4] Solved checkstyle issues --- src/main/java/com/jcabi/aspects/Loggable.java | 5 +++-- src/main/java/com/jcabi/aspects/aj/MethodLogger.java | 8 ++++---- src/test/java/com/jcabi/aspects/LoggableTest.java | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/jcabi/aspects/Loggable.java b/src/main/java/com/jcabi/aspects/Loggable.java index 45ae1eac..2e024e18 100644 --- a/src/main/java/com/jcabi/aspects/Loggable.java +++ b/src/main/java/com/jcabi/aspects/Loggable.java @@ -133,11 +133,12 @@ int value() default Loggable.INFO; /** - * Log level for exceptions. If not defined then the log level of value is used. + * Log level for exceptions. If not defined then the log + * level of value is used. * @return The log level */ int exceptionLevel() default -1; - + /** * Maximum amount allowed for this method (a warning will be * issued if it takes longer). diff --git a/src/main/java/com/jcabi/aspects/aj/MethodLogger.java b/src/main/java/com/jcabi/aspects/aj/MethodLogger.java index e926289e..a08735dc 100644 --- a/src/main/java/com/jcabi/aspects/aj/MethodLogger.java +++ b/src/main/java/com/jcabi/aspects/aj/MethodLogger.java @@ -66,11 +66,11 @@ ) public final class MethodLogger { - /** + /** * constant used to disregard logging. */ private static final int IGNORE_EXCEPTION_LEVEL = -1; - + /** * Currently running methods. */ @@ -245,8 +245,8 @@ private Object wrap(final ProceedingJoinPoint point, final Method method, } else { origin = "somewhere"; } - if(annotation.exceptionLevel() != IGNORE_EXCEPTION_LEVEL) { - level = annotation.exceptionLevel(); + if (annotation.exceptionLevel() != IGNORE_EXCEPTION_LEVEL) { + level = annotation.exceptionLevel(); } if (LogHelper.enabled(level, method.getDeclaringClass())) { LogHelper.log( diff --git a/src/test/java/com/jcabi/aspects/LoggableTest.java b/src/test/java/com/jcabi/aspects/LoggableTest.java index 76ad5b51..4a537da1 100644 --- a/src/test/java/com/jcabi/aspects/LoggableTest.java +++ b/src/test/java/com/jcabi/aspects/LoggableTest.java @@ -177,12 +177,12 @@ void logsWithErrorExceptionLevel() throws Exception { MatcherAssert.assertThat( writer.toString(), new LoggableTest.RegexContainsMatcher(DEBUG_LOG) - ); + ); // Assert exception ERROR MatcherAssert.assertThat( writer.toString(), new LoggableTest.RegexContainsMatcher(ERROR_LOG) - ); + ); } } From eb56f33de3965308f5f793273a4a09f386b7bb21 Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Tue, 19 Sep 2023 23:20:00 +0200 Subject: [PATCH 4/4] Fixed qulice errors --- .../com/jcabi/aspects/aj/MethodLogger.java | 6 ++-- .../java/com/jcabi/aspects/LoggableTest.java | 36 +++++++++++-------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/jcabi/aspects/aj/MethodLogger.java b/src/main/java/com/jcabi/aspects/aj/MethodLogger.java index a08735dc..6ba04ccd 100644 --- a/src/main/java/com/jcabi/aspects/aj/MethodLogger.java +++ b/src/main/java/com/jcabi/aspects/aj/MethodLogger.java @@ -67,9 +67,9 @@ public final class MethodLogger { /** - * constant used to disregard logging. + * Constant used to disregard logging. */ - private static final int IGNORE_EXCEPTION_LEVEL = -1; + private static final int DEROGATION_OFF = -1; /** * Currently running methods. @@ -245,7 +245,7 @@ private Object wrap(final ProceedingJoinPoint point, final Method method, } else { origin = "somewhere"; } - if (annotation.exceptionLevel() != IGNORE_EXCEPTION_LEVEL) { + if (annotation.exceptionLevel() != MethodLogger.DEROGATION_OFF) { level = annotation.exceptionLevel(); } if (LogHelper.enabled(level, method.getDeclaringClass())) { diff --git a/src/test/java/com/jcabi/aspects/LoggableTest.java b/src/test/java/com/jcabi/aspects/LoggableTest.java index 4a537da1..5a50c861 100644 --- a/src/test/java/com/jcabi/aspects/LoggableTest.java +++ b/src/test/java/com/jcabi/aspects/LoggableTest.java @@ -57,7 +57,15 @@ final class LoggableTest { * Foo toString result. */ private static final transient String RESULT = "some text"; + + /** + * Log prefix for DEBUG. + */ private static final transient String DEBUG_LOG = "DEBUG"; + + /** + * Log prefix for ERROR. + */ private static final transient String ERROR_LOG = "ERROR"; @Test @@ -163,7 +171,7 @@ void logsShortArray() { Matchers.stringContainsInOrder(shorts) ); } - + @Test void logsWithErrorExceptionLevel() throws Exception { final StringWriter writer = new StringWriter(); @@ -171,21 +179,19 @@ void logsWithErrorExceptionLevel() throws Exception { new WriterAppender(new SimpleLayout(), writer) ); try { - LoggableTest.Foo.errorExceptionLogging(); - } catch(Exception e) { - // Assert prepend DEBUG - MatcherAssert.assertThat( - writer.toString(), - new LoggableTest.RegexContainsMatcher(DEBUG_LOG) + LoggableTest.Foo.errorExceptionLogging(); + } catch (final UnsupportedOperationException exception) { + MatcherAssert.assertThat( + writer.toString(), + new LoggableTest.RegexContainsMatcher(LoggableTest.DEBUG_LOG) + ); + MatcherAssert.assertThat( + writer.toString(), + new LoggableTest.RegexContainsMatcher(LoggableTest.ERROR_LOG) ); - // Assert exception ERROR - MatcherAssert.assertThat( - writer.toString(), - new LoggableTest.RegexContainsMatcher(ERROR_LOG) - ); } } - + @Test void logsWithExplicitLoggerName() throws Exception { final StringWriter writer = new StringWriter(); @@ -259,14 +265,14 @@ public static String text() throws Exception { public static String explicitLoggerName() { return LoggableTest.Foo.hiddenText(); } - + /** * Method annotated with Loggable specifying exceptionLevel. * @return A String */ @Loggable(value = Loggable.DEBUG, exceptionLevel = Loggable.ERROR, prepend = true) public static String errorExceptionLogging() { - throw new RuntimeException(); + throw new UnsupportedOperationException(); } /**