# Logs from Node.js

You need a log source token (`WT15_LOGS_TOKEN`, `wt15ls_…`): see [custom log sources](https://docs.watchtower15.com/logs/custom-sources/) and the [ingest API](https://developer.watchtower15.com/ingest/logs/) it wraps. Redaction is yours: the SDK ships the text it is given.

## Push lines

```ts
await wt15.ingest.logs.push([{ message: 'Payment failed', log_level: 'error', stream_name: 'billing' }]);
```

Lines go in chunks of at most 1,000 lines and 5,000,000 bytes, gzipped and keyed as metrics are. A `message` and a `raw_line` are cut at 65,536 UTF-8 bytes on a character boundary; a string that is not well-formed text is made so (`U+FFFD`); a `Date` `logged_at` becomes RFC 3339. A line without a non-blank `message` is refused with an `InvalidPayloadError` naming its index. **Pushing a different batch under a key you already used is refused 422 `idempotency_key_reused`.**

## The Logger

```ts
import { Logger } from '@watchtower15/sdk';

const log = new Logger({ client: wt15, stream: 'billing' });
log.info('User {user} paid {amount}', { user: 'ann', amount: 3 });
log.error('Payment failed', { order: 42, err: new Error('boom') });
await log.close();
```

The eight levels (`emergency` … `debug`) are methods; `log(level, message, context)` takes any. `{placeholder}` is filled from the context, the context follows the message as JSON, an `err` as its stack. `stream_name` is `stream`, else `name`, kept only when it matches `^[A-Za-z0-9._/@:+-]{1,255}$`.

## pino

```ts
import pino from 'pino';

const logger = pino({ name: 'billing' }, pino.transport({
  targets: [
    { target: 'pino/file', options: { destination: 1 } },
    { target: '@watchtower15/pino', options: { stream: 'billing' } }, // WT15_LOGS_TOKEN from the environment
  ],
}));
```

Options: `logsToken`, `ingestUrl`, `stream`, `batchSize`, `selfLog` (reports to standard error). Levels: `fatal` → `critical`, `error` → `error`, `warn` → `warning`, `info` → `info`, `debug` and `trace` → `debug`. The message is `msg`, then the other fields but `level`, `time`, `msg`, `pid` and `hostname` as JSON, an `err` as its stack. `stream_name` is `stream`, else the record’s `name`. The transport runs in pino’s worker thread and flushes its batch when it closes.

## winston

```ts
import winston from 'winston';
import { WatchTower15Transport } from '@watchtower15/winston';

const logger = winston.createLogger({ transports: [new WatchTower15Transport({ client: wt15, stream: 'billing' })] });
```

npm levels: `error`, `warn` → `warning`, `info`, `http` → `info`, `verbose`, `debug` and `silly` → `debug`; syslog’s eight keep their meaning. `stream_name` is `stream`, else the record’s `label`. `logger.close()` flushes what is queued.

## Batches, bounds and the breaker

Every logger and transport shares one `LogShipper`: a queue of 10,000 lines (past it `overflow` is counted), batches of `batchSize` (100) or every second, each batch one push with a 3-second timeout and one retry. When the API refuses, the shipper stops instead of hammering it: the volume stop (until `Retry-After` or `resumes_at`, at most an hour), a 60-second hold after a connection error, a 5xx or a 403, a throttle hold of `Retry-After`, and a 10-second probe when one ends; the stop is per process and per token. What was not sent is counted by reason (`config`, `missing_token`, `invalid_payload`, `volume_limit`, `rate_limited`, `held`, `connection`, `rejected_<status>`, `internal`, `overflow`) and written at most once a flush as `WatchTower15 logs: lines not sent: <reason> <lines>` to the `selfLogger` you give (none by default); a standing reason is repeated at most hourly. `flush()` never rejects and resolves to `{ linesSent, notSent, reports }`.

Source: https://developer.watchtower15.com/sdks/node/logs/
