Collecting Nodejs Logs Using Winston and OpenTelemetry

You can directly send logs of your application to SigNoz using Winston and OpenTelemetry Node.js libraries.

📝 Note

The default logging level can be configured.

To send all the logs to SigNoz please change the default log level to DEBUG.

const logger = winston.createLogger({
  level: 'debug',
  // ... other configurations
});

For SigNoz Cloud

For sending logs to SigNoz cloud, while running the above example set the below environment variables

  • The value of OTEL_EXPORTER_OTLP_ENDPOINT environment variable will be https://ingest.{region}.signoz.cloud:443 where depending on the choice of your region for SigNoz cloud, the otlp endpoint will vary according to this table.

    RegionEndpoint
    USingest.us.signoz.cloud:443
    INingest.in.signoz.cloud:443
    EUingest.eu.signoz.cloud:443
  • The value of SIGNOZ_ACCESS_TOKEN environment variable will be <SIGNOZ_INGESTION_KEY> where <SIGNOZ_INGESTION_KEY> is your ingestion key

How to collect logs from Node.js Winston without the requiring Otel collectors

  • First we will install a few dependencies using npm

    npm install @opentelemetry/api-logs @opentelemetry/sdk-logs @opentelemetry/exporter-logs-otlp-http @opentelemetry/winston-transport winston dotenv
    
  • logger.js

    const logsAPI = require('@opentelemetry/api-logs')
    const { LoggerProvider, SimpleLogRecordProcessor } = require('@opentelemetry/sdk-logs')
    const { OTLPLogExporter } = require('@opentelemetry/exporter-logs-otlp-http')
    const { OpenTelemetryTransportV3 } = require('@opentelemetry/winston-transport')
    const winston = require('winston')
    require('dotenv').config()
    
    // Initialize the Logger provider
    const loggerProvider = new LoggerProvider()
    
    // Configure OTLP exporter for SigNoz
    const otlpExporter = new OTLPLogExporter({
      url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT,
      headers: {
        'signoz-access-token': process.env.SIGNOZ_ACCESS_TOKEN,
      },
    })
    
    // Add processor with the OTLP exporter
    loggerProvider.addLogRecordProcessor(new SimpleLogRecordProcessor(otlpExporter))
    
    // Set the global logger provider
    logsAPI.logs.setGlobalLoggerProvider(loggerProvider)
    
    // Create Winston logger with Console and OpenTelemetry transports
    const logger = winston.createLogger({
      level: 'debug',
      format: winston.format.combine(
        winston.format.timestamp(),
        winston.format.errors({ stack: true }),
        winston.format.metadata(),
        winston.format.json()
      ),
      defaultMeta: {
        service: 'winston-logger',
        environment: process.env.NODE_ENV || 'development',
      },
      transports: [
        new winston.transports.Console({
          format: winston.format.combine(winston.format.colorize(), winston.format.simple()),
        }),
        new OpenTelemetryTransportV3({
          loggerProvider,
          logAttributes: {
            'service.name': 'winston-logger',
            'deployment.environment': process.env.NODE_ENV || 'development',
          },
        }),
      ],
    })
    
    module.exports = logger
    
    • index.js
    const logger = require('./logger')
    
    // Log different levels
    logger.info('Server started', { port: 3000 })
    logger.debug('Debug information')
    logger.error('Error occurred', { error: 'Something went wrong' })
    
  • We will modify the OTLPLogExporter to

    OTLPLogExporter(url="0.0.0.0:4317")
    

    Here we are using 0.0.0.0 as our host since we are running this application in the same machine where SigNoz is running, for other configurations please check the troubleshooting guide.

  • Now you can run your application by running node index.js

  • If there are no errors your logs will be visible on SigNoz UI.

Collecting Nodejs logs when application is deployed on Docker or Kubernetes

When your application is deployed in Docker or a Kubernetes cluster, the logs from the console are automatically collected and stored in the node. The SigNoz collector will automatically collect the logs and they will be visible on the SigNoz UI.

Console Transport Configuration

You can easily add a console transport to your Winston logger with the following code:

logger.add(new winston.transports.Console(options))

The Console transport supports several configuration options:

  • level: Specifies the level of messages this transport should log (default: level set on parent logger).
  • silent: A boolean flag to suppress output (default is false).
  • eol: Defines the end-of-line characters to use (default is os.EOL).
  • stderrLevels: An array of log levels to be sent to stderr instead of stdout. For example: ['error', 'debug', 'info'] (default is an empty array).
  • consoleWarnLevels: An array of log levels to use console.warn() or stderr (in Node.js) instead of stdout. For example: ['warn', 'debug'] (default is an empty array).

Example configuration:

logger.add(
  new winston.transports.Console({
    level: 'info',
    format: winston.format.simple(),
    silent: false,
    stderrLevels: ['error'],
  })
)

By using these configuration options, you can fine-tune how your logs are output and collected when deployed in containerized environments like Docker or Kubernetes.

Was this page helpful?