Understand Context Truncation
main"...": "1 item not stringified" entry in the JSON output.repository·main·Indexed 22 days ago
https://github.com/gajus/roarrA fast, structured JSON logger for Node.js and the browser. It supports printf-style formatting, contextual child loggers, and asynchronous context propagation via the adopt method in Node.js. It provides convenience methods for log levels (trace, debug, info, warn, error, fatal) and integrates with @roarr/cli for filtering and pretty-printing.
"...": "1 item not stringified" entry in the JSON output.Roarr does not have reserved context property names, but following these conventions ensures compatibility with the roarr pretty-print CLI tool and other integrations:
| Property | Use Case |
|---|---|
application | Name of the application (use package instead for distributed code) |
logLevel | Numeric value indicating the log level |
namespace | Namespace within a package (e.g., function name), similar to the debug package |
package | Name of the NPM package |
The roarr pretty-print CLI translates logLevel numeric values as follows:
logLevel | Name |
|---|---|
| 10 | TRACE |
| 20 | DEBUG |
| 30 | INFO |
| 40 | WARN |
| 50 | ERROR |
| 60 | FATAL |
In Node.js, Roarr logging is disabled by default. To enable it, you must set the ROARR_LOG environment variable to true when starting your program. When enabled, all logs are written to stdout.
ROARR_LOG=true node ./index.jsThe Roarr CLI is a separate package used to filter and pretty-print Roarr logs. It is useful for inspecting JSON log streams in a human-readable format.
Install it globally via npm:
npm install @roarr/cli -gRun roarr --help to see all available commands and options.
In a browser environment, you must manually implement the ROARR.write method to capture and process logs. The ROARR.write method accepts a single argument: a message string (which is a JSON-formatted log entry).
If you need to configure ROARR.write before the roarr package is loaded, you should initialize it on globalThis.ROARR.
import { ROARR } from "roarr";
// Basic implementation
ROARR.write = (message) => {
console.log(JSON.parse(message));
};To produce logs in either Node.js or the browser, import the Roarr class and use its API methods. The API is consistent across both environments.
import { Roarr as log } from "roarr";
log("foo");In Node.js, Roarr registers an error listener on the output stream to ignore EPIPE errors. If your test runner creates isolated module environments, you should call ROARR.teardown() during teardown to prevent listener leaks.
import { ROARR } from "roarr";
afterEach(() => {
ROARR.teardown?.();
});In Node.js, Roarr prints all or none based on the ROARR_LOG variable. To filter the output, pipe the stdout to the @roarr/cli program using the --filter flag.
In the browser, you filter logs by implementing custom logic inside your globalThis.ROARR.write implementation.
# Node.js filtering with @roarr/cli
ROARR_LOG=true node ./index.js | roarr --filter 'context.logLevel:>30'To avoid code duplication and ensure consistent context across your application, create a dedicated logger file (e.g., Logger.js) that exports a child instance of Roarr with predefined context properties.
/**
* @file Example contents of a Logger.js file.
*/
import { Roarr } from "roarr";
export const Logger = Roarr.child({
// .foo property is going to appear only in the logs that are created using
// the current instance of a Roarr logger.
foo: "bar",
});You can use nestjs-logger-roarr to integrate Roarr into NestJS applications.
Option 1: Shared Logger Instance
Use RoarrLoggerService.sharedInstance() to provide a single logger for the entire application during NestFactory creation.
Option 2: Module Injection
Use RoarrLoggerModule.forRoot() in your AppModule to enable multiple injected loggers with a minimum logLevel configuration.
// Option 1: Shared Instance
import { RoarrLoggerService } from 'nestjs-logger-roarr';
import { AppModule } from "app.module";
const logger = RoarrLoggerService.sharedInstance();
const app = await NestFactory.create(AppModule, { logger });
// Option 2: Module Syntax
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { RoarrLoggerModule } from 'nestjs-logger-roarr';
@Module({
imports: [
RoarrLoggerModule.forRoot({
logLevel: 'warn', // minimum log level displayed
}),
],
})
export class AppModule {}You can control Roarr's behavior using the following environment variables:
| Name | Type | Function | Default |
|---|---|---|---|
ROARR_LOG | Boolean | Enables/disables logging | false |
ROARR_STREAM | STDOUT or STDERR | The stream where logs are written | STDOUT |
Tip: If you set ROARR_STREAM=STDERR, you may need to use shell redirection to pipe the output correctly, for example: `3>&1 1>&2 2>&3 3&-".
Roarr uses structured data for its logs.
MessageContext
A MessageContext is a JsonObject (a valid JSON object) that can be extended with custom properties. It represents the metadata attached to a log entry.
Message
A Message<T> represents the complete log entry structure. It contains:
context: The MessageContext (type T).message: The actual log string.sequence: A unique sequence identifier.time: A timestamp (number).version: The version of the log format.