diff --git a/src/main/java/com/jcabi/aspects/Loggable.java b/src/main/java/com/jcabi/aspects/Loggable.java index 61e68a84..2e024e18 100644 --- a/src/main/java/com/jcabi/aspects/Loggable.java +++ b/src/main/java/com/jcabi/aspects/Loggable.java @@ -132,6 +132,13 @@ */ 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..6ba04ccd 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 DEROGATION_OFF = -1; + /** * Currently running methods. */ @@ -240,6 +245,9 @@ private Object wrap(final ProceedingJoinPoint point, final Method method, } else { origin = "somewhere"; } + if (annotation.exceptionLevel() != MethodLogger.DEROGATION_OFF) { + 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..5a50c861 100644 --- a/src/test/java/com/jcabi/aspects/LoggableTest.java +++ b/src/test/java/com/jcabi/aspects/LoggableTest.java @@ -58,6 +58,16 @@ final class LoggableTest { */ 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 void logsSimpleCall() { new LoggableTest.Foo().revert("hello"); @@ -162,6 +172,26 @@ void logsShortArray() { ); } + @Test + void logsWithErrorExceptionLevel() throws Exception { + final StringWriter writer = new StringWriter(); + Logger.getRootLogger().addAppender( + new WriterAppender(new SimpleLayout(), writer) + ); + try { + 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) + ); + } + } + @Test void logsWithExplicitLoggerName() throws Exception { final StringWriter writer = new StringWriter(); @@ -236,6 +266,15 @@ 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 UnsupportedOperationException(); + } + /** * Revert string. * @param text Some text