From 68664e8312dca89f35d74b9ab93219f1d8be1835 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20Tr=C3=B8en?= Date: Wed, 17 Apr 2024 12:17:41 +0200 Subject: [PATCH] feat(logback): only use logback config in standalone server (#667) * fix deprecated pattern layout * rename to logback-standalone.xml as to not override any default logback.xml files * add env var to byo config file fix #403 and #658 --- README.md | 15 ++++++++------- .../mock/oauth2/StandaloneMockOAuth2Server.kt | 10 ++++++---- src/main/resources/logback-standalone.xml | 17 +++++++++++++++++ src/main/resources/logback.xml | 17 ----------------- 4 files changed, 31 insertions(+), 28 deletions(-) create mode 100644 src/main/resources/logback-standalone.xml delete mode 100644 src/main/resources/logback.xml diff --git a/README.md b/README.md index 4d41ff73..71e90db2 100644 --- a/README.md +++ b/README.md @@ -217,13 +217,14 @@ On Windows, it's easier to run the server in docker while specifying the host as The standalone server supports the following configuration by `ENV` variables: -| Variable | Description | -|-------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `SERVER_HOSTNAME` | Lets the standalone server bind to a specific hostname, by default it binds to `0.0.0.0` | -| `SERVER_PORT` or `PORT` | The port that the standalone server will listen to, defaults to `8080`. The `PORT` environment variable may be used to [run the Docker image on Heroku](https://devcenter.heroku.com/articles/container-registry-and-runtime#pushing-an-existing-image) as per the documentation [here](https://devcenter.heroku.com/articles/setting-the-http-port-for-java-applications). | -| `JSON_CONFIG_PATH` | The absolute path to a json file containing configuration about the OAuth2 part of the server (`OAuth2Config`). More details on the format below. | -| `JSON_CONFIG` | The actual JSON content of `OAuth2Config`, this ENV var takes precedence over the `JSON_CONFIG_PATH` var. More details on the format below. | -| `LOG_LEVEL` | How verbose the root logging output is, defaults to `INFO` | +| Variable | Description | +|-------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `SERVER_HOSTNAME` | Lets the standalone server bind to a specific hostname, by default it binds to `0.0.0.0` | +| `SERVER_PORT` or `PORT` | The port that the standalone server will listen to, defaults to `8080`. The `PORT` environment variable may be used to [run the Docker image on Heroku](https://devcenter.heroku.com/articles/container-registry-and-runtime#pushing-an-existing-image) as per the documentation [here](https://devcenter.heroku.com/articles/setting-the-http-port-for-java-applications). | +| `JSON_CONFIG_PATH` | The absolute path to a json file containing configuration about the OAuth2 part of the server (`OAuth2Config`). More details on the format below. | +| `JSON_CONFIG` | The actual JSON content of `OAuth2Config`, this ENV var takes precedence over the `JSON_CONFIG_PATH` var. More details on the format below. | +| `LOG_LEVEL` | How verbose the root logging output is, defaults to `INFO` | +| `LOGBACK_CONFIG` | You can override the default logging config in [logback-standalone.xml](src/main/resources/logback-standalone.xml) with a path to your own logback xml file. | ##### JSON_CONFIG diff --git a/src/main/kotlin/no/nav/security/mock/oauth2/StandaloneMockOAuth2Server.kt b/src/main/kotlin/no/nav/security/mock/oauth2/StandaloneMockOAuth2Server.kt index 07aaeaab..8f89e076 100644 --- a/src/main/kotlin/no/nav/security/mock/oauth2/StandaloneMockOAuth2Server.kt +++ b/src/main/kotlin/no/nav/security/mock/oauth2/StandaloneMockOAuth2Server.kt @@ -1,15 +1,16 @@ package no.nav.security.mock.oauth2 +import ch.qos.logback.classic.ClassicConstants +import java.io.File +import java.io.FileNotFoundException +import java.net.InetAddress +import java.net.InetSocketAddress import no.nav.security.mock.oauth2.StandaloneConfig.hostname import no.nav.security.mock.oauth2.StandaloneConfig.oauth2Config import no.nav.security.mock.oauth2.StandaloneConfig.port import no.nav.security.mock.oauth2.http.NettyWrapper import no.nav.security.mock.oauth2.http.OAuth2HttpResponse import no.nav.security.mock.oauth2.http.route -import java.io.File -import java.io.FileNotFoundException -import java.net.InetAddress -import java.net.InetSocketAddress object StandaloneConfig { const val JSON_CONFIG = "JSON_CONFIG" @@ -47,6 +48,7 @@ object StandaloneConfig { } fun main() { + System.setProperty(ClassicConstants.CONFIG_FILE_PROPERTY, "LOGBACK_CONFIG".fromEnv("logback-standalone.xml")) MockOAuth2Server( oauth2Config(), route("/isalive") { diff --git a/src/main/resources/logback-standalone.xml b/src/main/resources/logback-standalone.xml new file mode 100644 index 00000000..28e82dd5 --- /dev/null +++ b/src/main/resources/logback-standalone.xml @@ -0,0 +1,17 @@ + + + + + + %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{70} - %msg%n + + + + + + + + + + + diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml deleted file mode 100644 index f44889fb..00000000 --- a/src/main/resources/logback.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{70} - %msg%n - - - - - - - - - - -