Best way to toggle between enabled and disabled console logs?

Scenario: I have a collection with multiple requests and within each request there are numerous console.log lines that write out variable names/values creating a detailed trace of what is going on. With the amount of data I’m looping through and writing out to the console, postman becomes very sluggish (Which I expect given how much is writing out).

What would be the best way to conditionally toggle logging on/off? I like the idea of having all of the logging for trace and troubleshooting purposes, but I don’t want it enabled full time.

The only thing I can think of is set a postman variable e.g. “EnableConsoleLog” and give it a true/false value.

Then within my scripts use an if conditional to check that variable value to determine whether or not my console.log line gets hit or not.

Is there a cleaner or better approach to doing this?

1 Like

Sorry we didn’t get to this sooner. This isn’t the experience we want for our users.

Not the most elegant of solutions but something like this could work, it could be placed at the Collection level or in the Package Library. It’s checking if the EnableConsoleLog variable is present rather than if it’s a particular value and then turning on/off logging.

let EnableConsoleLog = pm.environment.has("EnableConsoleLog") ? true : false;

function logMessage(message, EnableConsoleLog) {
    if (EnableConsoleLog) {
        console.log(message);
    }
}

logMessage('This is a log message', EnableConsoleLog);