Problem
After APM setup, the Logs UI may still be empty for your app. Enable application log forwarding and use a structured logger.
Enable forwarding
{
"scripts": {
"start": "NEW_RELIC_APPLICATION_LOGGING_FORWARDING_ENABLED=true NEW_RELIC_APP_NAME=test NEW_RELIC_LICENSE_KEY=YOUR_LICENSE_KEY node -r newrelic index.js"
}
}Winston logger
Winston can send logs to files, console, databases, and more via transports.
npm install winstonrequire("newrelic");
const winston = require("winston");
const express = require("express");
const logger = winston.createLogger({
level: "info",
format: winston.format.json(),
defaultMeta: { service: "user-service" },
transports: [
new winston.transports.File({ filename: "error.log", level: "error" }),
new winston.transports.File({ filename: "combined.log" }),
],
});
if (process.env.NODE_ENV !== "production") {
logger.add(
new winston.transports.Console({
format: winston.format.simple(),
}),
);
}
const app = express();
app.get("/", (req, res) => {
logger.info("route hit");
if (Math.random() < 0.5) {
logger.error("there was an error");
}
res.json({ message: "hi there" });
});
app.listen(3000, () => {
console.log("listening on port 3000");
});Formats example
format: winston.format.combine(
winston.format.timestamp(),
winston.format.prettyPrint(),
);Metrics on logs
You can chart / alert on log volume — especially errors:
SELECT count(`message`)
FROM Log
WHERE entity.guid = 'YOUR_APP_GUID'
AND message LIKE '%there was an error%'
TIMESERIES AUTOUse this to catch error spikes without waiting for users to report them.
Prefer structured JSON logs (winston.format.json()) so fields are queryable in New Relic.