APM
Course: Monitoring, Logging and Newrelic - chapter 8
Newrelic also lets you see all the logs and stats from your process, not just the host machine
Default logs
Section titled “Default logs”By default, you will see it is tailing global nginx logs, but not the application logs
/var/log/nginx/access.log
Setting up APM for a Node.js process
Section titled “Setting up APM for a Node.js process”
- Create a new data source with Node.js
- Use one of 5 ways to set it up (we’ll go with on the host)
- Install dependencies
npm install newrelic express- Update
package.json
"scripts": { "start": "NEW_RELIC_APP_NAME=test NEW_RELIC_LICENSE_KEY=license_key node -r newrelic index.js" },- Create a simple express app
require("newrelic");
const express = require("express");
const app = express();
app.get("/", (req, res) => { console.log("route hit"); res.json({message: "hi there"})})
app.listen(3000, () => { console.log("listening on port 3000");});- Open another terminal and loadtest the app
npm i -g loadtestloadtest -c 10 --rps 200 http://localhost:3000/-
Check the APM dashboard

-
You will notice logs are still empty

Adding logs
Section titled “Adding logs”Ref - https://docs.newrelic.com/docs/logs/logs-context/configure-logs-context-nodejs/
- Try enabling the
NEW_RELIC_APPLICATION_LOGGING_FORWARDING_ENABLEDflag
"scripts": { "start": "NEW_RELIC_APPLICATION_LOGGING_FORWARDING_ENABLED=true NEW_RELIC_APP_NAME=test NEW_RELIC_LICENSE_KEY=your_key node -r newrelic index.js" },- Add winston as the logger
npm install winston- Update the code
require("newrelic");const winston = require('winston');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 express = require("express");const app = express();
app.get("/", (req, res) => { logger.info("route hit"); if (Math.random() < 0.5) { logger.error("there was an err"); } res.json({message: "hi there"})});
app.listen(3000, () => { console.log("listening on port 3000");});