Module swanling::logger[][src]

Expand description

An optional thread for writing logs.

Swanling can generate a number of log files during a load test, enabled through any combination of the following run time options:

  • --debug-log, --request-log, --task-log

It’s also possible to configure the format of any of thse logs to be json, csv, or raw (the standard debug output of a Rust structure), using the following run time optios:

  • --debug-format, --request-format, --task-format

All of these loggers use a single shared logger thread, with SwanlingUsers sending log messages through the same shared channel. The logger determines which log file to write the message to based on the message data type. The logger thread uses Tokio’s asynchronous BufWriter. The logger thread only starts if at least one logger is enabled.

Note: there’s also a --swanling-log run time option which records any errors or messages generated by Swanling while running a load test. This functionality is not implemented in this file.

Request File logger

The Swanling requests logger is enabled with the --request-log command-line option, or the SwanlingDefault::RequestLog default configuration option. The format of the log is configured with the --request-format command-line option, or the SwanlingDefault::RequestFormat default configuration option.

Each SwanlingRequestMetric object generated by all SwanlingUser threads during a load test is written to this log file.

Task File logger

The Swanling tasks logger is enabled with the --task-log command-line option, or the SwanlingDefault::TaskLog default configuration option. The format of the log is configured with the --task-format command-line option, or the SwanlingDefault::TaskFormat default configuration option.

Each SwanlingTaskMetric object generated by all SwanlingUser threads during a load test is written to this log file.

Debug File logger

The Swanling debug logger is enabled with the --debug-log command-line option, or the SwanlingDefault::DebugLog default configuration option.

Each SwanlingDebug object generated by all SwanlingUser threads during a load test is written to this log file.

Writing Debug Logs

Logs can be sent to the logger thread by invoking log_debug from load test task functions.

Calls to set_failure automatically invoke log_debug.

Most of the included examples showing how to use the debug logger include a copy of the request made, the response headers returned by the server, and the response body. It can also be used to log arbitrary information, for example if you want to record everything you sent via a POST to a form.

use swanling::prelude::*;

let mut task = task!(post_to_form);

async fn post_to_form(user: &SwanlingUser) -> SwanlingTaskResult {
    let path = "/path/to/form";
    let params = [
     ("field_1", "foo"),
     ("field_2", "bar"),
     ("op", "Save"),
    ];

    // Only log the form parameters we will post.
    user.log_debug(
        &format!("POSTing {:?} on {}", &params, path),
        None,
        None,
        None,
    )?;

    let request_builder = user.swanling_post(path).await?;
    let swanling = user.swanling_send(request_builder.form(&params), None).await?;

    // Log the form parameters that were posted together with details about the entire
    // request that was sent to the server.
    user.log_debug(
        &format!("POSTing {:#?} on {}", &params, path),
        Some(&swanling.request),
        None,
        None,
    )?;

    Ok(())
}

The first call to log_debug results in a debug log message similar to:

{"body":null,"header":null,"request":null,"tag":"POSTing [(\"field_1\", \"foo\"), (\"field_2\", \"bar\"), (\"op\", \"Save\")] on /path/to/form"}

The second call to log_debug results in a debug log message similar to:

{"body":null,"header":null,"request":{"elapsed":1,"final_url":"http://local.dev/path/to/form","method":"POST","name":"(Anon) post to form","redirected":false,"response_time":22,"status_code":404,"success":false,"update":false,"url":"http://local.dev/path/to/form","user":0},"tag":"POSTing [(\"field_1\", \"foo\"), (\"field_2\", \"bar\"), (\"op\", \"Save\")] on /path/to/form"}

For a more complex debug logging example, refer to the log_debug documentation.

Reducing File And Memory Usage

The debug logger can result in a very large debug file, as by default it includes the entire body of any pages returned that result in an error. This also requires allocating a bigger BufWriter, and can generate a lot of disk io.

If you don’t need to log response bodies, you can disable this functionality (and reduce the amount of RAM required by the BufWriter by setting the --no-debug-body command-line option, or the SwanlingDefault::NoDebugBody default configuration option. The debug logger will still record any custom messages, details about the request (when available), and all server response headers (when available).

Enums

If enabled, the logger thread can accept any of the following types of messages, and will write them to the correct log file.

Defines the formats logs can be written to file.