Skip to main content

Outputs

Outputs determine where and how log records are written.

Built-in Outputs

(You don't need to pass these in if you're happy with the default behavior. Note that the format convenience setting on the Logger will override/replace the outputs array if used.)

import { ConsolePrettyOutput, ConsoleJsonOutput } from '@versionzero/logger';

// if a terminal, use pretty formatting. otherwise default to data logging.
let consoleOutput = tty.isatty(1)? new ConsolePrettyOutput() : new ConsoleJsonOutput();

// Create logger with selected output
const logger = new Logger({outputs: [consoleOutput]});

Custom Outputs

Create custom outputs by implementing the output(record) method:

class SlowSlowMessageOutput {
constructor() {
this.queue = [];
this.processing = false;
}
// synchronous call! (anything complex should be queued for asynchronous processing)
output(record) {
// this example output filters records based on the value of a field...
if (record.dt > 10000) {
this.queue.push(record);
void this.processQueue(); // fire and forget
}
}
async processQueue() {
if (this.processing) { return }
this.processing = true;
while (this.queue.length > 0) {
await setTimeout(1000);
const record = this.queue.shift();
console.log('slowly logged slow transaction:', record.msg);
}
this.processing = false;
}
}

const slowLogger = new Logger({ outputs: [new SlowSlowMessageOutput()] });

for (let i = 0; i < 100; i++) {
let dt = Math.random() * 11000;
slowLogger.info({i, dt}, `transaction ${i} took ${dt}ms`);
}
logger.notice('slow logs are now being processed asynchronously');