https://kotlinlang.org logo
#ktor
Title
# ktor
n

napperley

01/29/2019, 12:38 AM
Is it possible to use Log4j Kotlin ( https://logging.apache.org/log4j/kotlin/index.html ) with Ktor?
Doesn't seem possible since Ktor doesn't support Log4j 2 🙁.
r

rocketraman

01/31/2019, 8:48 PM
Ktor uses slf4j, which supports log4j2 as a logging backend. That being said, I'm not sure I really understand your question... you can absolutely use log4j2's kotlin api in your own code, thereby having both Ktor and your own logging go to the same backend logging system.
n

napperley

01/31/2019, 9:52 PM
When I tried to get Log4j 2 working with Ktor the framework wouldn't recognise the library, even when the log4j-to-slf4j library was used.
r

rocketraman

01/31/2019, 9:55 PM
That's the wrong library:
log4j-to-slf4j
directs log4j logs to slf4j, which then use a different logging backend. You want
log4j-slf4j-impl
to direct logging using slf4j to log4j2.
n

napperley

01/31/2019, 10:11 PM
Tried using log4j-slf4j-impl however nothing is being logged to the terminal. Even Netty log messages aren't being logged.
r

rocketraman

01/31/2019, 10:40 PM
What does your
log4j2.xml
look like?
You also removed
log4j-to-slf4j
right? I have to ask 🙂
n

napperley

01/31/2019, 11:40 PM
Below are the contents of the Log4j 2 configuration file:
Yes I have removed log4j-to-slf4j.
r

rocketraman

01/31/2019, 11:43 PM
Forgeting about ktor for a second, do you get logs straight from log4j2?
n

napperley

02/01/2019, 12:02 AM
No, the logs aren't coming through from log4j2.
Now getting log messages outputted to the console however there are multiple errors appearing about the message format which is strange:
Copy code
2019-02-01 13:31:10,440 main ERROR Unrecognized format specifier [p]
2019-02-01 13:31:10,441 main ERROR Unrecognized conversion specifier [p] starting at position 28 in conversion pattern.
2019-02-01 13:31:10,442 main ERROR Unrecognized format specifier [c]
2019-02-01 13:31:10,442 main ERROR Unrecognized conversion specifier [c] starting at position 34 in conversion pattern.
2019-02-01 13:31:10,442 main ERROR Unrecognized format specifier [L]
2019-02-01 13:31:10,442 main ERROR Unrecognized conversion specifier [L] starting at position 37 in conversion pattern.
2019-02-01 13:31:10,443 main ERROR Unrecognized format specifier [m]
2019-02-01 13:31:10,443 main ERROR Unrecognized conversion specifier [m] starting at position 42 in conversion pattern.
2019-02-01 13:31:10,443 main ERROR Unrecognized format specifier [n]
2019-02-01 13:31:10,443 main ERROR Unrecognized conversion specifier [n] starting at position 44 in conversion pattern.
Wondering if there are class path issues, aka Ktor's own version of log4j is interfering with the program's log4j2 ( https://stackoverflow.com/questions/44480197/log4j2-error-unrecognized-format-specifier-t ).
Below is the contents of the dependencies block (from build.gradle.kts):
Copy code
val kotlinVer = "1.3.20"
    val ktorVer = "1.1.2"
    val mariaDbVer = "2.3.0"
    val jooqVer = "3.10.0"
    val jacksonKotlinModuleVer = "2.9.6"
    val log4jVer = "2.11.1"

    implementation(kotlin(module = "stdlib-jdk8", version = kotlinVer))
    implementation("io.ktor:ktor-server-core:$ktorVer")
    implementation("io.ktor:ktor-server-netty:$ktorVer")
    implementation("io.ktor:ktor-jackson:$ktorVer")
    implementation("org.apache.logging.log4j:log4j-slf4j-impl:$log4jVer")
    implementation("org.jooq:jooq:$jooqVer")
    implementation("org.mariadb.jdbc:mariadb-java-client:$mariaDbVer")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin:$jacksonKotlinModuleVer")
r

rocketraman

02/01/2019, 5:48 PM
Ktor doesn't have its own version of log4j2 -- it uses slf4j as stated before. I haven't used the properties config in a while but it looks like you might be using a 1.x properties rather than a 2.x properties (not the same)... see "Configuration with Properties" at https://logging.apache.org/log4j/2.x/manual/configuration.html.
In any case, your issue appears to have nothing to do with Ktor, as even straight logging does not work in your environment, so you'd do better asking on the log4j2 users list.
🆗 1
305 Views