[**Documentation**](../../../../README)

***

[Documentation](../../../../README) / [@zerotal/core](../../README) / [logger](../README) / LogManager

# Class: LogManager

Defined in: [logger/LogManager.ts:84](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/logger/LogManager.ts#L84)

The engine behind Zerotal's logging: builds a [LogChannel](../interfaces/LogChannel) for every
entry in [LoggingConfigShape.channels](../interfaces/LoggingConfigShape#channels), enriches each
[LogEntry](../interfaces/LogEntry) (timestamp, hostname, pid, app/env, request id), filters by
each channel's minimum [LogLevel](../type-aliases/LogLevel), and dispatches the write.

Implements [BoundLogger](../interfaces/BoundLogger), so the five level methods log to the default
channel; use [channel](#channel) to target another and
[withContext](#withcontext) for contextual logging. Normally
resolved via the [Log](../variables/Log) facade rather than constructed directly.

Channel `write` failures are swallowed (reported to stderr) so logging can
never throw into application code.

## Example

```ts
const log = new LogManager({
  default: "console",
  channels: { console: { driver: "console", format: "pretty" } },
});
log.info("Server started", { port: 3000 });
log.channel("console").error("Boom", {}, new Error("kaboom"));
```

## Implements

- [`BoundLogger`](../interfaces/BoundLogger)

## Constructors

### Constructor

> **new LogManager**(`config`, `enrich?`, `override?`): `LogManager`

Defined in: [logger/LogManager.ts:103](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/logger/LogManager.ts#L103)

#### Parameters

##### config

[`LoggingConfigShape`](../interfaces/LoggingConfigShape)

Channel map and default-channel selection.

##### enrich?

`Partial`\<`_Enrich`\>

Overrides for the enrichment fields (`app`, `env`,
  `hostname`, `pid`); `hostname` and `pid` default to the current host/process.

##### override?

[`LogChannel`](../interfaces/LogChannel)

When supplied, replaces the default channel's
  implementation with this [LogChannel](../interfaces/LogChannel) and skips building the rest —
  primarily used in tests.

#### Returns

`LogManager`

#### Throws

If a `stack` channel references an unknown channel name, or
  a channel declares an unknown `driver`.

## Channels

### channel()

> **channel**(`name`): [`BoundLogger`](../interfaces/BoundLogger)

Defined in: [logger/LogManager.ts:248](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/logger/LogManager.ts#L248)

Return a [BoundLogger](../interfaces/BoundLogger) that writes to the named channel instead of the
default. Unknown channel names are silently ignored (nothing is emitted).

#### Parameters

##### name

`string`

Name of a channel from the config.

#### Returns

[`BoundLogger`](../interfaces/BoundLogger)

#### Example

```ts
log.channel("daily").info("Written to today's rotating file");
```

## Context

### withContext()

> **withContext**(`extra`): [`BoundLogger`](../interfaces/BoundLogger)

Defined in: [logger/LogManager.ts:267](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/logger/LogManager.ts#L267)

Return a [BoundLogger](../interfaces/BoundLogger) on the default channel that merges `extra` into
the context of every entry it emits. Per-call context keys override the
shared ones.

#### Parameters

##### extra

`Record`\<`string`, `unknown`\>

Context fields shared across all subsequent log calls.

#### Returns

[`BoundLogger`](../interfaces/BoundLogger)

#### Example

```ts
const scoped = log.withContext({ orderId: 99 });
scoped.info("Charged");            // context: { orderId: 99 }
scoped.warn("Retry", { attempt: 2 }); // context: { orderId: 99, attempt: 2 }
```

***

### scope()

> **scope**(`name`): [`BoundLogger`](../interfaces/BoundLogger)

Defined in: [logger/LogManager.ts:286](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/logger/LogManager.ts#L286)

Return a [BoundLogger](../interfaces/BoundLogger) that tags every entry with the subsystem it
came from. The console channel renders the tag as `[FLOW]`; file and JSON
channels keep it as a `scope` field, so a log file can be filtered by source.

#### Parameters

##### name

`string`

Subsystem name, e.g. `"flow"`, `"queue"`.

#### Returns

[`BoundLogger`](../interfaces/BoundLogger)

#### Example

```ts
const log = Log.scope("queue");
log.info("Worker started", { queues: ["default"] });
// 01:08:35.224 INFO  [QUEUE]  Worker started {"queues":["default"]}
```

## Logging

### debug()

> **debug**(`msg`, `ctx?`, `err?`): `void`

Defined in: [logger/LogManager.ts:190](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/logger/LogManager.ts#L190)

Log a `debug`-level message to the default channel.

#### Parameters

##### msg

`string`

##### ctx?

`Record`\<`string`, `unknown`\>

##### err?

`unknown`

#### Returns

`void`

#### Implementation of

[`BoundLogger`](../interfaces/BoundLogger).[`debug`](../interfaces/BoundLogger#debug)

***

### info()

> **info**(`msg`, `ctx?`, `err?`): `void`

Defined in: [logger/LogManager.ts:194](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/logger/LogManager.ts#L194)

Log an `info`-level message to the default channel.

#### Parameters

##### msg

`string`

##### ctx?

`Record`\<`string`, `unknown`\>

##### err?

`unknown`

#### Returns

`void`

#### Implementation of

[`BoundLogger`](../interfaces/BoundLogger).[`info`](../interfaces/BoundLogger#info)

***

### warn()

> **warn**(`msg`, `ctx?`, `err?`): `void`

Defined in: [logger/LogManager.ts:198](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/logger/LogManager.ts#L198)

Log a `warn`-level message to the default channel.

#### Parameters

##### msg

`string`

##### ctx?

`Record`\<`string`, `unknown`\>

##### err?

`unknown`

#### Returns

`void`

#### Implementation of

[`BoundLogger`](../interfaces/BoundLogger).[`warn`](../interfaces/BoundLogger#warn)

***

### error()

> **error**(`msg`, `ctx?`, `err?`): `void`

Defined in: [logger/LogManager.ts:202](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/logger/LogManager.ts#L202)

Log an `error`-level message to the default channel.

#### Parameters

##### msg

`string`

##### ctx?

`Record`\<`string`, `unknown`\>

##### err?

`unknown`

#### Returns

`void`

#### Implementation of

[`BoundLogger`](../interfaces/BoundLogger).[`error`](../interfaces/BoundLogger#error)

***

### fatal()

> **fatal**(`msg`, `ctx?`, `err?`): `void`

Defined in: [logger/LogManager.ts:206](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/logger/LogManager.ts#L206)

Log a `fatal`-level message to the default channel.

#### Parameters

##### msg

`string`

##### ctx?

`Record`\<`string`, `unknown`\>

##### err?

`unknown`

#### Returns

`void`

#### Implementation of

[`BoundLogger`](../interfaces/BoundLogger).[`fatal`](../interfaces/BoundLogger#fatal)

***

### table()

> **table**(`msg`, `rows`, `level?`): `void`

Defined in: [logger/LogManager.ts:232](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/logger/LogManager.ts#L232)

Log `msg` with `rows` attached, rendered as a box-drawn table by the console
channel. Past three or four keys an inline JSON blob stops being readable;
the same data in columns can be scanned.

The rows are ordinary context, so file and JSON channels record what any
other level method would have recorded — only the presentation differs.

#### Parameters

##### msg

`string`

##### rows

[`TableData`](../type-aliases/TableData)

One object (rendered as key/value rows) or a list of objects
  (rendered as a column per key, with a header).

##### level?

[`LogLevel`](../type-aliases/LogLevel) = `"info"`

Severity to emit at. Defaults to `info`.

#### Returns

`void`

#### Example

```ts
Log.table("Compile summary", { compiled: 4, cached: 2, runtime: 8 });
Log.table("Slow routes", [
  { route: "/posts", ms: 812 },
  { route: "/search", ms: 1204 },
], "warn");
```

#### Implementation of

[`BoundLogger`](../interfaces/BoundLogger).[`table`](../interfaces/BoundLogger#table)

## Other

### tap()

> `static` **tap**(`fn`): () => `void`

Defined in: [logger/LogManager.ts:400](https://github.com/zerotaldev/zerotal/blob/1d6fb26b05b18ba077de1bfaeb46c42131cd8b70/packages/core/src/logger/LogManager.ts#L400)

Register a sink invoked for every log entry, after enrichment and before the
channel write. Used by `@zerotal/monitor` to surface logs in the panel.
Returns an unsubscribe function.

#### Parameters

##### fn

(`entry`) => `void`

#### Returns

() => `void`
