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.