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

KLogging + Log4j: Always getting JVMLogger.kt:21 as filename and line number #5

Open
Zomis opened this issue Mar 24, 2018 · 7 comments

Comments

@Zomis
Copy link

Zomis commented Mar 24, 2018

I am using:
log4j.appender.stdout.layout.ConversionPattern=[%d{ISO8601}] %5p %10c{1} [%10t] (%22F:%3L) - %m%n

Having the filename and line number available is very helpful for me when developing/debugging.

When using Log4j without KLogging, I get the file name and line number of my code, which varies for each place I call logger.info....

When I use Log4j with KLogging, I always get the same filename and line number, JVMLogger.kt:21 because JVMLogger is the one making the call to the Slf4j API.

Can this be handled in KLogging somehow or can I do something about it in my code?

@Lewik
Copy link
Contributor

Lewik commented Mar 25, 2018

Funny, sl4j asks "do not make any wrappers"
https://www.slf4j.org/faq.html#optional_dependency
Inline functions will not help

Just tried you format. Works for me. (Have no idea why, actually )) )

@Zomis How do you add logger in class? And how do you call logger?

@Zomis
Copy link
Author

Zomis commented Mar 25, 2018

Example code:

class KLoggingExample {

    private val logger = KLoggers.logger(this)

    fun run() {
        logger.info { "First logging" }
        logger.info { "Second logging" }
    }

}

fun main(args: Array<String>) {
    KLoggingExample().run()
}

Gradle dependencies:

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"
    compile "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"
    expectedBy project(":games-core")

    compile "com.fasterxml.jackson.core:jackson-core:2.4.2"
    compile "com.fasterxml.jackson.core:jackson-annotations:2.4.2"
    compile "com.fasterxml.jackson.core:jackson-databind:2.4.2"
    compile "com.beust:jcommander:1.58"
    compile "org.java-websocket:Java-WebSocket:1.3.0"
    compile "log4j:log4j:1.2.17"
    compile 'org.slf4j:slf4j-log4j12:1.7.25'
    compile "com.github.lewik.klogging:klogging.jvm:$kotlinVersion"
}

log4j.properties:

log4j.rootLogger=info, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=[%d{ISO8601}] %5p %10c{1} [%10t] (%22F:%3L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=server2.log

log4j.appender.R.MaxFileSize=1GB
# log4j.appender.R.MaxBackupIndex=10

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%d{ISO8601}] %5p %10c{1} [%10t] - %m%n

# appender-specific settings
log4j.appender.stdout.Threshold=DEBUG
log4j.appender.R.Threshold=DEBUG

Output:

[2018-03-25 23:04:55,407]  INFO KLoggingExample [      main] (          JVMLogger.kt: 21) - First logging
[2018-03-25 23:04:55,411]  INFO KLoggingExample [      main] (          JVMLogger.kt: 21) - Second logging

@Zomis
Copy link
Author

Zomis commented Mar 27, 2018

@Lewik Can you share more about your setup? What logging implementation do you use? Log4j 1.2.17 ? Log4j2? Logback? Commons? Anything else? It would be very helpful for me to see something else than JVMLogger.kt:21

@Lewik
Copy link
Contributor

Lewik commented Jun 7, 2018

@Zomis I am late a bit. Do you have any updates?

@Zomis
Copy link
Author

Zomis commented Jun 11, 2018

@Lewik No updates here, I've been waiting for a response from you

@BorzdeG
Copy link

BorzdeG commented Jun 11, 2018

@Zomis
so works:

class MyClass: WithLogging {
  override val logger: KLogger
    get() = KLoggers.logger(this)
// code
}

@Zomis
Copy link
Author

Zomis commented Sep 28, 2018

@BorzdeG No it does not, all my logging message still looks like they are being logged from JVMLogger.kt: 21

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

3 participants