Giter Club home page Giter Club logo

Comments (21)

oshai avatar oshai commented on September 2, 2024 3

Added it here: https://github.com/MicroUtils/kotlin-logging/wiki/Coverage-reporting

from kotlin-logging.

bjpe avatar bjpe commented on September 2, 2024 1

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.

oshai avatar oshai commented on September 2, 2024

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):
image

from kotlin-logging.

snowe2010 avatar snowe2010 commented on September 2, 2024

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.

oshai avatar oshai commented on September 2, 2024

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.

snowe2010 avatar snowe2010 commented on September 2, 2024

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.

oshai avatar oshai commented on September 2, 2024

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.

oshai avatar oshai commented on September 2, 2024

@snowe2010 will you be able to test such flag or should I close the issue?

from kotlin-logging.

snowe2010 avatar snowe2010 commented on September 2, 2024

Sorry, I've been out of the country for a while. I will try to get back to this tonight!

from kotlin-logging.

snowe2010 avatar snowe2010 commented on September 2, 2024

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.

oshai avatar oshai commented on September 2, 2024

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.

snowe2010 avatar snowe2010 commented on September 2, 2024

Yes, thanks for the help!

from kotlin-logging.

sluongng avatar sluongng commented on September 2, 2024

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.

sluongng avatar sluongng commented on September 2, 2024

image
image

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.

oshai avatar oshai commented on September 2, 2024

@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 avatar sluongng commented on September 2, 2024

@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.

oshai avatar oshai commented on September 2, 2024

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.

sluongng avatar sluongng commented on September 2, 2024

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.

oshai avatar oshai commented on September 2, 2024

@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.

sluongng avatar sluongng commented on September 2, 2024

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.

oshai avatar oshai commented on September 2, 2024

I will add that to the wiki.

from kotlin-logging.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.