Logging From Node.js

This feature requires AppSignal for Node.js version 3.0.1 or higher.

This documentation outlines how to configure logging with the AppSignal for Node.js integration.

Configure Logging

Your log data must not contain any personal data, such as names, email addresses, etc. It is your responsibility to ensure this data is filtered out before being sent to AppSignal, and when identifying a person is necessary that your application uses alternative forms of identification such as a user ID, hash, or pseudonym.

You do not need to create a log source to send logs from the AppSignal for Node.js integration. An "application" log source will be created automatically.

Stand-alone usage

To use the logger, you need to import AppSignal and set up the logger using the .logger() function.

The .logger() function accepts two arguments: group and optionally severityThreshold. If severityThreshold is not provided, it will default to info.

Node.js
const { Appsignal } = require("@appsignal/nodejs"); const logger = Appsignal.logger("app"); // with a severity threshold: const logger = Appsignal.logger("app", "info");

There are six severity levels available trace, debug, info, log, warn, and error, each of them with its own function with the same name. These functions accept two arguments message and attributes. Attributes is used for metadata and is optional.

Messages will only be logged if their severity level is above the severity threshold.

Node.js
logger.info("Log message line"); logger.debug("User logged in", { user_id: 123 });

Note that if the message's severity is below the severity threshold set up when initializing the logger, it will not be sent to AppSignal.

You can query and filter on message contents and attribute values from within the Log Management tool.

Usage with Winston

This feature requires AppSignal for Node.js version 3.0.3 or higher.

If you use Winston to log messages throughout your application, you can use our Winston transport to send those logs to AppSignal. After importing the transport from the AppSignal integration, add it to the list of transports in your Winston logger:

Node.js
const winston = require("winston"); const { WinstonTransport } = require("@appsignal/nodejs"); const logger = winston.createLogger({ transports: [new WinstonTransport({ group: "app" })], });

Child Logger

You can use a child logger to add additional tags to log messages. You define tags when creating the child logger constant or pass tags as metadata when writing a log message. Say we want greater context in our logs of the actions of a particular user within a specific session. In the below example, a child logger for the background group is used to add sessionID with the additional drinkID, and paymentID tags are provided as metadata when creating a log message, and if a group is given to the child, it'll override the one set up in the transport.

Node.js
const logger = winston.createLogger({...}); const sessionLogger = logger.child({ group: "background", sessionID: user.id, }); sessionLogger.debug("User logged in"); sessionLogger.info("User ordered coffee", { drinkID: 30, paymentID: 20082 }); sessionLogger.debug("User logged out");

Usage with Pino

This feature requires AppSignal for Node.js version 3.5.4 or higher.

If Pino is your preferred logging library, you can use the AppSignal Pino transport to send logs to AppSignal.

The group argument will default to "app" if not provided.

Node.js
import pino from "pino"; const logger = pino({ transport: { targets: [ { target: "@appsignal/nodejs/pino", options: { group: "my-group" } }, // { target: ... } ], }, });

Logfmt and JSON

This feature requires AppSignal for Node.js version 3.0.11 or higher.

The log format can be specified when creating a logger, passing it as an argument when initializing the logger, after the severity threshold argument. For Logfmt:

Node.js
const { Appsignal } = require("@appsignal/nodejs"); const logger = Appsignal.logger("app", "info", "logfmt"); logger.info("category=blog this is about the blog");

For JSON:

Node.js
const { Appsignal } = require("@appsignal/nodejs"); const logger = Appsignal.logger("app", "info", "json"); logger.info('{"category": "blog", "message": "this is about the blog"}');

In both of the examples above, a filterable category attribute with the value "blog" will be logged.

Filtering Logs

You can use the ignoreLogs configuration option to ignore log lines based on their message. See our Ignore Logs guide to learn more.

Need Help?

After configuring your Node.js application to send logs, logs should appear in AppSignal. If you are unsure about this step or AppSignal is not receiving any logs, you can always reach out for assistance. We'll help get you back on track!