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

6.6.0 • Public • Published

loglayer

NPM version NPM Downloads TypeScript

loglayer is a unified Typescript logger that routes logs to various logging libraries, cloud providers, files, and OpenTelemetry while providing a fluent API for specifying log messages, metadata and errors.

  • For full documentation, read the docs.
// Example using the Pino logging library with LogLayer
// You can also start out with a console logger and swap to another later!
import { LogLayer } from 'loglayer';
import { pino } from 'pino';
import { PinoTransport } from '@loglayer/transport-pino';
import { redactionPlugin } from '@loglayer/plugin-redaction';

const log = new LogLayer({
  // Multiple loggers can also be used at the same time. 
  transport: new PinoTransport({
    logger: pino()
  }),
  // Plugins modify log data before it's shipped to your logging library.
  plugins: [
    redactionPlugin({
      paths: ['password'],
      censor: '[REDACTED]',
    }),
  ],
  // Put context data in a specific field (default is flattened)
  contextFieldName: 'context',
  // Put metadata in a specific field (default is flattened)
  metadataFieldName: 'metadata',
})

// persisted data that is always included in logs
log.withContext({
  path: "/",
  reqId: "1234"
})

log.withPrefix("[my-app]")
  .withError(new Error('test'))
  // data that is included for this log entry only
  .withMetadata({ some: 'data', password: 'my-pass' })
  // Non-object data only (numbers, booleans, and strings only)
  // this can be omitted to just log an object / error
  // by using .errorOnly() / .metadataOnly() instead of withError() / withMetadata()
  .info('my message')
{
  "level": 30,
  "time": 1735857465669,
  "msg": "[my-app] my message",
  "context": {
    "path": "/",
    "reqId": "1234",
  },
  "metadata": {
    "password": "[REDACTED]",
    "some": "data",
  },
  "err":{
    "type": "Error",
    "message": "test",
    "stack": "Error: test\n ..."
  }
}

With the Pretty Terminal Transport:

Pretty Terminal Transport

Installation

Install the core package:

npm i loglayer

Quick Start

import { LogLayer, ConsoleTransport } from 'loglayer'

const log = new LogLayer({
  transport: new ConsoleTransport({
    logger: console,
  }),
})

log
  .withMetadata({ some: 'data'})
  .withError(new Error('test'))
  .info('my message')

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
6.6.011,333latest

Version History

VersionDownloads (Last 7 Days)Published
6.6.011,333
6.5.03
6.4.35,133
6.4.24,264
6.4.10
6.4.0792
6.3.35,683
6.3.294
6.3.11,295
6.3.028
6.2.037
6.1.3124
6.1.21
6.1.176
6.1.00
6.0.03
5.7.0455
5.6.0365
5.5.147
5.5.00
5.4.130
5.4.00
5.3.00
5.2.00
5.1.40
5.1.30
5.1.20
5.1.10
5.1.00
5.0.125
5.0.110
5.0.100
5.0.90
5.0.80
5.0.70
5.0.60
5.0.50
5.0.40
5.0.30
5.0.20
5.0.10
5.0.00
4.8.04,143
4.7.018
4.6.10
4.6.00
4.5.01
4.4.11
4.4.0198
4.3.30
4.3.20
4.3.14,014
4.3.00
4.2.10
4.2.00
4.1.10
4.1.00
4.0.00
3.1.00
3.0.10
3.0.00
2.1.03
2.0.33,298
2.0.20
2.0.10
2.0.01
1.6.00
1.5.01
1.4.21
1.4.10
1.3.40
1.3.30
1.3.20
1.3.10
1.2.10
1.1.11
1.0.20
1.0.10
1.0.00

Package Sidebar

Install

npm i loglayer

Homepage

loglayer.dev

Weekly Downloads

41,448

Version

6.6.0

License

MIT

Unpacked Size

127 kB

Total Files

7

Last publish

Collaborators

  • theo.gravity
  • gravity.bot