Logger

QuantaJS reports problems through its own logger: a store that persists its whole state, a getter that shadows a state key, a subscriber that throws. logger and LogLevel let you decide what gets printed.

import { logger, LogLevel } from '@quantajs/core';

These diagnostics are only emitted in development. In a production build, where your bundler sets process.env.NODE_ENV to "production", they are skipped. Failures your code has to handle are not only logged: they are thrown, or passed to callbacks such as persist.onError.

Log levels

enum LogLevel {
  DEBUG = 0,
  INFO = 1,
  WARN = 2,
  ERROR = 3,
  SILENT = 4,
}

Messages below the current level are dropped. The default is LogLevel.INFO.

Silencing or reducing output

import { logger, LogLevel } from '@quantajs/core';

// Only errors, for example in a test suite that expects warnings.
logger.setLevel(LogLevel.ERROR);

// Nothing at all.
logger.setLevel(LogLevel.SILENT);

logger.getLevel() returns the current level.

Formatting

logger.configure({
  level: LogLevel.WARN,
  prefix: 'quanta',   // prepended to every message
  timestamp: false,   // on by default
  colors: false,      // ANSI colours in Node.js; ignored in browsers
});

configure merges into the current configuration, so pass only what you want to change.

Capturing messages

The logger writes through console, so tools that capture console output, such as error trackers and test spies, see its messages:

import { vi } from 'vitest';

const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
// ...code that should warn...
expect(warn).toHaveBeenCalledWith(expect.stringContaining('persists its entire state'));

In 3.0

Logger, createLogger and the environment helpers are no longer exported: the logger is QuantaJS's diagnostics channel, not a general-purpose logger. Use your own logging library for application messages. See Migrating to 3.0.

Spot something that needs improving?

Edit page