Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added exceptionLevel to allow different log level for exceptions #364

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/com/jcabi/aspects/Loggable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be a constant here, probably Loggable.WARNING

Copy link
Author

@fnmps fnmps Sep 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andreoss Creating a constant on this class would cause it to be visible to developers using the Loggable annotation, which could cause confusion. At most I could create a class for the constant, so it would be less confusing to developers using the library.
Should I proceed with that implementation or is it overkill?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fnmps I don't quite get why we need a class. -1 means default, so you can add Loggable.DEFAULT. onException probably would be a better name than exceptionLevel


/**
* Maximum amount allowed for this method (a warning will be
* issued if it takes longer).
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/jcabi/aspects/aj/MethodLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@
)
public final class MethodLogger {

/**
* Constant used to disregard logging.
*/
private static final int DEROGATION_OFF = -1;

/**
* Currently running methods.
*/
Expand Down Expand Up @@ -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,
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/com/jcabi/aspects/LoggableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down