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

Hi @olegbespalov
I use k6 to write load test, and would like to generate HTML report after run script.
I import import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js"; to my script, and would like to show fail http request in the report.
Now I can generate json file, but I have no idea how to show on report.
I an wondering how handleSummary function can show fail request. Thank you.

Thank you for your help.

Jennifer

Hi @jennifershih !

Currently, it’s impossible:frowning_face: The handleSummary works with the metrics, and the JSON file I mentioned in the previous answers contained log entries.

So the possible workaround could still be to save both artifacts, the HTML report, and test run log whenever you need manually open the log for investigations.

Hope that helps
Cheers

Hi @olegbespalov,
Thank you for your reply.