ktorio / ktor

Framework for quickly creating connected applications in Kotlin with minimal effort
https://ktor.io
Apache License 2.0
13.09k stars 1.07k forks source link

Ktor-client-logging : Add an option to filter logged calls #1618

Closed eMZet-zz closed 2 years ago

eMZet-zz commented 4 years ago

Subsystem Kotlin multiplatform library: ktor-client-logging:1.3.0

Is your feature request related to a problem? Please describe. Problem: not able to filter calls to be logged. All calls are being logged by default.

Describe the solution you'd like Add a filter option in Logging configuration block (this solution is available on Android in CallLogging feature, but it is absent in kotlin multiplatform) https://ktor.io/servers/features/call-logging.html

install(Logging) {
    filter { call -> call.request.path().startsWith("/section1") }
}

Motivation to include to ktor Logging all queries is helpful in debug mode. However, for a production version in which, for example, logs are saved to a file, saving only failed requests would be sufficient. It would be convenient to use the function that is already in the library.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity.

oleg-larshin commented 4 years ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

AndresAusecha commented 3 years ago

Hey I would like to try to solve this one

mervyn-mccreight commented 2 years ago

Is this really still open? Looking at ktor-client/ktor-client-plugins/ktor-client-logging/common/src/io/ktor/client/plugins/logging/Logging.kt it looks like this is there already:

    /**
     * [Logging] plugin configuration
     */
    @KtorDsl
    public class Config {
        /**
         * filters
         */
        internal var filters = mutableListOf<(HttpRequestBuilder) -> Boolean>()

        /**
         * [Logger] instance to use
         */
        public var logger: Logger = Logger.DEFAULT

        /**
         * log [LogLevel]
         */
        public var level: LogLevel = LogLevel.HEADERS

        /**
         * Log messages for calls matching a [predicate].
         */
        public fun filter(predicate: (HttpRequestBuilder) -> Boolean) {
            filters.add(predicate)
        }
    }
Stexxe commented 2 years ago

Fixed by https://github.com/ktorio/ktor/commit/da7cb98d829596fe66342e47e5a5f270e63c261c