tilo
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

tilo

CI Build Status Coverage Status npm release license TypeScript

© 2021, Onur Yıldırım (@onury). MIT License.

Tiny logger with styles and levels for Node/TypeScript.

npm i tilo

Usage

// Node/CommonJS environments
const { Tilo } = require('tilo');

// With modern ES / transpilers
import { Tilo } from 'tilo';

// Usage:
const tilo = new Tilo({ level: 'debug' });
tilo.info('Output colorful logs with date/time and level info.');

Read the concise API reference.

Formatted Output

You can provide a custom function that returns a formatted string.

tilo.format = (info, chalk) => {
    const text = `${info.time} ${info.level.toUpperCase()}\t${info.text}`;
    return info.level === Tilo.Level.ERROR
        ? chalk.red(text)
        : chalk.white(text);
};
tilo.info('Custom formatted log...');
// —> 15:30:43 INFO   Custom formatted log...

Safely Stringified Logs

You can log safely strigified objects (i.e. with circular references).
Use #s() to stringify an individual or multiple values.
#sp() is for stringify with pretty format & indents.

tilo.info(tilo.s({ key: 'stringify' }));
tilo.warn(tilo.sp({ key: 'stringify pretty' }));

Configuration

Pass an options object to constructor, with the following properties:

Option Type Default Description
enabled boolean true Whether log output is enabled.
level string "debug" Logging level. You can use Tilo.Level enumeration.
format LogFormatFn Tilo.defaultFormat Function for formatting and styling. Set to null to disable formatting.
styles boolean true Specifies whether styles and colors are enabled.
streams ILogLevelStreams | NodeJS.WriteStream process.stdout A hash-map of objects that defines write streams for each individual log level. If set to a single stream, it's treated as default for each log level.
cleanStack boolean | string[] false If set to true, stack lines with no file-path in them will be removed. Or set to a list of case-sensitive keywords to be filtered out from the stacks.

Log Levels and Methods

Level / Priority Methods Details
ERROR 0 #error() Error logs. Always printed to the stream.
WARN 1 #warn() Warning logs.
INFO 2 #info() Informational logs. Alias: #ok()
#plain() Regardless the configuration, output has no formatting. Clean text.
#table() Prints a visual table to the stream with the given data.
VERBOSE 3 #verbose() Verbose logs.
DEBUG 4 #debug() Debug logs.
#dir() Inspects the given object and prints the result to the stream.
#trace() Prints the message to the stream with stack trace to the current position in code.
SILLY 5 #silly() Silly logs.

There is also a #log() method which is INFO level by default. But you can pass the log level as the first argument to change that:

tilo.log('debug', 'message...');

Log Event

Tilo is also an EventEmitter. You can run custom functionallity on the log event.

tilo.on('log', logInfo => {
    if (logInfo.level === 'error' && /\bfatal/i.test(logInfo.text)) {
        // e.g. send email to admin
    }
});

Changelog

v2.0.1 (2023-03-20)

  • Handle edge-case inputs.

v2.0.0 (2020-01-31)

  • Drop support for Node v6.
  • Update dependencies to latest version.

v1.0.0 (2019-01-28)

  • initial release.

License

MIT.

Package Sidebar

Install

npm i tilo

Weekly Downloads

2

Version

2.0.1

License

MIT

Unpacked Size

54.5 kB

Total Files

23

Last publish

Collaborators

  • onury