From 34d9ece6d717d2b852afc686fbf91f8be851aff1 Mon Sep 17 00:00:00 2001 From: LamGC Date: Fri, 11 Nov 2022 18:17:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(logging):=20=E6=94=AF=E6=8C=81=E9=80=9A?= =?UTF-8?q?=E8=BF=87=E5=8F=82=E6=95=B0=E6=8E=A7=E5=88=B6=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E7=BA=A7=E5=88=AB.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 通过添加新的 -D 参数 `scalabot.log.level`, 使得用户可以根据需要, 调整日志输出级别, 通过日志来自行(或协助开发者)排查问题, 对于与网络有关的 Debug 级别日志, 则由 `scalabot.log.network.verbose` 参数进行控制; 另外, 由于开发过程中可通过该参数修改日志级别, 因此移除 `logback-test.xml` 文件. --- scalabot-app/src/main/kotlin/AppConfigs.kt | 29 +++++++++++++++++++ .../src/main/resources/base-logback.xml | 2 ++ .../src/main/resources/logback-test.xml | 16 ---------- scalabot-app/src/main/resources/logback.xml | 9 +++++- 4 files changed, 39 insertions(+), 17 deletions(-) delete mode 100644 scalabot-app/src/main/resources/logback-test.xml diff --git a/scalabot-app/src/main/kotlin/AppConfigs.kt b/scalabot-app/src/main/kotlin/AppConfigs.kt index 7c19ed0..51f54ab 100644 --- a/scalabot-app/src/main/kotlin/AppConfigs.kt +++ b/scalabot-app/src/main/kotlin/AppConfigs.kt @@ -12,6 +12,7 @@ import org.eclipse.aether.repository.Authentication import org.eclipse.aether.repository.Proxy import org.eclipse.aether.repository.RemoteRepository import org.eclipse.aether.repository.RepositoryPolicy +import org.slf4j.event.Level import org.telegram.telegrambots.bots.DefaultBotOptions import java.io.File import java.net.URL @@ -248,6 +249,34 @@ internal class LogDirectorySupplier : PropertyDefinerBase() { } } +internal class LogLevelSupplier : PropertyDefinerBase() { + override fun getPropertyValue(): String { + val property = System.getProperty("scalabot.log.level", System.getenv("BOT_LOG_LEVEL")) + val level = if (property != null) { + try { + Level.valueOf(property.uppercase()) + } catch (e: IllegalArgumentException) { + addWarn("Invalid log level: `$property`, the log will be output using the Info log level.") + Level.INFO + } + } else { + Level.INFO + } + return level.name + } +} + +internal class NetworkVerboseLogSupplier : PropertyDefinerBase() { + override fun getPropertyValue(): String { + val propertyValue = System.getProperty("scalabot.log.network.verbose", "false") + return if (propertyValue.toBoolean()) { + "DEBUG" + } else { + "INFO" + } + } +} + internal object Const { val config = loadAppConfig() const val METRICS_NAMESPACE = "scalabot" diff --git a/scalabot-app/src/main/resources/base-logback.xml b/scalabot-app/src/main/resources/base-logback.xml index 82da7b8..b1dc020 100644 --- a/scalabot-app/src/main/resources/base-logback.xml +++ b/scalabot-app/src/main/resources/base-logback.xml @@ -1,6 +1,8 @@ + + diff --git a/scalabot-app/src/main/resources/logback-test.xml b/scalabot-app/src/main/resources/logback-test.xml deleted file mode 100644 index dce4786..0000000 --- a/scalabot-app/src/main/resources/logback-test.xml +++ /dev/null @@ -1,16 +0,0 @@ - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/scalabot-app/src/main/resources/logback.xml b/scalabot-app/src/main/resources/logback.xml index 041f5fa..10a4e74 100644 --- a/scalabot-app/src/main/resources/logback.xml +++ b/scalabot-app/src/main/resources/logback.xml @@ -1,7 +1,14 @@ - + + + + + + + +