Enhanced logging

I was after a way to enhance the logging output when making simple requests and/or collection runs. This replicates the unix logging levels.

Creates the following commands;
log.[debug|info|notice|warn|error|crit|alert|emerg](“message”) // sends messages to the console
log.test(“message”) // sends messages to the test window

Implementation
Define the following as the variable logging at the collection level.

({
    test:function(s) {
        pm.test("[TEST]" + request.name + ": " + s)
    },
    emerg:function(s) {
        console.log("[EMERG]" + request.name + ": " + s)
    },
    alert:function(s)  {
        if (levels.length > 1) { console.log("[ALERT]" + request.name + ": " + s) }
    },
    crit:function(s)  {
        if (levels.length > 2) { console.log("[CRIT]" + request.name + ": " + s) }
    },
    error:function(s)  {
        if (levels.length > 3) { console.log("[ERROR]" + request.name + ": " + s) }
    },
    warn:function(s)  {
        if (levels.length > 4) { console.log("[WARN]" + request.name + ": " + s) }
    },
    notice:function(s)  {
        if (levels.length > 5) { console.log("[NOTICE]" + request.name + ": " + s) }
    },
    info:function(s)  {
        if (levels.length > 6) { console.log("[INFO]" + request.name + ": " + s) }
    },
    debug:function(s)  {
        if (levels.length > 7) { console.log("[DEBUG]" + request.name + ": " + s) }
    }
})

Then at the top of each request add the following.
log = eval(pm.variables.get(“logging”));

This is required because single requests do not pickup any tests from the collection. Not sure why it is that way but it is, otherwise this would be a single command at the collection level.

Usage
log.debug(" This is an interesting fact")
log.notice(“Cookies have been cleared”)
log.test(“There is a reason why this failed”)

Console Output
[DEBUG]Request Name: This is an interesting fact.
[NOTICE]Cookie Manager: Cookies have been cleared.

Test Results Window
STATUS There is a reason why this failed

Feedback wanted.

Great stuff @rtfmoz2

I tend to use the built-in log-level options (log, info, warn and error), which provides the ability to filter for a specific log-level and the icon/colour combo, allows me to parse the whole log quickly to focus on a message.

1 Like

Ahh Crap! Its already there! LOL Only been doing this for a week!! :slight_smile: Well thats excellent then! My end goal was to add logging filtering…

Inside the above add…

level:function(s)  {
    levels = { "debug","info","notice","warn","error","crit","alert","emerg" }
    while  ( !levels[0].equals(s)) { levels.shift() }
},

So you would only see the level of logging you want in the console output with

log.level("notice")

But as you said its already there!