Skip to content

Commit

Permalink
Initial file logging support
Browse files Browse the repository at this point in the history
  • Loading branch information
DGuhr committed Mar 1, 2022
1 parent 7b11808 commit 7c77d22
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/guides/src/main/server/logging.adoc
Expand Up @@ -86,6 +86,20 @@ Be aware that you need to escape characters when invoking commands containing sp
<@kc.start parameters="\"\'%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c{3.}] (%t) %s%e%n\'\""/>
The example above abbreviates the category name, which could get rather long in some cases, to three characters by setting `[%c{3.}]` in the template instead of the default `[%c]`.
== File logging
Logging to a file is disabled by default. To enable it, run the following command:
<@kc.start parameters="--log-file-enabled=true"/>
By enabling the file log handler, a log file named `keycloak.log` will be created inside the `data/log` directory of your Keycloak installation by default.
To change the location and name of the generated log file, run the following command:
<@kc.start parameters="--log-file-enabled=true --log-file=<path>/<your-file.log>"/>
Please make sure the location for the logfile is writeable. If not, an error will be thrown at start-up. Keycloak will start correctly, but no file containing logs will be created.
== Configuring raw quarkus logging properties
At the time of writing, the logging features of the quarkus based Keycloak are basic, yet powerful. Nevertheless, expect more to come and feel free to join the https://github.com/keycloak/keycloak/discussions/8870[discussion] at GitHub.
Expand Down
7 changes: 7 additions & 0 deletions quarkus/dist/assembly.xml
Expand Up @@ -79,6 +79,13 @@
<include>*.*</include>
</includes>
</fileSet>
<fileSet>
<directory>src/main/content/data</directory>
<outputDirectory>data</outputDirectory>
<includes>
<include>**/**</include>
</includes>
</fileSet>
<fileSet>
<directory>../../</directory>
<outputDirectory></outputDirectory>
Expand Down
Expand Up @@ -2,18 +2,23 @@

import static org.keycloak.quarkus.runtime.integration.QuarkusPlatform.addInitializationException;

import java.io.File;
import java.util.Arrays;
import java.util.Locale;
import java.util.function.BiFunction;
import java.util.logging.Level;

import org.jboss.logmanager.LogContext;
import org.keycloak.quarkus.runtime.Environment;
import org.keycloak.quarkus.runtime.Messages;

import io.smallrye.config.ConfigSourceInterceptorContext;

final class LoggingPropertyMappers {

private static final String DEFAULT_LOG_LEVEL = "info";
private static final String DEFAULT_LOG_PATH = "/data/log/keycloak.log";
private static final String DEFAULT_LOG_FILENAME = "keycloak.log";

private LoggingPropertyMappers(){}

Expand Down Expand Up @@ -69,10 +74,32 @@ public String apply(String value, ConfigSourceInterceptorContext configSourceInt
.defaultValue("%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%c] (%t) %s%e%n")
.description("The format of log entries. If the format has spaces in it, you need to escape the value such as \"<format>\".")
.paramLabel("format")
.build(),
builder().from("log-file-enabled")
.to("quarkus.log.file.enable")
.defaultValue(Boolean.FALSE.toString())
.description("Enable or disable File logging.")
.paramLabel(Boolean.TRUE + "|" + Boolean.FALSE)
.expectedValues(Arrays.asList(Boolean.TRUE.toString(), Boolean.FALSE.toString()))
.build(),
builder().from("log-file")
.to("quarkus.log.file.path")
.defaultValue(Environment.getHomeDir()+DEFAULT_LOG_PATH)
.description("Set the path... TODO.")
.paramLabel("<path>/<file-name>.log")
.transformer(LoggingPropertyMappers::resolveFileLogLocation)
.build()
};
}

private static String resolveFileLogLocation(String value, ConfigSourceInterceptorContext configSourceInterceptorContext) {
if (value.endsWith(File.separator))
{
return value + DEFAULT_LOG_FILENAME;
}
return value;
}

private static Level toLevel(String categoryLevel) throws IllegalArgumentException {
return LogContext.getLogContext().getLevelForName(categoryLevel.toUpperCase(Locale.ROOT));
}
Expand Down
2 changes: 2 additions & 0 deletions quarkus/runtime/src/main/resources/META-INF/keycloak.conf
Expand Up @@ -22,3 +22,5 @@ metrics-enabled=false
%import_export.hostname-strict=false
%import_export.hostname-strict-https=false
%import_export.cluster=local

log-file-location=${kc.home.dir}/data/log/keycloak.log

0 comments on commit 7c77d22

Please sign in to comment.