error-prone-support
error-prone-support copied to clipboard
Canonical SLF4J Logger usage
Problem
Internally we heavily use SLF4J for logging in our classes. We would like to have a check to canonicalize the usage of such loggers. For that reason we would like a check that looks at the following things:
- Is the variable declared with the modifiers
private static final
? - Is the variable name of the logger
LOG
? - Is the correct class name passed to
LoggerFactory#getLogger
?
Description of the proposed new feature
- [x] Support a stylistic preference.
- [x] Avoid a common gotcha, or potential problem.
- [ ] Improve performance.
I would like to rewrite the following code:
final class SimpleExample {
public Logger LOGGER = LoggerFactory.getLogger(WrongClass.class);
...
}
to:
final class SimpleExample {
private static final Logger LOG = LoggerFactory.getLogger(SimpleExample.class);
...
}
Considerations
For interfaces we have to make an exception when it comes to the private static final
, as in interfaces this is not an option.
As raised by @mlrprananta himself, we could parameterize the variable name of the logger. Perhaps we can add an option that people can override the allowed variable name of the logger via the ErrorProneFlags
.
For this check we don't need to look at the line on which the logger is defined, we will do that in a different check.
Additional context
This project has some related checks KengoTODA/errorprone-slf4j. However, we want a check that is specific to our internal standards. Additionlly, in EPS we have our own best practices that we want to use for the code of this check.
Later, we might want to add more features to this check. This would be a good first version 😄.