Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/graphql logging #674

Merged
merged 4 commits into from
Sep 3, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ open class ConfigManager {

// set log level early
if (debugLogsEnabled(config)) {
setLogLevel(Level.DEBUG)
setLogLevelFor(BASE_LOGGER_NAME, Level.DEBUG)
}

return config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,31 @@ private fun getBaseLogger(): ch.qos.logback.classic.Logger {
return (KotlinLogging.logger(Logger.ROOT_LOGGER_NAME).underlyingLogger as ch.qos.logback.classic.Logger)
}

private fun getLogger(name: String): ch.qos.logback.classic.Logger {
val context = LoggerFactory.getILoggerFactory() as LoggerContext
return context.getLogger(name)
}

fun initLoggerConfig(appRootPath: String) {
val context = LoggerFactory.getILoggerFactory() as LoggerContext
val logger = getBaseLogger()
// logback logs to the console by default (at least when adding a console appender logs in the console are duplicated)
logger.addAppender(createRollingFileAppender(context, "$appRootPath/logs"))

// set "kotlin exposed" log level
context.getLogger("Exposed").level = Level.ERROR
setLogLevelFor("Exposed", Level.ERROR)
}

fun setLogLevel(level: Level) {
getBaseLogger().level = level
const val BASE_LOGGER_NAME = "_BaseLogger"

fun setLogLevelFor(name: String, level: Level) {
val logger = if (name == BASE_LOGGER_NAME) {
getBaseLogger()
} else {
getLogger(name)
}

logger.level = level
}

fun debugLogsEnabled(config: Config) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class ServerConfig(getConfig: () -> Config, val moduleName: String = SERVER_CONF

// misc
val debugLogsEnabled: MutableStateFlow<Boolean> by OverrideConfigValue(BooleanConfigAdapter)
val gqlDebugLogsEnabled: MutableStateFlow<Boolean> by OverrideConfigValue(BooleanConfigAdapter)
val systemTrayEnabled: MutableStateFlow<Boolean> by OverrideConfigValue(BooleanConfigAdapter)

// backup
Expand Down
27 changes: 18 additions & 9 deletions server/src/main/kotlin/suwayomi/tachidesk/server/ServerSetup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import eu.kanade.tachiyomi.App
import eu.kanade.tachiyomi.source.local.LocalSource
import io.javalin.plugin.json.JavalinJackson
import io.javalin.plugin.json.JsonMapper
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.serialization.json.Json
import mu.KotlinLogging
Expand All @@ -32,10 +33,11 @@ import suwayomi.tachidesk.server.util.SystemTray
import xyz.nulldev.androidcompat.AndroidCompat
import xyz.nulldev.androidcompat.AndroidCompatInitializer
import xyz.nulldev.ts.config.ApplicationRootDir
import xyz.nulldev.ts.config.BASE_LOGGER_NAME
import xyz.nulldev.ts.config.ConfigKodeinModule
import xyz.nulldev.ts.config.GlobalConfigManager
import xyz.nulldev.ts.config.initLoggerConfig
import xyz.nulldev.ts.config.setLogLevel
import xyz.nulldev.ts.config.setLogLevelFor
import java.io.File
import java.security.Security
import java.util.Locale
Expand Down Expand Up @@ -73,19 +75,26 @@ fun applicationSetup() {
ServerConfig.register { GlobalConfigManager.config }
)

serverConfig.subscribeTo(serverConfig.debugLogsEnabled, { debugLogsEnabled ->
if (debugLogsEnabled) {
setLogLevel(Level.DEBUG)
} else {
setLogLevel(Level.INFO)
}
}, ignoreInitialValue = false)

// Application dirs
val applicationDirs = ApplicationDirs()

initLoggerConfig(applicationDirs.dataRoot)

val setupLogLevelUpdating = { configFlow: MutableStateFlow<Boolean>, loggerNames: List<String> ->
serverConfig.subscribeTo(configFlow, { debugLogsEnabled ->
if (debugLogsEnabled) {
loggerNames.forEach { loggerName -> setLogLevelFor(loggerName, Level.DEBUG) }
} else {
loggerNames.forEach { loggerName -> setLogLevelFor(loggerName, Level.ERROR) }
}
}, ignoreInitialValue = false)
}

setupLogLevelUpdating(serverConfig.debugLogsEnabled, listOf(BASE_LOGGER_NAME))
// gql "ExecutionStrategy" spams logs with "... completing field ..."
// gql "notprivacysafe" logs every received request multiple times (received, parsing, validating, executing)
setupLogLevelUpdating(serverConfig.gqlDebugLogsEnabled, listOf("graphql", "notprivacysafe"))

logger.info("Running Tachidesk ${BuildConfig.VERSION} revision ${BuildConfig.REVISION}")

logger.debug {
Expand Down
1 change: 1 addition & 0 deletions server/src/main/resources/server-reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ server.basicAuthPassword = ""

# misc
server.debugLogsEnabled = false
server.gqlDebugLogsEnabled = false # this includes logs with non privacy safe information
server.systemTrayEnabled = true

# backup
Expand Down
1 change: 1 addition & 0 deletions server/src/test/resources/server-reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ server.globalUpdateInterval = 12

# misc
server.debugLogsEnabled = true
server.gqlDebugLogsEnabled = false
server.systemTrayEnabled = false

# webUI
Expand Down