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

How to set project loglevel #20

Closed
hughesadam87 opened this issue Apr 5, 2017 · 10 comments
Closed

How to set project loglevel #20

hughesadam87 opened this issue Apr 5, 2017 · 10 comments

Comments

@hughesadam87
Copy link

Hi,

Thanks for putting this library together. In using kotlin, I was putoff by the long/complex answers to the simple question "how do you log in kotlin (http://stackoverflow.com/questions/34416869/idiomatic-way-of-logging-in-kotlin)".

I'm using Klogging as follows:

    companion object: KLogging()

     init {
        logger.info("NewsArticle initialized: " + this)
      }

This works nicely, but when I do logger.debug, my IntelliJ IDEA run console doesn't show any output. Do you know how I could set the console logger to debug or trace levels? Is this an IDE setting, or can it be set in the call to KLogging()?

@oshai
Copy link
Owner

oshai commented Apr 5, 2017

I saw a similar question on SO: http://stackoverflow.com/questions/43146977/how-to-configure-kotlin-logging-logger
Let me know if that clear things.

@oshai
Copy link
Owner

oshai commented Apr 11, 2017

updated the frequently asked questions

@oshai oshai closed this as completed Apr 11, 2017
@sschuberth
Copy link

sschuberth commented Jul 11, 2017

Despite the FAQ entry I believe we desperately need a concrete example on how to set the log level when using e.g. logback as the logger implementation. I have in build.gradle:

dependencies {
    // Use logback-classic as the logger for kotlin-logging / slf4j as it allow changing the log level at runtime.
    compile 'io.github.microutils:kotlin-logging:1.4.5'
    compile 'ch.qos.logback:logback-classic:1.2.3'
}

And in my main.kt:

internal val log = KotlinLogging.logger {}

// ...

if (!debug) {
    // Logback's default log level is DEBUG, so change that to INFO unless --debug is given.
    (log as ch.qos.logback.classic.Logger).level = ch.qos.logback.classic.Level.INFO
}

But that gives

Exception in thread "main" java.lang.ClassCastException: mu.internal.LocationAwareKLogger cannot be cast to ch.qos.logback.classic.Logger

So how am I supposed to access the logger implementation via kotlin-logging in order to call the logger implementation-specific code to set the log level?

@oshai
Copy link
Owner

oshai commented Jul 11, 2017

I think I understand the issue you mention. What I suggest is to add to the KLogger interface a method - getUnderlyingLogger() or something similar that will provide the underlying log implementation which can be casted to ch.qos.logback.classic.Logger in the case of logback.
Another alternative is to use xml configuration like explained here: https://logback.qos.ch/manual/configuration.html

What do you think about this solution?

@oshai oshai reopened this Jul 11, 2017
@sschuberth
Copy link

getUnderlyingLogger() or something similar that will provide the underlying log implementation

That would probably work, although it seems a tad inconvenient. That is, slightly less convenient than being able to just casting the main class.

Another alternative is to use xml configuration

But that does not seem to be usable if I need to change the log level at runtime triggered by external events (potentially multiple times).

@oshai
Copy link
Owner

oshai commented Jul 11, 2017

I can add that, probably a property underlyingLogger is more appropriate. will you use that if such exists?

@sschuberth
Copy link

will you use that if such exists?

Well, given that I just migrated to slf4k because of that missing feature, probably not right away, but I'll certainly give it a try in one of my next projects.

@oshai
Copy link
Owner

oshai commented Jul 11, 2017 via email

@oshai
Copy link
Owner

oshai commented Jul 15, 2017

1.4.6 released

@oshai oshai closed this as completed Jul 15, 2017
sschuberth added a commit to oss-review-toolkit/ort that referenced this issue Nov 6, 2017
SimpleLogger cannot set the log level at runtime, and kotlin-logging
does not seem to expose a way to access the actual logger implementation
in order to set the log level, see

oshai/kotlin-logging#20 (comment)

Change-Id: I8f769fc4113bff25ac2dc84732862523025edd68
@ObserverOfTime
Copy link

ObserverOfTime commented Mar 31, 2020

This works pretty well with underlyingLogger:

import ch.qos.logback.classic.Level
import ch.qos.logback.classic.Logger

var mu.KLogger.level
    get() = (underlyingLogger as Logger).level
    set(value) { (underlyingLogger as Logger).level = value }

logger.level = Level.DEBUG
logger.level //=> Level.DEBUG

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants