Home > Web Front-end > JS Tutorial > LogLayer: A Modern Logging Library for TypeScript / JavaScript

LogLayer: A Modern Logging Library for TypeScript / JavaScript

Susan Sarandon
Release: 2025-01-16 13:08:57
Original
203 people have browsed it

LogLayer: A Modern Logging Library for TypeScript / JavaScript

Tired of juggling multiple logging libraries across projects? Frustrated with inconsistent error and metadata logging? LogLayer, an open-source solution, streamlines your logging process and enhances developer experience.

What is LogLayer?

LogLayer acts as a unified logging layer, compatible with popular logging libraries (Pino, Winston, Bunyan) and cloud providers (AWS, Google Cloud, DataDog). It offers a consistent, intuitive API for logging messages, metadata, and errors, directing your logs to your chosen destination.

Here's a quick example:

<code class="language-typescript">import { LogLayer } from 'loglayer'
import { PinoTransport } from '@loglayer/transport-pino'
import pino from 'pino'

// Configure LogLayer with Pino
const log = new LogLayer({
  transport: new PinoTransport({ logger: pino() })
})

// Log with metadata
log.withMetadata({ userId: '123', action: 'login' })
   .info('User logged in successfully')

// Contextual logging (persists across calls)
log.withContext({ requestId: '123' })
log.info('Processing request') // requestId included

try {
  // ... potential error-throwing code
} catch (error) {
  log.withError(error)
     .withMetadata({ userId: '123' })
     .error('User request processing failed')
}</code>
Copy after login

Key Advantages of LogLayer:

  • Seamless Library Switching: Easily switch logging libraries without code refactoring. Imagine needing to swap from Pino to Winston mid-project – LogLayer makes this painless.

  • Consistent API: A unified API eliminates the need to remember different method names and parameter orders across libraries. Compare:

<code>// LogLayer's consistent approach:
log.withMetadata({ some: 'data' }).info('my message')

// Inconsistent APIs without LogLayer:
winston.info('my message', { some: 'data' })     // winston
bunyan.info({ some: 'data' }, 'my message')      // bunyan</code>
Copy after login
  • Multi-Transport Support: Send logs to multiple destinations (DataDog and your logger, for instance) simultaneously:
<code class="language-typescript">import { LogLayer } from 'loglayer'
import { datadogLogs } from '@datadog/browser-logs'
import { PinoTransport } from "@loglayer/transport-pino"
import { DatadogBrowserLogsTransport } from "@loglayer/transport-datadog-browser-logs"

// Datadog initialization (replace placeholders)
datadogLogs.init({
  clientToken: '<client_token>',
  site: '<datadog_site>',
  forwardErrorsToLogs: true,
  sampleRate: 100
})

const log = new LogLayer({
  transport: [
    new PinoTransport({ logger: pino() }),
    new DatadogBrowserLogsTransport({ logger: datadogLogs })
  ]
})

// Logs sent to both Pino and DataDog
log.info('User logged in successfully')
</datadog_site></client_token></code>
Copy after login
  • Simplified Error Handling: Standardized error handling across all libraries.

  • Extensible Plugin System: Add plugins for features like redacting sensitive data.

  • Effortless Testing: Built-in mocks simplify testing.

Getting Started:

Installation is simple:

<code class="language-bash">npm install loglayer</code>
Copy after login

Basic usage:

<code class="language-typescript">import { LogLayer, ConsoleTransport } from 'loglayer'

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

log.info('Hello world!')</code>
Copy after login

Learn More:

  • Official Documentation
  • GitHub Repository
  • NPM Package

The above is the detailed content of LogLayer: A Modern Logging Library for TypeScript / JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template