Table of contents
Table of Contents | ||
---|---|---|
|
...
This should be treated carefully to avoid excessive logging: if the method is simple, or does not require logging (everything is logged in nested methods for example) skip this recommendation. Common layered architecture of a service contains controllers, services and repositories (generated and hand-written). Methods of these classes are a good place to log enter/exit.This can not be directly applied to reactive-style methods, returning Future objects. In this case, the logging should be done in the code which sets Future result.
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
public int sumWithCondition(int a, int b, int cond) { LOGGER.debug("sumWithCondition:: parameters a: {}, b: {}, cond: {}", a, b, cond); int sumResult; if (a > cond) { LOGGER.info("sumWithCondition:: a > cond"); sumResult = a + b; } else { LOGGER.info("sumWithCondition:: a <= cond"); sumResult = cond + b; } LOGGER.info("sumWithCondition:: result: {}", sumResult); return sumResult; } |
This can not be directly applied to reactive-style methods, returning Future objects. In this case, the logging should be done in the code which sets Future result, e.g. add logging for onSuccess and ensure there is error logging for onFailure.
1.3 Branching and conditions
...
If sensitive data or part of it is required to troubleshoot, it should be tagged in log with a special token. It will allow to mask/remove those messages after they are no longer required or to limit access to logs, containing those tags.
Masking example from RMB:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
/** * Log postgreSQLClientConfig. */ @SuppressWarnings("squid:S2068") // Suppress "Credentials should not be hard-coded" // "'password' detected in this expression". // False positive: Password is configurable, here we remove it from the log. private void logPostgresConfig() { if (! log.isInfoEnabled()) { return; } JsonObject passwordRedacted = postgreSQLClientConfig.copy(); passwordRedacted.put(PASSWORD, "..."); log.info("postgreSQLClientConfig = {}", passwordRedacted.encode()); } |
Info |
---|
Additional information in log
...
- All log messages must be created using log framework substitution mechanism.
- Complex log message parameters should be created lazily where possible
- Using Pattern Layout, pay attention to the conversion characters you use. See https://www.codejava.net/coding/common-conversion-patterns-for-log4js-patternlayout