Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

fifo-logger

cheminfo4.2kMIT2.0.1TypeScript support: included

Simple event logger for the browser and node.js

readme

fifo-logger

NPM version npm download test coverage license

Simple event logger for the browser and Node.js inspired by pino.

By default, it will keep the 10'000 last events that can easily be retrieved and filtered.

Installation

npm install fifo-logger

Simple usage

import { FifoLogger } from 'fifo-logger';

const logger = new FifoLogger({
  limit: 1000, // default value
  level: 'info', // by default we will not log the level under 'info' (trace and debug)
});

logger.trace('a trace');
logger.debug('a debug');
logger.info('an info');
logger.warn('a warning');
logger.error('a error');
logger.fatal('fatal');

// you have also the possibility to log an object or object + message

logger.warn({ a: 1, b: 2, c: 'Hello' }, 'a warning');

// errors can also be directly added to the logger

logger.fatal(new Error('a fatal error'));

// to get the logs

const logs = logger.getLogs();

Logging in a specific context

A child logger may be created in order to store specific related logs. Each logger or child logger will receive a specific UUID.

import { FifoLogger } from 'fifo-logger';

const logger = new FifoLogger();
logger.info('an info');

const childLogger = logger.child();
childLogger.info('from child');

const grandChildLogger = childLogger.child();
grandChildLogger.info('from grandchild');

const anotherLogger = logger.child();
anotherLogger.info('from another child');

console.log(logger.getLogs()); // 1 element
console.log(logger.getLogs({ includeChildren: true })); // 4 elements
console.log(childLogger.getLogs()); // 1 element
console.log(childLogger.getLogs({ includeChildren: true })); // 3 elements

Callback when new logs are added

If you need to update the log list based on new addition you can add the onChange callback

const logger = new FifoLogger({
  onChange: (log, logs, info) => {
    console.log(log, logs, info);
  },
});
logger.info('Hello world');

// info contains 'depth' starting at 1

Callback with throttling

Libraries may be quite verbose and you can throttle the callback using such a code

import { throttle } from 'throttle-debounce';

import { FifoLogger } from 'fifo-logger';

let results: any = [];

let throttleFunc = throttle(100, (log, logs) => {
  results.push(log.message);
  results.push(logs.length);
});

const logger = new FifoLogger({
  onChange: throttleFunc,
});

logger.info('first info');
logger.info('second info');

const start = Date.now();
while (Date.now() - start < 120);

logger.info('an info after 120ms');

console.log(results);
// ['first info', 1, 'an info after 120ms', 3]

In the browser

<html>
  <head>
    <script
      src="https://www.lactame.com/lib/fifo-logger/0.3.0/fifo-logger.js"
      type="text/javascript"
    ></script>
    <script Logger="fifo-logger" type="text/javascript">
      const logger = new FifoLogger.FifoLogger({
        onChange: (log, logs) => {
          console.log(logs);
          document.getElementById('logs').innerHTML =
            '<table><tr><th>Level</th><th>Message</th></tr>' +
            logs
              .map((log) => {
                return `<tr><td>${log.level}</td><td>${log.message}</td></tr>`;
              })
              .join('') +
            '</table>';
        },
      });
    </script>
  </head>
  <body>
    <button onclick="logger.warn('warning')">Warning</button>
    <div id="logs"></div>

    <script>
      logger.info('Hello World!');
      logger.error('This is an error');
    </script>
  </body>
</html>

License

MIT

changelog

Changelog

2.0.1 (2025-08-13)

Bug Fixes

2.0.0 (2025-06-25)

⚠ BREAKING CHANGES

  • make project ESM-only and modernize structure (#28)

Code Refactoring

  • make project ESM-only and modernize structure (#28) (dabbfac)

1.0.0 (2024-04-05)

⚠ BREAKING CHANGES

  • replace onChange callback option with "change" event

Features

0.7.0 (2024-04-04)

Features

0.6.1 (2023-03-16)

Bug Fixes

  • children share the same incremental id (c7f6ea9)

0.6.0 (2023-03-15)

Features

0.5.4 (2023-03-14)

Bug Fixes

  • set correct types to level and levelLabel properties (#11) (26b4c5d)

0.5.3 (2023-03-09)

Bug Fixes

  • deal correctly with empty message (92b8b74)

0.5.2 (2023-03-09)

Bug Fixes

  • logger limit was not passed to child logger (681cf2b)

0.5.1 (2023-03-09)

Bug Fixes

  • logger level was not passed to child logger (7a6b820)

0.5.0 (2023-03-07)

Features

0.4.0 (2023-03-07)

Features

  • add setLevel and setLimit (4377c98)

Documentation

  • add info about usage in the browser (9cfa5f3)

0.3.0 (2023-03-07)

Features

Documentation

  • add docs for bindings (5216421)
  • correct new Logger in readme (19cde0e)

0.2.0 (2023-02-28)

Features

0.1.0 (2023-02-26)

Features

  • add onChange callback (d5ad5e6)
  • getLogs returns only the current context (c24acb5)

Documentation

0.0.1 (2023-02-24)

Features

  • working version of the FifoLogger (7a87d1e)

Bug Fixes

  • code should work in the browser and node.js in the same way (de813c3)

release-as