Warning

**Deprecation Notice**: The Coralogix Node.js SDK (`coralogix-logger`) will be deprecated in favor of the [OpenTelemetry SDK](https://opentelemetry.io/docs/languages/js/) and will no longer be supported after **June 30, 2026**. See the [end-of-life notice](https://coralogix.com/docs/user-guides/latest-updates/deprecations/nodejs-sdk/index.md) for migration details.

# Node.js

This guide shows how to send application logs from Node.js to Coralogix using the [OpenTelemetry Logs SDK for JavaScript](https://opentelemetry.io/docs/languages/js/instrumentation/#logs) with the OTLP/gRPC log exporter. This approach replaces direct use of the legacy `coralogix-logger` Node.js package for log shipment over OTLP.

You can also add traces and metrics with the wider [OpenTelemetry Node.js documentation](https://opentelemetry.io/docs/languages/js/); see the [Node.js OpenTelemetry instrumentation](https://coralogix.com/docs/opentelemetry/instrumentation-options/nodejs-opentelemetry-instrumentation/index.md) overview for related options.

## Package dependencies setup

Add the following packages (versions are examples; prefer the latest compatible releases from the JavaScript package registry):

- `@opentelemetry/api-logs`
- `@opentelemetry/sdk-logs`
- `@opentelemetry/exporter-logs-otlp-grpc`
- `@opentelemetry/resources`
- `@opentelemetry/semantic-conventions`

Install them with the Node package manager:

```bash
npm install @opentelemetry/api-logs @opentelemetry/sdk-logs \
  @opentelemetry/exporter-logs-otlp-grpc @opentelemetry/resources \
  @opentelemetry/semantic-conventions
```

Here is a minimal `package.json` you can use as a starting point:

```json
{
  "name": "nodejs-otel-sdk-app",
  "version": "1.0.0",
  "main": "index.js",
  "type": "commonjs",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "@opentelemetry/api-logs": "^0.206.0",
    "@opentelemetry/exporter-logs-otlp-grpc": "^0.206.0",
    "@opentelemetry/resources": "^2.1.0",
    "@opentelemetry/sdk-logs": "^0.206.0",
    "@opentelemetry/semantic-conventions": "^1.37.0"
  }
}
```

Select the https://ingress.\[[DOMAIN_VALUE]\] endpoint that corresponds to your Coralogix [domain](https://coralogix.com/docs/user-guides/account-management/account-settings/coralogix-domain/index.md) using the domain selector at the top of the page.

## Application implementation

Create a `LoggerProvider` with a `BatchLogRecordProcessor` and `OTLPLogExporter`, register it as the global logger provider, then emit log records with `logs.getLogger(...)`. Set `service.name`, `cx.application.name`, and `cx.subsystem.name` on the resource so Coralogix can organize your telemetry.

The following example sends structured JSON bodies as log record bodies to a local collector on OTLP gRPC port `4317` by default.

```javascript
"use strict";

const { logs } = require("@opentelemetry/api-logs");
const { LoggerProvider, BatchLogRecordProcessor } = require("@opentelemetry/sdk-logs");
const { OTLPLogExporter } = require("@opentelemetry/exporter-logs-otlp-grpc");
const { resourceFromAttributes } = require("@opentelemetry/resources");
const { ATTR_SERVICE_NAME } = require("@opentelemetry/semantic-conventions");

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

const serviceName = process.env.OTEL_SERVICE_NAME || "nodejs-otel-logs-sample";
const applicationName = process.env.CORALOGIX_APPLICATION || "nodejs-otel-app";
const subsystemName = process.env.CORALOGIX_SUBSYSTEM || "worker";

const endpoint =
  process.env.OTEL_EXPORTER_OTLP_LOGS_ENDPOINT ||
  process.env.OTEL_EXPORTER_OTLP_ENDPOINT ||
  "http://localhost:4317";

const exporter = new OTLPLogExporter({ url: endpoint });
const loggerProvider = new LoggerProvider({
  resource: resourceFromAttributes({
    [ATTR_SERVICE_NAME]: serviceName,
    "cx.application.name": applicationName,
    "cx.subsystem.name": subsystemName,
  }),
  processors: [
    new BatchLogRecordProcessor(exporter),
  ],
});

logs.setGlobalLoggerProvider(loggerProvider);

const logger = logs.getLogger("nodejs-otel-sdk", "1.0.0");

function emitLog(lineNo) {
  logger.emit({
    severityNumber: 9, // INFO
    severityText: "INFO",
    body: JSON.stringify({
      message: `hello from OpenTelemetry log line ${lineNo}`,
      sequence: lineNo,
      source: "nodejs-sdk-migration",
      timestamp: new Date().toISOString(),
    }),
    attributes: {
      application: applicationName,
      subsystem: subsystemName,
    },
  });
}

async function run() {
  console.log("Node.js OTEL logs sample started");
  console.log(`Exporter endpoint: ${endpoint}`);

  const totalLogs = 20;
  for (let i = 1; i <= totalLogs; i += 1) {
    emitLog(i);
  }
  console.log(`Emitted ${totalLogs} logs`);

  await sleep(1500);
  await loggerProvider.forceFlush();
  await loggerProvider.shutdown();
  console.log("Done: forceFlush + shutdown completed");
}

run().catch(async (err) => {
  console.error("Application failed:", err);
  try {
    await loggerProvider.shutdown();
  } catch {
    // no-op
  }
  process.exit(1);
});
```

**Notes**

- Call `forceFlush()` or `shutdown()` before exit so batched OTLP payloads are delivered.
- Adjust `severityNumber` and `severityText` to match the [OpenTelemetry severity model](https://opentelemetry.io/docs/specs/otel/logs/data-model/#field-severitynumber) for your use case.

### Logging output

With the OpenTelemetry SDK, you can send logs either to a local [OpenTelemetry Collector](https://coralogix.com/docs/opentelemetry/kubernetes-observability/kubernetes-observability-using-opentelemetry/index.md) or directly to Coralogix using an OTLP endpoint.

#### OpenTelemetry Collector

Point `OTEL_EXPORTER_OTLP_ENDPOINT` (or `OTEL_EXPORTER_OTLP_LOGS_ENDPOINT`) at your collector’s OTLP/gRPC address (often port `4317`).

#### Coralogix OpenTelemetry endpoint

Configure the exporter to send to your Coralogix OTLP endpoint and authenticate with your [Send-Your-Data API key](https://coralogix.com/docs/user-guides/account-management/api-keys/send-your-data-api-key/index.md).

```bash
OTEL_EXPORTER_OTLP_ENDPOINT=https://ingress.[[DOMAIN_VALUE]]:443
OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer send_your_data_key"
OTEL_RESOURCE_ATTRIBUTES="service.name=nodejs-otel-logs-sample,cx.application.name=AppName,cx.subsystem.name=SubName"
```

### Additional resources

|                          |                                                                                                                                                         |
| ------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| OpenTelemetry JavaScript | [OpenTelemetry JS docs](https://opentelemetry.io/docs/languages/js/)                                                                                    |
| Node.js instrumentation  | [Node.js OpenTelemetry instrumentation](https://coralogix.com/docs/opentelemetry/instrumentation-options/nodejs-opentelemetry-instrumentation/index.md) |
| Coralogix Endpoints      | [Coralogix Endpoints](https://coralogix.com/docs/integrations/coralogix-endpoints/index.md)                                                             |

### Support

For help, use in-app chat or email [support@coralogix.com](mailto:support@coralogix.com).
