How can I change the logging level in k6 for better debugging information?

I’m running performance tests using k6 on a Windows 10 environment, and I’m trying to get more detailed logs during execution. I’ve tried using the log package but haven’t been able to adjust the logging levels properly. I expected to see more detailed logs like HTTP requests and responses, but I’m only getting minimal information. How can I change the k6 logging level to get more comprehensive debug data? What might I be missing in my configuration?

Ran into this issue myself when I first started with k6 logging. It can be frustrating, but adjusting the log level is pretty straightforward once you know how.

In k6, logging is controlled by setting the K6_LOG environment variable to your desired log level. The available log levels are DEBUG, INFO, WARN, and ERROR. By default, k6 uses the INFO level, which outputs general operational messages. If you want to see more detailed execution information, especially for debugging, you can set it to DEBUG.

Here is the command I use to change the log level:

export K6_LOG=DEBUG
k6 run script.js

This command sets the environment variable to DEBUG before running your script, which enables verbose logging, including HTTP request and response details. Remember to replace “script.js” with the path to your actual script file.

One common mistake is forgetting to set the environment variable before running the test or using an incorrect log level name. Make sure to use the exact case-sensitive name as shown above.

As a tip, always revert your logging level back to INFO or WARN in production to avoid large log files and potential performance hits.

I initially tried using the default settings and got stuck with minimal logging. After experimenting a bit, I found a different way that helped me get detailed logs for my specific use case.

In addition to setting the K6_LOG environment variable, you can use command-line flags to directly control the verbosity. If setting the environment variable doesn’t work in your setup, try specifying the log level when starting your k6 test.

Here’s the command that worked in my situation:

k6 run --verbose script.js

This command enables verbose output, providing more detailed information including requests being made. It’s helpful in scenarios where environmental settings might not be read due to execution context constraints.

One thing to note is that the --verbose flag automatically adjusts the log level to DEBUG, which might be overkill if you’re only looking for certain types of logs. Adjust according to your needs and the volume of data your tests generate.

If running in a CI/CD pipeline, consider how much information you want logged to avoid overwhelming your build logs.