Skip to content
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

Turn off logging #37

Closed
excitement-engineer opened this issue Mar 6, 2018 · 18 comments
Closed

Turn off logging #37

excitement-engineer opened this issue Mar 6, 2018 · 18 comments

Comments

@excitement-engineer
Copy link

Thanks for this project. Really great stuff!!

I was wondering whether it is possible to turn off logging? I am getting all kinds of output such as INFO messages in the console.

Thanks in advance

@tomkoptel
Copy link

👍 +1 I am looking for the same option. What about?

val dependency = mockk<Dependency>(logging = false)

@oleksiyp
Copy link
Collaborator

oleksiyp commented Mar 6, 2018 via email

@tomkoptel
Copy link

What do you propose? To disable info and debug logging? I don't think it is a good idea to remove logs. I would still prefer to see the option to enable/disable logs.

@oleksiyp
Copy link
Collaborator

oleksiyp commented Mar 6, 2018

I propose to not have info logs, so by default nothing is shown. There is just one info message saying what is an answer. On the other hand you always can disable/show logging in logback.xml(any other SLF4J framework) anyway. Doing it per mock is not an option because it would need to revisit each logging message and somehow link it to this flag.

@tomkoptel
Copy link

It sounds like it is required to have SLF4J on the classpath and then to include logback.xml in the resources folder. Is it correct?

@oleksiyp
Copy link
Collaborator

oleksiyp commented Mar 6, 2018

Yes, logback-test.xml even better

@excitement-engineer
Copy link
Author

I like the idea of not showing any logs by default. Frameworks like mockito also don't do this for example. I think that the logs are only useful when debugging the code so being able to easily turn this on/off would be a win I think.

@oleksiyp
Copy link
Collaborator

oleksiyp commented Mar 6, 2018

The idea was to keep one top-level info message. It can help to see what has answered actually in your tests by mocking library. But I think this easily can be switched to DEBUG

@tomkoptel
Copy link

Me personally not against the logging it was a good idea, but when you put additional logs in the source code to see sequence call with your tests it becomes a little bit noisy from the mock instances. I would still prefer to have logs, but with an option to disable them on demand. To use SLF4J sounds like a solution, but it requires to add yet another lib just for the sake of disabling logs, which sounds like an overkill.

@oleksiyp
Copy link
Collaborator

oleksiyp commented Mar 8, 2018

Version 1.7.10 released, please check if by default has no logging and close the ticket.

@excitement-engineer
Copy link
Author

Awesome thanks so much, it works:) Really appreciate it!

@charlesritchea
Copy link

@oleksiyp I'm confused by this. I don't want mockk logging and I use slf4j myself, but my logback is handled by dropwizard, so I don't have a logback.xml or test-logback.xml. Do I still need to create and configure a test-logback.xml to disable logging. It makes finding build failures in gradle output frustrating because there are all these mock DEBUG statements I have to wade through

@oleksiyp
Copy link
Collaborator

oleksiyp commented Oct 31, 2018

Unfortunately, DEBUG is what MockK cannot live without. I usually request to turn on DEBUG logging to capture what is happening on the other side. It is pretty weird that it is turned on by default for tests with Dropwizard. So you need to configure test-logback.xml to fix it.

I can think of a switch in MockK properties, but you will still need to create some file with settings, so that is almost the same as creation of test-logback.xml

@charlesritchea
Copy link

charlesritchea commented Oct 31, 2018 via email

@AlexCzar
Copy link

I tried adding test-logback.xml (and logback-test.xml) with following content:

<configuration>
	<root level="warn"/>
</configuration>

This doesn't seem to affect logging in any way, my log is still full of [Test worker] DEBUG io.mockk.impl... lines.
How do I disable this?

@laxa88
Copy link

laxa88 commented Mar 1, 2022

For those curious:

Mockk uses Logback.

You need to add an logback-test.xml or logback.xml file in the resource folder. Any other filename won't be recognised.
Screen Shot 2022-03-01 at 11 33 24

Then, in the xml, add something like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%msg%n</pattern>
        </encoder>
    </appender>
    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

Notes:

  • Set debug="false" if you want to turn off logging completely.
  • Change the value to level="info" or "warn" as you see fit.
  • You need to define an <appender> first, then use it in the <appender-ref>.
  • You need to define the message pattern in <pattern>
  • Note the the ref can be any arbitrary value instead of STDOUT.

Some links for reference:

@erksch
Copy link

erksch commented Aug 29, 2023

After multiple hours I am still unable to turn off the DEBUG logs of mockk in my multiplatform project.

I enabled standard streams for my test logging:

testLogging {
  showStandardStreams = true
}

I tried to add the logback.xml or logback-test.xml to shared/src/commonTest/resources/.
I also tried to only enable standard streams for INFO log level only:

testLogging {
  info { 
    showStandardStreams = true
  }
}

But nothing seems to work. I want logging for all my INFO level logs but not the DEBUG level logs from mockk.
Has anyone found a solution, or could explain how to verify that the logback setup works?

@erksch
Copy link

erksch commented Oct 20, 2023

I got it to work in my multiplatform project where I have tests in commonTest.
The trick was to not add logback.xml to shared/src/commonTest/resources/ but to shared/src/test/resources/!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants