Graylog: Using the Winston GELF package to send Node.js logs to Graylog
This documentation is part of the Using with Node.js guide. View the full guide here: How to send logs from Node.js to Graylog.
👋 Welcome to Stackhero documentation
Stackhero provides a fully managed Graylog cloud solution designed for speed and simplicity. You can:
- Rely on an unlimited, dedicated SMTP email server included with your service.
- Apply updates effortlessly with a single click, with no manual intervention needed.
- Use a custom domain name secured by HTTPS (for example, https://logs.your-company.com), giving your team secure, direct access.
- Experience strong performance and security on a private, dedicated infrastructure with no shared resources or noisy neighbors.
Focus on your data, not your tooling: you can get started with Stackhero's Graylog cloud hosting solution in just a few minutes.
warning It is best to use the
graylog2package unless Winston is already part of your project. If that is the case, you can replace it withwinston-gelf.
To install the Winston GELF package, run:
npm install winston-gelf
If Winston is not already in your project, you can add it with:
npm install winston
Here is a basic setup:
const winston = require('winston');
const winstonGelf = require('winston-gelf');
const process = require('process');
const logger = winston.createLogger({
transports: [
new winston.transports.Console(),
new winstonGelf({
// Check all gelfPro options here: https://www.npmjs.com/package/gelf-pro
gelfPro: {
fields: {
env: process.env.NODE_ENV || 'development'
},
adapterName: 'udp',
adapterOptions: {
host: '<XXXXXX>.stackhero-network.com', // Replace with your Graylog domain
port: 12201,
}
}
})
]
});
// Example of an informational log
logger.info('This is a log information');
// Example of an error log
try {
throw Error('This is an example error');
}
catch(error) {
logger.warn({ message: 'Error triggered', error });
}
Do not forget to configure your Graylog input (details below).