A structured logger with Slack integration and AWS Lambda support.
See CHANGELOG.md for a detailed list of changes.
- Structured logging with Bunyan
- Slack integration
- AWS Lambda context support
- TypeScript support
- Log level management
- Async context tracking
npm install @heyatlas/logger
import { Logger } from "@heyatlas/logger";
// Only name is required at root level
const logger = new Logger({
name: "my-app",
});
// Environment is determined by process.env.NODE_ENV
// Defaults to "local" if not set
logger.info("Hello world", { userId: "123" });
const logger = new Logger({
name: "my-app",
slackApiToken: "your-slack-token",
staging: {
streams: [{ type: "stdout", level: "info" }],
slack: {
defaultChannel: "#staging-logs",
level: "warn",
},
},
production: {
streams: [{ type: "stdout", level: "info" }],
slack: {
defaultChannel: "#production-logs",
level: "error",
},
},
});
logger.error("Something went wrong", {
slack: { channel: "#alerts" },
error: new Error("Details here"),
});
import { withLoggerLambda } from "@heyatlas/logger";
import { AsyncLocalStorage } from "async_hooks";
const executionContext = new AsyncLocalStorage();
export const handler = withLoggerLambda(
executionContext,
async (event, context) => {
const logger = getLogger(executionContext);
logger.info("Processing event", { event });
// ... handler logic
}
);
The logger uses NODE_ENV to determine which environment configuration to use:
import { Logger } from "@heyatlas/logger";
const logger = new Logger({
name: "my-service",
slackApiToken: process.env.SLACK_TOKEN, // Optional: enable Slack integration
// Environment-specific configurations
local: {
streams: [{ level: "debug", type: "stdout" }],
},
test: {
streams: [{ level: "fatal", type: "stdout" }],
},
staging: {
streams: [{ level: "info", type: "stdout" }],
slack: {
defaultChannel: "#staging-logs",
level: "warn",
},
},
production: {
streams: [{ level: "info", type: "stdout" }],
slack: {
defaultChannel: "#prod-alerts",
level: "error",
},
},
});
The logger uses two levels of configuration:
-
Root Configuration:
name
(required): Logger nameslackApiToken
(optional): Slack API token for integration
-
Environment Configuration:
streams
(required): Array of stream configurationsslack
(optional): Slack settings per environmentdefaultChannel
: Default Slack channellevel
: Minimum level for Slack notifications
The logger supports different configurations per environment. You can use default configurations or provide your own:
The logger comes with default configurations for common environments:
const defaultConfigs = {
test: {
streams: [{ level: "fatal", type: "stdout" }],
},
local: {
streams: [{ level: "info", type: "stdout" }],
},
staging: {
streams: [{ level: "info", type: "stdout" }],
slack: {
defaultChannel: "#staging-logs",
level: "warn",
},
},
production: {
streams: [{ level: "info", type: "stdout" }],
slack: {
defaultChannel: "#prod-alerts",
level: "warn",
},
},
};
You can provide your own environment configurations:
import { getLogger, EnvironmentConfigs } from "@heyatlas/logger";
import { AsyncLocalStorage } from "async_hooks";
const customEnvConfigs: EnvironmentConfigs = {
development: {
streams: [{ level: "debug", type: "stdout" }],
},
qa: {
streams: [
{ level: "info", type: "stdout" },
{ level: "error", type: "file", path: "/var/log/app.log" },
],
slack: {
defaultChannel: "#qa-alerts",
level: "error",
},
},
};
const executionContext = new AsyncLocalStorage();
const logger = getLogger(
executionContext,
{
name: "my-service",
slackApiToken: process.env.SLACK_TOKEN,
qa: {
streams: [{ level: "debug", type: "stdout" }],
},
},
customEnvConfigs
);
Each environment configuration can specify:
- Stream configurations (stdout, file)
- Slack integration settings
- Log levels per stream and Slack
The logger supports the following log levels in order of priority:
fatal
: System is unusableerror
: Error conditionswarn
: Warning conditionsinfo
: Informational messagesdebug
: Debug-level messagestrace
: Trace-level messages
MIT