4

I get over 1 GB logs after each execution of functional tests if DEBUG level is set for all requests using the the following line in the logback-test.xml:

<logger name="com.intuit.karate" level="DEBUG"/>

And the logs are getting much bigger if gatling performance tests are executed with DEBUG level. Setting the DEBUG level is required to analyze the concurrency problems.

I would like to reduce the logs and log only details on failed requests, but setting just the ERROR level is not enough, because the failed request and response details are not logged:

<logger name="com.intuit.karate" level="ERROR"/>

Is it possible to configure DEBUG log level only on failed REST API requests? What would be the correct way to achieve that?

UPDATE: Thanks to the answer bellow I ended up with INFO level in logback-test.xml:

<logger name="com.intuit.karate" level="INFO"/>

and the following code in karate-config.js:

  // setup global hook to log details only on failed scenarios
  karate.configure('afterScenario', function(){
    var info = karate.info;
    if(info.errorMessage) {
       karate.log('failed',info.scenarioType+':',info.scenarioName);
       var r = karate.prevRequest;
       if(r) {
         var log = 'request: ' + r.method + ' ' + r.uri + '\n' + karate.pretty(r.headers)
         if(r.body) log += '\n' + r.body
         karate.log(log);
         karate.log('response: ' + response);
       }
    }
  })

UPDATE 2: found some limitations due to the code reuse (sub-calls). The hook won't log the details in following scenarios:

  Scenario: CRUD for a machine group.
    When call read('this:create-machinegroup.feature')
    Then call read('this:retrieve-machinegroup.feature@last-created')
    And call read('this:update-machinegroup.feature')
    And call read('this:delete-machinegroup.feature@single')
    And call read('this:retrieve-machinegroup.feature@404')

beloyar
  • 247
  • 3
  • 11

2 Answers2

1

You can use a global afterScenario() hook to log the karate.prevRequest and response if karate.info.errorMessage is not null.

https://github.com/intuit/karate#hooks

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • @beloyar no you can actually do this in `karate-config.js`, read the docs – Peter Thomas Dec 18 '19 at 00:49
  • The only limitation to this approach is my code reuse... if I call sub-scenarios with parameters, then no details are logged by this hook... seems, that I have to refactor all my code reuse... very sad to break the readability of tests just to get this hook working... – beloyar Dec 18 '19 at 07:50
0

If you enable this at the top of your test:

Background:
  * configure report = { showLog: true, showAllSteps: true}

And then, in your logback-test.xml file, you enable the root logger to be DEBUG level, then you will see your requests being logged on the report and it's easier to debug.

Then, to turn off debugging, simply change the log level back to INFO.

djangofan
  • 28,471
  • 61
  • 196
  • 289