Transforming
The act of transforming is to take a value and change it or manipulate it in some way. In Lipe this is achieved by passing a function to the Pipe
method on the LoggerPipe.
const pipe = new LoggerPipe().Pipe((message) => "Hello " + message);
this will mean that if we now use that pipe and log “World”
const logger = new Logger();
logger.AddPipe(pipe.Pipe(Console));
logger.Log("World!");
we get back Hello World!
.
This can be used in various ways, from changing the format to censoring certain strings such as API keys or potential passwords.
an example of filtering the pipe for config values
const config = require("config.json")
const logger = new Logger();
logger.AddPipe(new LoggerPipe([
(msg) => {
Object.values(config).forEach(v => {
msg = msg.replace(v, "[REDACTED]")
})
return msg;
},
Console
]));
logger.Log("API key in config is: {key}", {key: config.apiKey});
// results in: "API key in config is: [REDACTED]"
Its not just limited to simple string manipulation. Each step can stop or change the execution of the entire pipe based on what it returns.
undefined
andtrue
are ignored and continue execution as normalnull
andfalse
will halt execution of the whole pipe preventing any further function from being calledstring
will overwrite the current message with whatever was returnedLoggerPipe
will execute the provided LoggerPipe with the current arguments and then resume execution
Any other value is treated as an error and will result in the halting of the execution.