JavaScript and TypeScript logger for MapColonies based on pino.
Check the autogenerated documentation here.
jsLogger is an async factory and returns a Promise<Logger>.
import { jsLogger } from '@map-colonies/js-logger';
const logger = await jsLogger();
logger.info('hello world');
logger.error({ hello: 'world' });
To let the logger serialize an error correctly, put it under the err key.
try {
// complex code
} catch (error) {
logger.error({ msg: 'oh noes', err: error });
}
For more detailed usage check the pino documentation.
LoggerOptions exposes a focused subset of pino options together with package-specific flags.
| name | type | default value | description |
|---|---|---|---|
| enabled | boolean | pino default | Determines if logging is enabled. |
| level | string | pino default (info) |
Specifies the logging level. |
| redact | array | object | undefined | Defines paths to redact from log output. |
| hooks | object | undefined | Hooks for customizing log behavior. |
| base | object | null | pino default | Base properties to include in log output. |
| mixin | function | undefined | Function to add custom properties to log output. |
| prettyPrint | boolean | false | Uses pino-pretty instead of JSON output. |
| pinoCaller | boolean | false | Includes the caller's file and line number in log output. |
| opentelemetryOptions.enabled | boolean | false | Emits logs through the OpenTelemetry transport. |
| opentelemetryOptions.url | string | http://localhost:4317 |
OpenTelemetry gRPC collector URL, for example an Alloy OTLP endpoint. |
| opentelemetryOptions.resourceAttributes | object | undefined | Extra resource attributes merged into the emitted OpenTelemetry resource. |
The second argument controls the output destination - either a file path or a file descriptor number. It defaults to standard output (1).
Once created, the logger emits a logger initialized message at debug level summarizing the settings it started with: level, prettyPrint, pinoCaller, and whether OpenTelemetry is enabled along with its URL.
When opentelemetryOptions.enabled is true, js-logger adds pino-opentelemetry-transport alongside the regular destination, so logs are still written locally as well as exported. Resource attributes are populated from:
process.env.K8S_POD_UIDresourceAttributes you pass, which override the detected valuesimport { jsLogger } from '@map-colonies/js-logger';
const logger = await jsLogger({
level: 'info',
opentelemetryOptions: {
enabled: true,
url: 'http://otel-collector:4317',
resourceAttributes: { 'deployment.environment': 'production' },
},
});
Note that with OpenTelemetry enabled the level field is emitted as its numeric pino value (30) rather than the label ("info"), since both the OpenTelemetry transport and the routing between the two targets rely on numeric levels.