log-prepend

Prepend a string to each line of console output

Usage no npm install needed!

<script type="module">
  import logPrepend from 'https://cdn.skypack.dev/log-prepend';
</script>

README

log-prepend

Create a console.log or console.error like function, but prepend a string to each line.

API

const {lp} = require('log-prepend');
const log = lp(' [suman] ', process.stdout);
const logerr = lp(' [suman error] ', process.stderr);

log('a','b','c');
log('log1', 'log2\n3',4,5 + '\n55');

To use colors in the prepending string, simply do:

const chalk = require('chalk');
const log = lp(chalk.blue(' [suman] '), process.stdout);
const logerr = lp(chalk.red(' [suman error] '), process.stderr);

Extra

This works as a rudimentary solution:

const log = console.log.bind(console, ' [suman] ');

But the problem with the above log function is that it won't handle new lines chars that are passed to it.