logging 1.0.0
Install from the command line:
Learn more about npm packages
$ npm install @enzsft/logging@1.0.0
Install via package.json:
"@enzsft/logging": "1.0.0"
About this version
Isomorphic logging library, suitable for use in both Node.js and the browser.
Install @enzsft/logging
:
pnpm add -E @enzsft/logging
All logs are sent to stdout/stderr
in JSON format. Every log is attached to a service
, which is used to identify the source of the log. Each log also has a level
and timestamp
, traceId
and spanId
applied automatically.
import { createLogger } from "@enzsft/logging";
const logger = createLogger({
service: "test-service",
});
logger.debug({
message: "debug",
});
logger.info({
message: "info",
});
logger.warn({
message: "warn",
});
logger.error({
message: "error",
});
You can provide additional error information to logs.
import { createLogger } from "@enzsft/logging";
const logger = createLogger({
service: "test-service",
});
// Standard error
logger.error({
message: "error",
error: new Error("error"),
});
// Custom error with code
class ErrorWithCode extends Error {
code: string;
constructor(message: string, code: string) {
super(message);
this.code = code;
}
}
logger.error({
message: "error",
error: new ErrorWithCode("error", "ERROR_123"),
});
// Manually provide error information
logger.error({
message: "error",
error: {
message: "error-message",
code: "error-code",
name: "error-name",
stacktrace: "error-stacktrace",
},
});
You can configure the logger to automatically add data to every log.
import { createLogger } from "@enzsft/logging";
const logger = createLogger({
service: "test-service",
data: {
environment: "production",
},
});
You can provide a data
object to provide additional information with specific logs. This is merged with the data provided to the createLogger
.
import { createLogger } from "@enzsft/logging";
const logger = createLogger({
service: "test-service",
});
logger.info({
message: "info",
data: {
foo: "bar",
},
});
You can also use withData
to add data to every log in the future after the logger has been created.
import { createLogger } from "@enzsft/logging";
const logger = createLogger({
service: "test-service",
});
logger.withData({
environment: "production",
});
By default traceId
and spanId
are auto generated UUIDs. But you can manually provide your own too.
import { v4 } from "uuid";
import { createLogger } from "@enzsft/logging";
const logger = createLogger({
service: "test-service",
traceId: v4(),
spanId: v4(),
});
The logger can be used with OpenTelemetry to automatically add spanId
and traceId
to logs from the current OpenTelemetry span using @opentelemetry/api
. Your logging payloads will also be pushed to the current spans attributes too.
import { createLogger } from "@enzsft/logging";
import { getOpenTelemetryTracingContext } from "@enzsft/logging/opentelemetry";
const logger = createLogger({
service: "test-service",
});
logger.withTracingContext(getOpenTelemetryTracingContext());
You can redact data values from logs by providing a list of keys to redact. It will redact through nested objects and arrays too.
import { createLogger } from "@enzsft/logging";
const logger = createLogger({
service: "test-service",
});
logger.withRedactions(["password"]);