hey guys, I’ve found this sample for structured lo...
# ktor
k
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
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
right, so I would need to inject either
Application
or
Environment
to the classes where I’d like to use the logger, correct?
d
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
thanks a lot! I’ll give it a go!
👍 1