-
-
Notifications
You must be signed in to change notification settings - Fork 112
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
How to wrap an existing logger? #88
Comments
I am not sure I understand what you mean. Can you give a concrete example? |
I have a |
The api to do that is internal (ie: it's not part of the api), you can look for the method of |
Yeah i digged inside the code and saw it. I have little knowledge on how to use properly those loggers and I wasn't sure if creating a new logger was a good idea or not. |
I don't think you can, those classes are internal. |
You would ideally want to write code as follows: companion object {
val logger= KotlinLogging.wrap(slf4jLogger)
// OR
val logger= KotlinLogging.wrap { code.to.obtain.logger() }
} I notice there is a method: |
@corneil yes, you can see I mentioned this method in one of the comments above. The thing that is not clear to me is why do you need that over just using |
How one should behave then? A simple val MyContext.klog by lazy {
KotlinLogging.logger{}
} Is ok? If so why? |
how about: private val logger = KotlinLogging.logger {} in the file, just outside of the class? you can see the example here: https://github.com/MicroUtils/kotlin-logging#getting-started |
@lamba92 was the issue resolved? |
I have not tried it yet but I guess it is! Still, a wrapper would have been better! |
@lamba92 How about adding the wrap method to the KotlinLogging? also added an extension method: actual object KotlinLogging {
/**
* This method allow defining the logger in a file in the following way:
* val logger = KotlinLogging.logger {}
*/
actual fun logger(func: () -> Unit): KLogger = KLoggerFactory.logger(func)
actual fun logger(name: String): KLogger = KLoggerFactory.logger(name)
fun logger(underlyingLogger: Logger) = KLoggerFactory.wrapJLogger(underlyingLogger)
}
fun Logger.toKLogger() = KotlinLogging.logger(this) see also PR #90 |
Oh great that is great :) One could now create an extension property val MyContext.klogger
get() = KotlinLogging.logger(logger) It is fine but every time you use it you are creating a new object. What about adding a In any case I guess a lazy could work as: val MyContext.klogger by lazy {
KotlinLogging.logger(logger)
} But if you have more then one context it is not fine at all! |
You can do the map solution, but I think adding it to the library is not that great. You could argue the same even without the new functionality but I think that map will make things more complicated and might lead to memory leaks. |
I like this solution!
[image: --]
Corneil du Plessis
[image: https://]about.me/corneil
<https://about.me/corneil?promo=email_sig>
…On Fri, 16 Aug 2019 at 06:51, Ohad Shai ***@***.***> wrote:
@lamba92 <https://github.com/lamba92> How about adding the wrap method to
the KotlinLogging? also added an extension method:
actual object KotlinLogging {
/** * This method allow defining the logger in a file in the following way: * val logger = KotlinLogging.logger {} */
actual fun logger(func: () -> Unit): KLogger = KLoggerFactory.logger(func)
actual fun logger(name: String): KLogger = KLoggerFactory.logger(name)
fun logger(underlyingLogger: Logger) = KLoggerFactory.wrapJLogger(underlyingLogger)
}
fun Logger.toKLogger() = KotlinLogging.logger(this)
see also PR #90 <#90>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#88>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AADR35VWDE6ZZUQTLIA2ZCTQEYW6HANCNFSM4IKYB4TA>
.
|
Oh yeah, that's memory leaks 101 :P Anyway, the wrapper is fine :) Thanks! |
Misclick the close button |
fixed in 1.7.6 |
Say you already have an
org.slf4j.Logger
object and you want to wrap around it the capabilities ofKLoggeg
. Is it possible?The text was updated successfully, but these errors were encountered: