Skip to content

Azure Monitor Transport ​

AzureMonitorTransport sends batches of entries to the Azure Monitor Logs Ingestion API (a Data Collection Endpoint plus a Data Collection Rule). It gets its own bearer token with the OAuth2 client-credentials flow, so you do not need an Azure SDK.

Quick start ​

Import it from logixia/azure and put an instance in transports.custom.

ts
import { createLogger } from 'logixia';
import { AzureMonitorTransport } from 'logixia/azure';

const logger = createLogger({
  appName: 'api',
  environment: 'production',
  transports: {
    custom: [
      new AzureMonitorTransport({
        endpoint: 'https://my-dce.ingest.monitor.azure.com',
        ruleId: process.env.AZURE_DCR_ID!,
        streamName: 'Custom-LogixiaLogs_CL',
        // tenantId, clientId and clientSecret are read from
        // AZURE_TENANT_ID, AZURE_CLIENT_ID and AZURE_CLIENT_SECRET when omitted
      }),
    ],
  },
});

await logger.error('Job failed', { job: 'nightly-sync' });
await logger.close();

Options ​

OptionTypeDefaultDescription
endpointstringrequiredData Collection Endpoint URL, such as https://<name>.ingest.monitor.azure.com. Must be https.
ruleIdstringrequiredData Collection Rule immutable ID.
streamNamestringrequiredStream name defined in the rule.
tenantIdstringAZURE_TENANT_IDAzure AD tenant (a GUID or a verified domain).
clientIdstringAZURE_CLIENT_IDApp registration (service principal) client ID.
clientSecretstringAZURE_CLIENT_SECRETClient secret.
batchSizenumber200Entries per request.
flushIntervalMsnumber5000Send whatever is buffered after this many ms.
requestTimeoutMsnumber10000Abort a request that has not finished after this many ms.
maxBufferSizenumber10000Most buffered entries; the oldest are dropped past this.
levelstringunsetMinimum level to forward.

If any of tenant, client ID or secret is missing, the transport logs a warning and keeps entries buffered. Managed identity is not supported.

The service principal must be allowed to publish to the Data Collection Rule.

What is sent ​

Each entry is posted to <endpoint>/dataCollectionRules/<ruleId>/streams/<streamName>?api-version=2023-01-01 as a record with these columns, so define a matching stream in your rule:

ColumnValue
TimeGeneratedEntry timestamp
SeverityLevelSee the table below
MessageMessage text
AppNameappName
ContextContext, or empty string
TraceIdTrace ID, or empty string
PropertiesThe payload serialized as a JSON string
logixia levelSeverityLevel
verbose, trace0
debug, info, log1
warn, warning2
error3
critical, fatal4
anything else1

TIP

The rule ID and stream name are URL-encoded and the tenant ID is validated before it goes into the token URL, so a bad config cannot redirect the request to another host or path.

Failure handling ​

A failed request puts the batch back at the front of the buffer and pauses threshold-triggered flushes for 1 s, doubling up to 30 s. HTTP 4xx responses other than 429 drop the batch with a warning. The buffer is capped by maxBufferSize, dropping the oldest first. Tokens are cached until a minute before they expire. close() flushes up to three times and reports anything undelivered.

Released under the MIT License.