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>
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>
<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>
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>
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>
Learn More:
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!