Seq Logging for JavaScript
seq-logging is a JavaScript library designed to send structured log events to the Seq HTTP ingestion API. It is currently at version 3.0.0 and serves as a low-level client, primarily intended for use by other logging frameworks and their respective Seq transports (e.g., winston-seq, pino-seq, bunyan-seq, @tsed/logger-seq), rather than for direct application interaction. The library handles asynchronous batching of log events based on payload size and provides mechanisms to ensure all buffered events are sent upon application exit or explicit flushing. Its key differentiator is acting as the foundational communication layer for Seq within the JavaScript ecosystem, offering both Node.js and browser-specific builds, and explicitly supports ESM.
Common errors
-
TypeError: require is not a function
cause Attempting to use CommonJS `require()` to import `seq-logging` in a project configured for ESM, or in `seq-logging` v3+ which is ESM-only.fixChange `const { Logger } = require('seq-logging');` to `import { Logger } from 'seq-logging';` and ensure your project is set up for ES Modules. -
ReferenceError: process is not defined
cause Attempting to use `process` global in a browser environment without proper polyfill or a Node.js specific build setup.fixWhen building for browsers, avoid direct use of Node.js globals like `process`. If environment variables are needed, pass them in at build time or use a browser-compatible method. Alternatively, ensure your bundler provides a `process` polyfill.
Warnings
- breaking Version 3.0.0 and later are ESM-only, meaning CommonJS `require()` syntax is no longer supported for importing the library. Attempting to use `require()` will result in import errors.
- gotcha Events are buffered internally for asynchronous batching. If the application exits before `close()` or `flush()` is called, buffered events may be lost.
- gotcha When targeting browser environments, a different entry point must be used (`seq-logging/browser`) compared to Node.js environments (`seq-logging`).
- gotcha The package requires Node.js version 14.18 or higher. Running on older Node.js versions may lead to unexpected errors or compatibility issues.
Install
-
npm install seq-logging -
yarn add seq-logging -
pnpm add seq-logging
Imports
- Logger
const { Logger } = require('seq-logging')import { Logger } from 'seq-logging' - Logger
const { Logger } = require('seq-logging/browser')import { Logger } from 'seq-logging/browser' - LoggerOptions
import type { LoggerOptions } from 'seq-logging'
Quickstart
import process from 'process';
import { Logger } from 'seq-logging';
const logger = new Logger({
serverUrl: process.env.SEQ_SERVER_URL ?? 'http://localhost:5341',
apiKey: process.env.SEQ_API_KEY ?? undefined, // Optional
requestTimeout: 30000 // default to 30s
});
logger.emit({
timestamp: new Date(),
level: 'Information',
messageTemplate: 'Hello from {appName}! It is {currentTime}.',
properties: {
appName: 'MySeqApp',
currentTime: new Date().toISOString(),
user: process.env.USERNAME ?? 'anonymous'
}
});
// Ensure all buffered events are sent before exiting
logger.close().then(() => console.log('Logger closed successfully.'));