Want to caputre only error response

Hi,

I want to capture the error on file, I have tried using --http-debug=error but it writes for every request. I want to write a log only for the req which gives an error.
please suggest some approach as I’m not able to find the error while the load test
this is the command I used to capture the error
k6 run --logformat raw --http-debug=error script.js 2>test.csv

also i want to know more about --logformat please mention if any documentation is there coz in k6 doc nothing much is there

Hi @ializrocks !

There is no value error for the --http-debug. It’s either --http-debug or --http-debug="full". These options are for HTTP debugging.

For your needs can work something from below:

import { sleep, check } from 'k6';
import http from 'k6/http';

export default function () {
  
  let resp = http.get("https://httpbin.test.k6.io/status/400/");

  let passed = check(resp, {
        "status is 200": (r) => r.status === 200,
        "content is present": (r) => r.body.indexOf("whatever") !== -1,
  });
  
  if (!passed) {
      console.log(`Request to ${resp.request.url} with status ${resp.status} failed the checks!`, JSON.stringify(resp));      
  }

  sleep(1);
}

And run with:

 k6 run --log-format json --log-output=file=./k6.log test.js

Regarding a --log-format it’s pretty simple. It controls the format of the logs’ output. It’s either raw (print only the log message) or JSON (print all the debug information in JSON format).

Let me know if that helps,
Cheers!

Hi, @olegbespalov thanks for sharing this info, is it possible to just log error request data and not all
It would be really helpful in the load test we have thousands of request

Hi @ializrocks

Sure, you can log whatever details you want (like the only request data) and with your definition of the failed request (just adjust check or use any other logic condition).

Let me know if that answers,
Cheers