-
-
Notifications
You must be signed in to change notification settings - Fork 116
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
Declaring logger outside the class vs. inside #364
Comments
Thank you for reporting an issue. See the wiki for documentation and slack for questions. |
Kotlin doesn't support static members yet, for details you can see KEEP-348. |
class MyClass {
companion object {
internal val logger = KotlinLogging.logger { }
}
} This is a semi-static variation of what you want. Using In my own library, I just turned the loggers into a part of some classes' lifecycles. |
Thank you for the opinions. The common idea on the ground seems that the logger should be
ps. @DanielGolan-mc You are right, I don't want to put it in the companion object, either. It sucks. |
We wanted to have one instance of logger per class / file (and not per instance of the class). In order to do that there are few options:
Initially both options were available, but in order to have one standard way we decided to recommend only the outside of the class style. It makes some sense to have a logger per file in most cases. So where to put the declaration is mostly a style thing, and the style we chose is to have it outside of the class. |
Thanks @oshai , I think that clarifies things enough. What if we allow a logger per instance of the class as the standard style?
In this example above, the full names of the |
This is inefficient in terms of memory usage (additional pointer for each instance) so not recommended. Also in java it's usually static. |
Thanks for the explanation. I close this issue now. |
According to the doc:
https://github.com/oshai/kotlin-logging#getting-started
This seems to be the official way to set up the logger.
The point is that the logger stands outside the class.
There is a fair reason as well looking at this wiki page:
https://github.com/oshai/kotlin-logging/wiki#obtaining-a-logger
My question is, what is the practical difference between putting the logger outside the class and putting it inside?
The code below still seems to work.
Actually, the latter style seems to be more preferred to the conventional java developers including my coworkers.
For the practical view, they say it was uncomfortable when trying to duplicate the original class code to start over,
as intellij was not able to copy the logger line outside the class, like the image below:
It is also a very common and de-facto standard java code convention that a file contains a single class.
I'm afraid the former style violates this convention, it's not java but kotlin though.
I also think that making the logger
static
wouldn't be such a big deal. It is a singletonobject
already.So going back to the question,
what is the difference between putting the logger outside the class and putting it inside?
And what should we use? Which is the better way?
What side effects are expected other than the explanation from wiki if we put the logger property inside the class?
Reference
The text was updated successfully, but these errors were encountered: