kru-node-logger

0.0.3 • Public • Published

Installation

npm install kru-node-logger

Usage

import log from 'kru-node-logger';

Basic Usage

log.write('Hello, world');
// > Hello, world

// printf style string formatting via util.format
// See: https://nodejs.org/api/util.html#utilformatformat-args

log.write('I have %d barrels of %s', 3, 'apples');
// > I have 3 barrels of apples

Fluent API Method Chaining

log.write('Before').indent().write('After');
// > Before
// > 	After

Logging Levels / Decorators

log.info('This is some information');
// stdout > [i] This is some information

log.warn('This is a warning');
// stderr > [!] This is a warning

log.success('This is a successful message');
// stdout > [✓] This is a successful message

log.error('This is an error message');
// stderr > [x] This is an error message

log.custom('foo', '[?] ', process.stderr, (e) => '<' + e + '>');
log.foo('This is a {custom} message');
// stderr > [?] This is a <custom> message

// log.pc is a reference to picocolors for color decorators.
// See: https://github.com/alexeyraspopov/picocolors
log.custom('foo', '[?] ', process.stdout, log.pc.red);

Indentation

log.indent(); // Add a single layer of indentation
log.indent(2); // Add X levels of indentation
log.dedent(); // Remove a single layer of indentation
log.dedent(2); // Remove X levels of identation
log.clearIndent(); // Remove all levels of indentation
log.setIndentString('\t'); // Sets indentation string (\t by default)

Instanced Usage

// Unique logging instances can be created which do not inherit or affect other logging instances, including the global one.
const myLogger = log.instance();
myLogger.indent().write('Foo');
log.write('Bar'); // Global logger unaffected.

// >     Foo
// > Bar

Miscellanous

log.prefix('[Fudge] '); // Set a prefix.
log.write('Hello!');
// > [Fudge] Hello!

log.prefix(); // Reset to no prefix.
log.write('Hello!');
// > Hello!

Potential Future Features

  • [ ] Table formatting similar to console.table()
  • [ ] Time/date prefixing
  • [ ] Blank line function
  • [ ] Horizontal rule insert
  • [ ] Instance clone()
  • [ ] Readline functionality (for user input)
  • [ ] Ability to rewrite current line
  • [ ] Clear console
  • [ ] Option to log directly to a file or other stream (possible by passing a fs stream when setting up custom logging levels, but API should be cleaner for this)

Readme

Keywords

Package Sidebar

Install

npm i kru-node-logger

Weekly Downloads

1

Version

0.0.3

License

MIT

Unpacked Size

16.6 kB

Total Files

5

Last publish

Collaborators

  • kruithne