Skip to content
Merged
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
2 changes: 1 addition & 1 deletion domain-models-api-interfaces/ramls/admin.raml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ traits:
example: "Internal server error, contact administrator"
/loglevel:
put:
description: Set logging level for all loggers in the JVM or just for a specific package / class
description: Set logging level for the comma separated list of class names. Use * as tailing wildcard to match all or specific loggers provided they have been used since startup
is: [log-level]
responses:
200:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@

private static final Logger log = LogManager.getLogger(LogUtil.class);

public static void formatStatsLogMessage(String clientIP, String httpMethod, String httpVersion, int responseCode, long responseTime,

Check warning on line 24 in domain-models-runtime/src/main/java/org/folio/rest/tools/utils/LogUtil.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this 135 characters long line (which is greater than 120 authorized).

See more on https://sonarcloud.io/project/issues?id=org.folio%3Araml-module-builder&issues=AZr9s3bX0BxmTJuUvisZ&open=AZr9s3bX0BxmTJuUvisZ&pullRequest=1210
long responseSize, String url, String queryParams, String message) {

String message1 = new StringBuilder(injectDeploymentId()).append(clientIP).append(" ").append(httpMethod).append(" ").append(url).append(" ").append(queryParams)
.append(" ").append(httpVersion).append(" ").append(responseCode).append(" ").append(responseSize).append(" ").append(responseTime)

Check warning on line 28 in domain-models-runtime/src/main/java/org/folio/rest/tools/utils/LogUtil.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this 139 characters long line (which is greater than 120 authorized).

See more on https://sonarcloud.io/project/issues?id=org.folio%3Araml-module-builder&issues=AZr9s3bX0BxmTJuUvisb&open=AZr9s3bX0BxmTJuUvisb&pullRequest=1210
.append(" ").append(message).toString();

log.info(message1);
}

public static void formatStatsLogMessage(String clientIP, String httpMethod, String httpVersion, int responseCode, long responseTime,

Check warning on line 34 in domain-models-runtime/src/main/java/org/folio/rest/tools/utils/LogUtil.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this 135 characters long line (which is greater than 120 authorized).

See more on https://sonarcloud.io/project/issues?id=org.folio%3Araml-module-builder&issues=AZr9s3bX0BxmTJuUvisc&open=AZr9s3bX0BxmTJuUvisc&pullRequest=1210
long responseSize, String url, String queryParams, String message, String tenantId, String body) {

String message1 = new StringBuilder(injectDeploymentId()).append(clientIP).append(" ").append(httpMethod).append(" ").append(url).append(" ").append(queryParams)
Expand Down Expand Up @@ -59,7 +59,7 @@
long responseTime, String tenantId, String body) {

if (routingContext == null) {
log.info(injectDeploymentId() + responseTime + " tid=" + tenantId + " " + body);

Check warning on line 62 in domain-models-runtime/src/main/java/org/folio/rest/tools/utils/LogUtil.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Format specifiers should be used instead of string concatenation.

See more on https://sonarcloud.io/project/issues?id=org.folio%3Araml-module-builder&issues=AZr9s3bX0BxmTJuUvisj&open=AZr9s3bX0BxmTJuUvisj&pullRequest=1210
return;
}
HttpServerRequest request = routingContext.request();
Expand All @@ -79,7 +79,7 @@
}

public static void formatLogMessage(String clazz, String function, String message) {
log.info(new StringBuilder(injectDeploymentId()).append(clazz).append(" ").append(function).append(" ").append(message));

Check warning on line 82 in domain-models-runtime/src/main/java/org/folio/rest/tools/utils/LogUtil.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this 125 characters long line (which is greater than 120 authorized).

See more on https://sonarcloud.io/project/issues?id=org.folio%3Araml-module-builder&issues=AZr9s3bX0BxmTJuUvisf&open=AZr9s3bX0BxmTJuUvisf&pullRequest=1210
}
public static void formatErrorLogMessage(String clazz, String function, String message) {
log.error(new StringBuilder(injectDeploymentId()).append(clazz).append(" ").append(function).append(" ").append(message));
Expand All @@ -104,26 +104,39 @@

/**
* Update the log level for all packages / a specific package / a specific class
* @param packageName - pass "*" for all packages
* @param packageNames - pass "*" for all packages, use comma to separate multiple names
* @param level - see {@link Level}
* @return - JsonObject with a list of updated loggers and their levels
*/
public static JsonObject updateLogConfiguration(String packageName, String level){
public static JsonObject updateLogConfiguration(String packageNames, String level) {
var updatedLoggers = new JsonObject();
if (packageNames == null) {
return updatedLoggers;
}
for (var packageName : packageNames.split(",")) {
updateLogConfiguration(packageName, level, updatedLoggers);
}
return updatedLoggers;
}

private static void updateLogConfiguration(String packageName, String level, JsonObject updatedLoggers) {
var prefix = packageName.replace("*", "");
if (prefix.equals(packageName)) {
// create the logger, it might not have been used since container startup
LogManager.getLogger(packageName);
}

JsonObject updatedLoggers = new JsonObject();
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Collection<org.apache.logging.log4j.core.Logger> allLoggers = ctx.getLoggers();

allLoggers.forEach( log -> {
if(log != null && packageName != null && (log.getName().startsWith(packageName.replace("*", "")) || "*".equals(packageName)) ){
if(log != null){
log.setLevel(getLog4jLevel(level));
updatedLoggers.put(log.getName(), log.getLevel().toString());
}
for (var log: ctx.getLoggers()) {
if (log == null) {
continue;
}
});

return updatedLoggers;
if (log.getName().startsWith(prefix)) {
log.setLevel(getLog4jLevel(level));
updatedLoggers.put(log.getName(), log.getLevel().toString());
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,22 @@ public void append(LogEvent event) {
message.append(event.getMessage().getFormattedMessage());
}
}

@Test
void updateLogConfigurationUnused() {
LogUtil.updateLogConfiguration("some.package.UnusedClass", "ERROR");
assertThat(LogManager.getLogger("some.package.UnusedClass").getLevel(), is(Level.ERROR));
}

@Test
void updateLogConfiguration3() {
LogUtil.updateLogConfiguration("foo.X,bar.Y,baz.Z", "WARN");
assertThat(LogManager.getLogger("foo.X").getLevel(), is(Level.WARN));
assertThat(LogManager.getLogger("bar.Y").getLevel(), is(Level.WARN));
assertThat(LogManager.getLogger("baz.Z").getLevel(), is(Level.WARN));
LogUtil.updateLogConfiguration("foo.*,bar.*,baz.*", "DEBUG");
assertThat(LogManager.getLogger("foo.X").getLevel(), is(Level.DEBUG));
assertThat(LogManager.getLogger("bar.Y").getLevel(), is(Level.DEBUG));
assertThat(LogManager.getLogger("baz.Z").getLevel(), is(Level.DEBUG));
}
}
Loading