Comments (8)
Thank you for the opinions. The common idea on the ground seems that the logger should be static
. We need to think about this.
- Like I said, it may not necessarily be
static
. What is the traditional purpose of beingstatic
? What I know is that we need only one logger, and we don't want to create it twice. And it is ALREADY one, it is created viaobject
, so it is singleton. It will never be created twice in the name of kotlin. - The performance between the approach to get the only instance from singleton and from the static is controversial nowadays, more and more people believe singleton logger is fast enough compared to the static logger. Some people even believe the instance is now faster than the static object. The logger instance in the heap memory also has some benefits that the static logger doesn't.
- The second important view is that, it still works anyway if you put it in the class property. It can be considered as another traditional pattern as well to put logger in the source code. So why is it considered bad and should be avoided? I believe we need clearer reason not to put it inside the class other than "to make it static".
ps. @DanielGolan-mc You are right, I don't want to put it in the companion object, either. It sucks.
from kotlin-logging.
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:
- outside of the class.
- in an object / companion object.
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.
Other options are still possible to some extent.
Hope that clarifies things.
from kotlin-logging.
What if we allow a logger per instance of the class as the standard style?
This is inefficient in terms of memory usage (additional pointer for each instance) so not recommended. Also in java it's usually static.
You can still do it, but it's not going to be the recommendation.
from kotlin-logging.
Thank you for reporting an issue. See the wiki for documentation and slack for questions.
from kotlin-logging.
Kotlin doesn't support static members yet, for details you can see KEEP-348.
from kotlin-logging.
class MyClass {
companion object {
internal val logger = KotlinLogging.logger { }
}
}
This is a semi-static variation of what you want. Using private
here won't allow the class to use the logger, and using internal
allows the entire project to use this logger. So for now, declare the logger outside of the class. Shouldn't turn your class's file icon into the kotlin symbol instead of a class icon if you care about that.
In my own library, I just turned the loggers into a part of some classes' lifecycles.
from kotlin-logging.
Thanks @oshai , I think that clarifies things enough.
I think the current style is good enough for the standard of kotlin-logging.
I'd just like to ask an additional question before closing this issue.
What if we allow a logger per instance of the class as the standard style?
There are some benefits that comes out of my head:
- We can stick to the java convention, which states that a file contains a class. This idea is not mandatory on the kotlin side though.
- We can have a bit smoother IDE support in the situations like duplicating source codes.
- There would be no confusion between loggers in the files that shares the same package names.
com
└── me
└── example
└── service
├── MyExample1.kt
└── MyExample2.kt
In this example above, the full names of the logger
fields would be: com.me.example.service.logger
,
and these loggers serves different classes, in this case MyExample1
and MyExample2
.
from kotlin-logging.
Thanks for the explanation. I close this issue now.
from kotlin-logging.
Related Issues (20)
- Payload not available to JS HOT 7
- underlyingLogger got a lot more complex HOT 5
- sflj jdk backend does not recognize class name HOT 14
- Gradle logging HOT 2
- Problems regarding Android HOT 3
- Problem using LogstashMarker HOT 15
- Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory HOT 2
- Possible to add a module-info.java for improved JPMS support? HOT 3
- setting logging level per KLogger instance in run-time HOT 4
- Built JAR Errors - SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder" HOT 3
- Darwin doesn't log stacktraces HOT 2
- Publish Dokka HTML docs on project site HOT 2
- Why was KLogging deprecated? HOT 4
- KLoggingEventBuilder accept nullable values in payload HOT 4
- IllegalArgumentException when message includes {} HOT 5
- Android native targets missing. HOT 3
- Wrong class name printed when using LoggingEventBuilder with payload HOT 10
- Undo workaround for Gradle issue #11412 HOT 1
- Add timestamp to the KLoggingEvent HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotlin-logging.