@reasonable-stack/logger
v0.1.1
Published
A reasonably good logger.
Readme
@reasonable-stack/logger
A reasonably good logger.
Five methods, one per syslog severity from error down to debug,
written to stdout / stderr as either colored text for terminals or one JSON
object per line for log collectors. No transports, no child loggers, no
dependencies.
Installation
npm install @reasonable-stack/loggerQuick start
import { createLogger } from '@reasonable-stack/logger';
const log = createLogger({ level: 'debug' });
log.debug('connecting to the database');
log.info('server listening on :3000');
log.warning('config file not found, using defaults');
log.error(new Error('connection refused'));14:03:07.512 [DBG] connecting to the database
14:03:07.512 [INF] server listening on :3000
14:03:07.513 [WRN] config file not found, using defaults
14:03:07.513 [ERR] Error: connection refusedLevels
| method | name | severity | label | accepts | stream |
| :-------- | :-------- | -------: | :---- | :---------------- | :------- |
| error | ERROR | 3 | ERR | string \| Error | stderr |
| warning | WARNING | 4 | WRN | string \| Error | stdout |
| notice | NOTICE | 5 | NTC | string | stdout |
| info | INFO | 6 | INF | string | stdout |
| debug | DEBUG | 7 | DBG | string | stdout |
A logger writes entries at its level and every level more severe (lower
number) than it. Methods for less severe levels are no-ops.
const log = createLogger({ level: 'warning' });
log.warning('disk 90% full'); // written
log.info('request handled'); // ignoredOptions
| option | type | default | description |
| :------- | :---------- | :---------- | :--------------------------------- |
| level | Level | 'info' | least severe level written |
| json | boolean | false | one JSON object per line |
| format | Formatter | undefined | custom formatter, wins over json |
| silent | boolean | false | only write errors, ignores level |
Level is one of 'error', 'warning', 'notice', 'info' or 'debug',
always in lowercase.
Output formats
The default, pretty, is meant for a human reading a terminal: the time of
day in UTC (dimmed) followed by the level label (colored by severity) and the
message.
17:57:26.351 [INF] server startedWith json: true, each entry is a single line of JSON, with the timestamp as a
full ISO 8601 string in UTC:
const log = createLogger({ json: true });
log.error(new RangeError('page must be positive'));
// => {"level":"ERROR","message":"RangeError: page must be positive","timestamp":"2026-11-26T17:57:26.351Z"}Custom formatters
A Formatter is a function from a LogEntry to a string. The logger trims
whatever it returns and adds a single trailing newline.
| field | type | description |
| :---------- | :---------------- | :---------------------------------- |
| level | object | { code, name, abv, stream } |
| message | string \| Error | exactly what was passed to a method |
| timestamp | Date | when the method was called |
Both built-in formatters are exported as formatters.pretty and
formatters.json, so a custom one can build on them:
import { createLogger, formatters, type Formatter } from '@reasonable-stack/logger';
const withPid: Formatter = (entry) => `[${process.pid}] ${formatters.pretty(entry)}`;
const log = createLogger({ format: withPid });
log.info('worker ready'); // => [4242] 17:57:26.351 [INF] worker readyThings worth knowing
silentstill writes errors. It raises the threshold to'error', it doesn't turn the logger off.- Errors lose their stack. Both built-in formatters render an
Erroras`${name}: ${message}`; the stack trace andcauseare dropped. Write a custom formatter if you need them. - Colors are always on. The
prettyformatter emits ANSI escape codes even when the output isn't a terminal, and it doesn't honorNO_COLOR. Preferjson: truewhen logs go to a file or a collector. - Pretty timestamps are UTC time only, with no date and no offset.
- Node.js or Bun only. Entries are written with
process.stdoutandprocess.stderr, so it won't run in a browser.
