Logflare Transport Server Deno Bun
Ships logs to Logflare using the HTTP transport with Logflare-specific configuration. Features include:
- Automatic Logflare JSON format
- Built on top of the robust HTTP transport
- Retry logic with exponential backoff
- Rate limiting support
- Batch sending with configurable size and timeout
- Error and debug callbacks
- Support for self-hosted Logflare instances
Installation
bash
npm install loglayer @loglayer/transport-logflare serialize-error
bash
pnpm add loglayer @loglayer/transport-logflare serialize-error
bash
yarn add loglayer @loglayer/transport-logflare serialize-error
Basic Usage
Logflare free tier issues
The free tier has very low rate limits which can be easily exceeded. You may find yourself getting 503: Service Unavailable
codes if you exceed the rate limit. Make sure to define the onError()
callback to catch this and adjust batch timings accordingly.
typescript
import { LogLayer } from 'loglayer'
import { LogflareTransport } from "@loglayer/transport-logflare"
import { serializeError } from "serialize-error";
const log = new LogLayer({
errorSerializer: serializeError,
contextFieldName: null, // recommended based on testing
metadataFieldName: null, // recommended based on testing
transport: new LogflareTransport({
sourceId: "YOUR-SOURCE-ID",
apiKey: "YOUR-API-KEY",
onError: (err) => {
console.error('Failed to send logs to Logflare:', err);
},
onDebug: (entry) => {
console.log('Log entry being sent to Logflare:', entry);
},
onDebugReqRes: ({ req, res }) => {
console.log("=== HTTP Request ===");
console.log("URL:", req.url);
console.log("Method:", req.method);
console.log("Headers:", JSON.stringify(req.headers, null, 2));
console.log("Body:", typeof req.body === "string" ? req.body : `[Uint8Array: ${req.body.length} bytes]`);
console.log("=== HTTP Response ===");
console.log("Status:", res.status, res.statusText);
console.log("Headers:", JSON.stringify(res.headers, null, 2));
console.log("Body:", res.body);
console.log("===================");
},
})
})
// Use the logger
log.info("This is a test message");
log.withMetadata({ userId: "123" }).error("User not found");
Configuration
Required Parameters
Name | Type | Description |
---|---|---|
sourceId | string | Your Logflare source ID |
apiKey | string | Your Logflare API key |
Optional Parameters
Name | Type | Default | Description |
---|---|---|---|
url | string | "https://api.logflare.app" | Custom Logflare API endpoint (for self-hosted instances) |
enabled | boolean | true | Whether the transport is enabled |
level | "trace" | "debug" | "info" | "warn" | "error" | "fatal" | "trace" | Minimum log level to process. Logs below this level will be filtered out |
HTTP Transport Optional Parameters
General Parameters
Name | Type | Default | Description |
---|---|---|---|
enabled | boolean | true | Whether the transport is enabled |
level | "trace" | "debug" | "info" | "warn" | "error" | "fatal" | "trace" | Minimum log level to process. Logs below this level will be filtered out |
method | string | "POST" | HTTP method to use for requests |
headers | Record<string, string> | (() => Record<string, string>) | {} | Headers to include in the request. Can be an object or a function that returns headers |
contentType | string | "application/json" | Content type for single log requests. User-specified headers take precedence |
compression | boolean | false | Whether to use gzip compression |
maxRetries | number | 3 | Number of retry attempts before giving up |
retryDelay | number | 1000 | Base delay between retries in milliseconds |
respectRateLimit | boolean | true | Whether to respect rate limiting by waiting when a 429 response is received |
maxLogSize | number | 1048576 | Maximum size of a single log entry in bytes (1MB) |
maxPayloadSize | number | 5242880 | Maximum size of the payload (uncompressed) in bytes (5MB) |
enableNextJsEdgeCompat | boolean | false | Whether to enable Next.js Edge compatibility |
Debug Parameters
Name | Type | Default | Description |
---|---|---|---|
onError | (err: Error) => void | - | Error handling callback |
onDebug | (entry: Record<string, any>) => void | - | Debug callback for inspecting log entries before they are sent |
onDebugReqRes | (reqRes: { req: { url: string; method: string; headers: Record<string, string>; body: string | Uint8Array }; res: { status: number; statusText: string; headers: Record<string, string>; body: string } }) => void | - | Debug callback for inspecting HTTP requests and responses. Provides complete request/response details including headers and body content |
Batch Parameters
Name | Type | Default | Description |
---|---|---|---|
batchContentType | string | "application/json" | Content type for batch log requests. User-specified headers take precedence |
enableBatchSend | boolean | true | Whether to enable batch sending |
batchSize | number | 100 | Number of log entries to batch before sending |
batchSendTimeout | number | 5000 | Timeout in milliseconds for sending batches regardless of size |
batchSendDelimiter | string | "\n" | Delimiter to use between log entries in batch mode |
batchMode | "delimiter" | "field" | "array" | "delimiter" | Batch mode for sending multiple log entries. "delimiter" joins entries with a delimiter, "field" wraps an array of entries in an object with a field name, "array" sends entries as a plain JSON array of objects |
batchFieldName | string | - | Field name to wrap batch entries in when batchMode is "field" |
For more details on these options, see the HTTP transport documentation.