@luca_scorpion/tinylogger
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

tinylogger

npm npm bundle size dependencies

A no-nonsense, dependency-free, minimalistic logger.

Usage

const { Logger } = require('@luca_scorpion/tinylogger');
// or:
// import { Logger } from '@luca_scorpion/tinylogger';

const log = new Logger('my-log');
const anotherLog = new Logger('another-logger');

log.debug('You cannot see me, because the default log level is INFO');
log.info('Tell me more, tell me more.');

anotherLog.warn('Something might be going on.');
anotherLog.error('Panic! Red alert!');

The above example will print:

2020-09-14T13:23:50.508Z [INFO ] my-log         | Tell me more, tell me more.
2020-09-14T13:23:50.510Z [WARN ] another-logger | Something might be going on.
2020-09-14T13:23:50.510Z [ERROR] another-logger | Panic! Red alert!

Minimum Log Level

The logger will automatically set the global minimum log level based on the LOG_LEVEL environment variable. This can also be changed by setting Logger.logLevel:

import { Logger, LogLevel } from '@luca_scorpion/tinylogger';

Logger.logLevel = LogLevel.DEBUG;

const log = new Logger('my-logger');
log.debug('Now you will see me!');

Custom Logging Handlers

By default, all messages will be logged to the console. One or more custom handlers can be set for each level using Logger.setLogHandlers:

import { Logger, LogLevel } from '@luca_scorpion/tinylogger';

const handleLog = (message: string) => {};
const handleError = (message: string) => {};

Logger.setLogHandlers({
  [LogLevel.INFO]: handleLog,
  [LogLevel.WARN]: handleLog,
  [LogLevel.ERROR]: handleError,
});

If a handler is not defined for a log level, it will be ignored.

Alternatively, you can also pass a single handler to use for all levels:

import { Logger } from '@luca_scorpion/tinylogger';

const handleLog = (message: string) => {};

Logger.setLogHandlers(handleLog);

You can also pass multiple handlers, for example to handle error messages differently:

import { Logger, LogLevel, getConsoleLogHandlers } from '@luca_scorpion/tinylogger';

const handleError = {
  [LogLevel.ERROR]: (message: string) => {}
};

Logger.setLogHandlers(getConsoleLogHandlers(), handleError);

The getConsoleLogHandlers function returns (a copy of) the console handlers which are used by default.

Dependents (0)

Package Sidebar

Install

npm i @luca_scorpion/tinylogger

Weekly Downloads

0

Version

1.0.0

License

WTFPL

Unpacked Size

14.1 kB

Total Files

6

Last publish

Collaborators

  • luca_scorpion