basic question : I want all my log messages to be ...
# kotlin-logging
t
basic question : I want all my log messages to be pre-pending w/ a specific string, and that string is setup when the logger is created by appending to the end of the existing string. so you kinda create a tree of "child" loggers. not sure if that makes sense. How would i do that using kotlin-logging?
o
do you have an example from other framework? or more details?
t
class Logger( _lvl: LogLevel, val prefix: String) { fun newLogger(appName: String?): Logger { val child = Logger(level, "$prefix:$appName") childern.add( child ) return child }
something simple like that.
it just keep appending the text when you ask for a new logger.
o
I don't know of a way to do exactly that, but you can append it to logger name. another approach is mdc
if you give more details on the use case I might have more ideas
maybe with a wrapper to logger
t
basicaly I don't wantt code to just grab a logger out of thin air. I want them to be passed a logger. And every new logger is created with a string pre-pended to each message. that' string is appened to as new loggers are created.
So you log messages look like "MainApp | Object1 | ChildThing2 | WorkedTask : INFO log message text" etc... it's reflecting the "tree" of how this code is created, and what instances created it.
make sense?
o
I think there is no exact feature for that but mdc or the logger name as mentioned above might help
t
ok, I'll look at that. maybe I'll just wrap what I have around kotlin-logging. BTW: What's "mdc"?
t
I'll look at this. Here is a quick example of what I currently have (w/o the logging, just the string prefix sutff) : https://gist.github.com/jonross-st/29d4aeba015746ce6a878c2c41bd5502
o
In your example those prefix are constants and predefined. If that is the case I would just use it as the logger name like this: https://github.com/MicroUtils/kotlin-logging/blob/4037ec81faad2d9acb6c732565ac4d1a4745c17c/src/commonMain/kotlin/mu/KotlinLogging.kt#L13
t
hmm... but how would I know the existing string to append it?