@loglayer/transport-new-relic
TypeScript icon, indicating that this package has built-in type declarations

1.0.10 • Public • Published

New Relic Transport for LogLayer

NPM Version NPM Downloads TypeScript

The New Relic transport for the LogLayer logging library.

Ships logs to New Relic using their Log API. Features include:

  • Automatic gzip compression (configurable)
  • Retry mechanism with exponential backoff
  • Rate limiting support with configurable behavior
  • Validation of New Relic's API constraints
  • Error handling callback
  • Configurable endpoints for different regions

Installation

npm install loglayer @loglayer/transport-new-relic serialize-error

Usage

import { LogLayer } from 'loglayer'
import { NewRelicTransport } from "@loglayer/transport-new-relic"
import { serializeError } from "serialize-error";

const log = new LogLayer({
  errorSerializer: serializeError,
  transport: new NewRelicTransport({
    apiKey: "YOUR_NEW_RELIC_API_KEY",
    endpoint: "https://log-api.newrelic.com/log/v1", // optional, this is the default
    useCompression: true, // optional, defaults to true
    maxRetries: 3, // optional, defaults to 3
    retryDelay: 1000, // optional, base delay in ms, defaults to 1000
    respectRateLimit: true, // optional, defaults to true
    onError: (err) => {
      console.error('Failed to send logs to New Relic:', err);
    },
    onDebug: (entry) => {
      console.log('Log entry being sent:', entry);
    },
  })
})

// Use the logger
log.info("This is a test message");
log.withMetadata({ userId: "123" }).error("User not found");

Configuration

interface NewRelicTransportConfig {
  /**
   * Whether the transport is enabled. Default is true.
   */
  enabled?: boolean;
  /**
   * The New Relic API key
   */
  apiKey: string;
  /**
   * The New Relic Log API endpoint
   * @default https://log-api.newrelic.com/log/v1
   */
  endpoint?: string;
  /**
   * Optional callback for error handling
   */
  onError?: (err: Error) => void;
  /**
   * Optional callback for debugging log entries
   * Called with the validated entry before it is sent
   */
  onDebug?: (entry: Record<string, any>) => void;
  /**
   * Whether to use gzip compression
   * @default true
   */
  useCompression?: boolean;
  /**
   * Number of retry attempts before giving up
   * @default 3
   */
  maxRetries?: number;
  /**
   * Base delay between retries in milliseconds. 
   * The actual delay will use exponential backoff with jitter.
   * @default 1000
   */
  retryDelay?: number;
  /**
   * Whether to respect rate limiting by waiting when a 429 response is received
   * @default true
   */
  respectRateLimit?: boolean;
}

Documentation

For more details, visit https://loglayer.dev/transports/new-relic

Dependencies (1)

Dev Dependencies (10)

Package Sidebar

Install

npm i @loglayer/transport-new-relic

Homepage

loglayer.dev

Weekly Downloads

5

Version

1.0.10

License

MIT

Unpacked Size

65.5 kB

Total Files

9

Last publish

Collaborators

  • theo.gravity