Logs Data Platform - Getting started with Node.js
Objective
This guide allows you to send logs from a Node.js application to Logs Data Platform (LDP). We will use the Pino logger, a fast and low-overhead logger for Node.js, combined with a GELF (Graylog Extended Log Format) transport. It's also possible to use Winston logger with a GELF transport, instructions are provided at the end of this guide.
Requirements
- A Logs Data Platform account.
- A Stream created and its Token (X-OVH-TOKEN).
- The address of your LDP cluster (e.g.,
gra1.logs.ovh.com) and the GELF port (usually12202for TLS). - Node.js installed on your environment (version >= 20 recommended).
Instructions for logging with Pino
Install Dependencies
We need two packages:
pino: Encodes logs to JSON.@alex-michaud/pino-graylog-transport: Sends the logs to the LDP endpoint using GELF.
Install them via npm:
Configure the Logger
Create a file named logger.js (or index.js) and configure the logger.
You need to:
- Configure the transport to point to your LDP cluster.
- Add your
X-OVH-TOKENto every log message so LDP accepts and routes them. In GELF, custom fields generally start with an underscore_.
-
Note: For security reasons, we recommend using environment variables to store your Token and Cluster address instead of hardcoding them.
-
Note: In your
package.json, make sure to set"type": "module"to use ES modules syntax.
Run and Verify
Run your application:
Then, go to your Graylog interface (access via the OVHcloud Control Panel]) and define a relative search time (e.g., "Last 5 minutes"). You should see your messages appearing in the stream.
Go further
- Pino Documentation
- @alex-michaud/pino-graylog-transport Documentation
- Graylog Extended Log Format (GELF)
Instructions for logging with Winston
Install Dependencies
We need two packages:
winston: A versatile logging library for Node.js.winston-log2gelf: A transport for Winston to send logs in GELF format.
Install them via npm:
Configure the Logger
Create a file named logger.js (or index.js) and configure the logger.
You need to:
- Configure the transport to point to your LDP cluster.
- Add your
X-OVH-TOKENto every log message so LDP accepts and routes them. In GELF, custom fields generally start with an underscore_. - Set the log level as needed.
Note: For security reasons, we recommend using environment variables to store your Token and Cluster address instead of hardcoding them.
Run and Verify
Run your application:
Then, go to your Graylog interface (access via the OVHcloud Control Panel) and define a relative search time (e.g., "Last 5 minutes"). You should see your messages appearing in the stream.
Go further
- Getting Started: Quick Start
- Documentation: Guides
- Create an account: Try it!
Join our community of users.