Comments (21)
Added it here: https://github.com/MicroUtils/kotlin-logging/wiki/Coverage-reporting
from kotlin-logging.
Good morning everyone,
yesterday I also stumpled upon JaCoCo marking the line
private val logger = KotlinLogging.logger {}
as untested. Adding the exclusion like in https://github.com/oshai/kotlin-logging/wiki/Coverage-reporting did not work, because the logger seems to be compiled into a static field in class MyClassKt
when defined as a top-level function as suggested on https://github.com/oshai/kotlin-logging/wiki. What helped a bit was to move the logger into a companion object
companion object: KLogging()
However, we have some classes where we want to use a KotlinLogger which extend classes that alredy define a logger (e.g., org.springframework.web.filter.GenericFilterBean
, where the logger in the companion object is then shadowed by the logger of the superclass. Due to this problem, we uniformly changed the logger name to kLogger
in our project, but this doesn't work anymore with the KLogging
class.
Maybe defining an own class similar to KLogging
with a different property name would help, but I would rather like to avoid adding a companion object just for logging. Another idea would be to use
private val logger = KotlinLogging.logger {"package.Class"}
but has other downsides, of course.
Does anybody has any other ideas?
from kotlin-logging.
I don't know what coverage tool you're using, but when I tested it in IntelliJ coverage it report as the line was visited (see the green mark on the side bar):
from kotlin-logging.
JaCoCo reports the line as visited, but missing a branch. IntelliJ doesn't report properly at all. It misses half the stuff that jacoco covers.
from kotlin-logging.
We can add method invocation which might solve the issue, but I'm hesitant to do that just for coverage since it is a waste of cpu cycles. Is adding it as a configuration a good alternative?
Another option is to use the inheritance flavour.
What do you think?
from kotlin-logging.
I'm not sure what you mean by
adding it as a configuration
And for the inheritance option, do you mean inherit a logging class?
from kotlin-logging.
adding it as a configuration will be something like starting your coverage run with a system property like -Dkotlin.logging.coverage
For inheritance I mean inherit KLogging as you can see in the example here: https://github.com/MicroUtils/kotlin-logging/wiki#usage
from kotlin-logging.
@snowe2010 will you be able to test such flag or should I close the issue?
from kotlin-logging.
Sorry, I've been out of the country for a while. I will try to get back to this tonight!
from kotlin-logging.
So the companion object version works just fine. Thank you very much. How does that actually work though? What is placing a logger
object into my object from the Companion? And I am still not sure I understand what you mean by
adding it as a configuration will be something like starting your coverage run with a system property like -Dkotlin.logging.coverage
from kotlin-logging.
OK, so can we close the issue? The thing is that coverage is checking your code and not companion code. About the flag it is not that important if it's working for you.
from kotlin-logging.
Yes, thanks for the help!
from kotlin-logging.
I would like to ask to reopen this issue.
Currently the recommended logger setup is not checked by jacoco code coverage. This may affect teams' adoption of this library since it affect their code coverage metric.
Its better to have a discussion on how to solve this issue moving forward
from kotlin-logging.
the above are the 2 screenshots of current jacoco coverage which showed missed instruction
Kotlin-logging 1.6.26
Jacoco 0.8.4
Surefire 3.0.0-M3
from kotlin-logging.
@sluongng thanks for raising the issue again. My suggested solution is to have a JVM arg like -Dkotlin.logging.coverageAggresive
and then activate it on the coverage run. What do you think about that? would you like to test that? or do you have other idea?
from kotlin-logging.
@sluongng thanks for raising the issue again. My suggested solution is to have a JVM arg like
-Dkotlin.logging.coverageAggresive
and then activate it on the coverage run. What do you think about that? would you like to test that? or do you have other idea?
The downside of adding JVM arg is that its a bit hard to check-in this arg into git unless you embeding it into a dockerfile or abstracting your project behind a Makefile layer so I dont find it to be an ideal solution.
I am relative new to kotlin but I think for start, we can just document a pattern
to exclude these logging methods in the Jacoco report generator. This pattern then will be check-in into git via maven pom file or gradle build file.
from kotlin-logging.
if you're using gradle or maven for coverage then I think commiting the JVM args to git is possible via its configuration.
Document a pattern is fine from my side, but from what I saw that excludes an entire class.
from kotlin-logging.
learning from Lombok, the generated code could be configured to have a "@generated" annotation attached and Jacoco automatically ignore code with said annotation
not sure if its applicable in this case?
from kotlin-logging.
@sluongng I saw you just opened the issue on jacoco itself. I think this is a good idea (I thought to do it myself) to see how they suggest solving the issue. Thanks!
from kotlin-logging.
as mentioned on jacoco/jacoco#903
adding <exclude>**/*$log$*.class</exclude>
into Jacoco configuration seems to solved the issue.
I suggest consider adding this tip into README.md or a wiki page
Thanks, cheers!
from kotlin-logging.
I will add that to the wiki.
from kotlin-logging.
Related Issues (20)
- Problem using LogstashMarker HOT 15
- Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory HOT 2
- Declaring logger outside the class vs. inside HOT 8
- 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 4
- 'NoClassDefFoundError: Failed resolution of: Lorg/slf4j/LoggerFactory' in kotlin multiplatform for android target HOT 2
- Needs a WASI target HOT 2
- Add a modifiable factory for SLF4J loggers HOT 2
- Improving Log Analytics by Indexing Cause Messages HOT 1
- add missing watchos targets HOT 1
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.