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

Krystian Rybarczyk

11/04/2020, 2:42 PM
hey guys, I’ve found this sample for structured logging: https://github.com/ktorio/ktor-samples/blob/1.3.0/other/structured-logging/src/Application.kt but it’s not quite clear to me how I would use this logger inside other classes (say my service class) so that I can see all other log messages in the context of a particular requestId. Could anyone help?
👀 1
@Deactivated User @katokay may I ask your advice as the authors? 🙂
d

Deactivated User

11/06/2020, 11:26 AM
As far as I recall and looking at the code, the logger is part of either the Call or the Pipeline, let me check
Copy code
public val Application.log: Logger get() = environment.log
With the Application or the Environment instance you have access to the Logger, you can get it from there when configuring the app, and propagate it to your code as required
k

Krystian Rybarczyk

11/06/2020, 11:34 AM
right, so I would need to inject either
Application
or
Environment
to the classes where I’d like to use the logger, correct?
d

Deactivated User

11/06/2020, 11:44 AM
I don't recall if the logger instance could be replaced, but probably not. So you can retrieve the logger and pass the logger directly. And yeah, if you prefer to propagate the Application or the Environment that should be ok too I guess
k

Krystian Rybarczyk

11/06/2020, 11:45 AM
thanks a lot! I’ll give it a go!
👍 1
23 Views