@cleverbrush/log 0.0.0-beta-20260424142030

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +80 -0
  2. package/dist/Enricher.d.ts +8 -0
  3. package/dist/Filter.d.ts +7 -0
  4. package/dist/LogContext.d.ts +58 -0
  5. package/dist/LogEvent.d.ts +24 -0
  6. package/dist/LogLevel.d.ts +44 -0
  7. package/dist/Logger.d.ts +111 -0
  8. package/dist/LoggerPipeline.d.ts +48 -0
  9. package/dist/MessageTemplate.d.ts +58 -0
  10. package/dist/SelfLog.d.ts +37 -0
  11. package/dist/Sink.d.ts +14 -0
  12. package/dist/chunk-EU6TDBKQ.js +3 -0
  13. package/dist/chunk-EU6TDBKQ.js.map +1 -0
  14. package/dist/clickhouse.d.ts +1 -0
  15. package/dist/clickhouse.js +16 -0
  16. package/dist/clickhouse.js.map +1 -0
  17. package/dist/correlation.d.ts +22 -0
  18. package/dist/createLogger.d.ts +51 -0
  19. package/dist/di.d.ts +29 -0
  20. package/dist/enrichers/application.d.ts +8 -0
  21. package/dist/enrichers/caller.d.ts +10 -0
  22. package/dist/enrichers/correlationId.d.ts +8 -0
  23. package/dist/enrichers/environment.d.ts +8 -0
  24. package/dist/enrichers/hostname.d.ts +8 -0
  25. package/dist/enrichers/index.d.ts +6 -0
  26. package/dist/enrichers/processId.d.ts +8 -0
  27. package/dist/formatters/ClefFormatter.d.ts +33 -0
  28. package/dist/index.d.ts +29 -0
  29. package/dist/index.js +9 -0
  30. package/dist/index.js.map +1 -0
  31. package/dist/middleware/correlationId.d.ts +28 -0
  32. package/dist/middleware/requestLogging.d.ts +41 -0
  33. package/dist/samplingFilter.d.ts +23 -0
  34. package/dist/serialization.d.ts +25 -0
  35. package/dist/sinks/BatchingSink.d.ts +45 -0
  36. package/dist/sinks/ClickHouseSink.d.ts +78 -0
  37. package/dist/sinks/ConsoleSink.d.ts +29 -0
  38. package/dist/sinks/FileSink.d.ts +43 -0
  39. package/dist/sinks/SeqSink.d.ts +37 -0
  40. package/dist/sinks/createSink.d.ts +35 -0
  41. package/dist/sinks/index.d.ts +5 -0
  42. package/dist/useLogging.d.ts +35 -0
  43. package/package.json +69 -0
@@ -0,0 +1,45 @@
1
+ import type { LogEvent } from '../LogEvent.js';
2
+ import type { LogSink } from '../Sink.js';
3
+ /**
4
+ * Configuration for the batching sink wrapper.
5
+ */
6
+ export interface BatchingSinkOptions {
7
+ /** Flush after this many events. @default 100 */
8
+ batchSize?: number;
9
+ /** Flush after this many milliseconds of inactivity. @default 2000 */
10
+ flushInterval?: number;
11
+ /** Maximum buffered events before dropping. @default 50000 */
12
+ maxQueueSize?: number;
13
+ /** Maximum retry attempts for failed flushes. @default 5 */
14
+ maxRetries?: number;
15
+ /** Initial retry delay in ms (exponential backoff). @default 1000 */
16
+ retryDelay?: number;
17
+ /** Consecutive failures before circuit breaker opens. @default 3 */
18
+ circuitBreakerThreshold?: number;
19
+ /** The emit function that writes a batch to the target. */
20
+ emit: (batch: LogEvent[]) => Promise<void>;
21
+ }
22
+ /**
23
+ * Production-grade batching wrapper for log sinks.
24
+ *
25
+ * Provides buffering, retry with exponential backoff, and circuit
26
+ * breaking. All network/file sinks use this internally.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * const sink = new BatchingSink({
31
+ * batchSize: 100,
32
+ * flushInterval: 2000,
33
+ * emit: async (batch) => {
34
+ * await fetch('/logs', { method: 'POST', body: JSON.stringify(batch) });
35
+ * },
36
+ * });
37
+ * ```
38
+ */
39
+ export declare class BatchingSink implements LogSink {
40
+ #private;
41
+ constructor(options: BatchingSinkOptions);
42
+ emit(events: LogEvent[]): Promise<void>;
43
+ flush(): Promise<void>;
44
+ [Symbol.asyncDispose](): Promise<void>;
45
+ }
@@ -0,0 +1,78 @@
1
+ import type { LogSink } from '../Sink.js';
2
+ /**
3
+ * Column mapping for the ClickHouse logs table.
4
+ */
5
+ export interface ClickHouseColumnMapping {
6
+ timestamp?: string;
7
+ level?: string;
8
+ messageTemplate?: string;
9
+ renderedMessage?: string;
10
+ properties?: string;
11
+ exception?: string;
12
+ correlationId?: string;
13
+ traceId?: string;
14
+ spanId?: string;
15
+ sourceContext?: string;
16
+ }
17
+ /**
18
+ * ClickHouse sink configuration.
19
+ */
20
+ export interface ClickHouseSinkOptions {
21
+ /** Knex-ClickHouse connection instance. */
22
+ connection: any;
23
+ /** Table name to insert into. */
24
+ table: string;
25
+ /** Events per batch. @default 1000 */
26
+ batchSize?: number;
27
+ /** Max milliseconds between flushes. @default 5000 */
28
+ flushInterval?: number;
29
+ /** Custom column mapping. */
30
+ columns?: ClickHouseColumnMapping;
31
+ }
32
+ /**
33
+ * Options for creating the ClickHouse logs table.
34
+ */
35
+ export interface CreateLogsTableOptions {
36
+ /** Table engine. @default 'MergeTree' */
37
+ engine?: string;
38
+ /** Partition expression. @default "toYYYYMM(timestamp)" */
39
+ partitionBy?: string;
40
+ /** ORDER BY columns. @default ['timestamp', 'level'] */
41
+ orderBy?: string[];
42
+ /** TTL expression for automatic data expiration. */
43
+ ttl?: string;
44
+ }
45
+ /**
46
+ * Creates the ClickHouse logs table with a recommended schema.
47
+ *
48
+ * @param connection - knex-clickhouse connection
49
+ * @param tableName - name for the logs table
50
+ * @param options - DDL options (engine, partitioning, TTL)
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * await createLogsTable(ch, 'application_logs', {
55
+ * ttl: 'timestamp + INTERVAL 90 DAY',
56
+ * });
57
+ * ```
58
+ */
59
+ export declare function createLogsTable(connection: any, tableName: string, options?: CreateLogsTableOptions): Promise<void>;
60
+ /**
61
+ * Creates a ClickHouse sink that batch inserts log events.
62
+ *
63
+ * Ships as a separate entrypoint (`@cleverbrush/log/clickhouse`) to
64
+ * avoid forcing `@cleverbrush/knex-clickhouse` as a dependency.
65
+ *
66
+ * @param options - ClickHouse connection and batching configuration
67
+ * @returns a `LogSink` that batch inserts into ClickHouse
68
+ *
69
+ * @example
70
+ * ```ts
71
+ * const sink = clickHouseSink({
72
+ * connection: ch,
73
+ * table: 'application_logs',
74
+ * batchSize: 1000,
75
+ * });
76
+ * ```
77
+ */
78
+ export declare function clickHouseSink(options: ClickHouseSinkOptions): LogSink;
@@ -0,0 +1,29 @@
1
+ import { type LogLevelName } from '../LogLevel.js';
2
+ import type { LogSink } from '../Sink.js';
3
+ /**
4
+ * Console sink configuration.
5
+ */
6
+ export interface ConsoleSinkOptions {
7
+ /** Output mode: `'pretty'` for colored human-readable, `'json'` for CLEF. @default 'pretty' */
8
+ mode?: 'pretty' | 'json';
9
+ /** Color theme for pretty mode. @default 'dark' */
10
+ theme?: 'dark' | 'light' | 'none';
11
+ /** Minimum level for this sink. @default undefined (uses pipeline level) */
12
+ minimumLevel?: LogLevelName;
13
+ }
14
+ /**
15
+ * Creates a console sink for log output.
16
+ *
17
+ * Supports two modes:
18
+ * - `'pretty'` — colored, human-readable output (default for development)
19
+ * - `'json'` — CLEF JSON output (for production / container logs)
20
+ *
21
+ * @param options - console sink configuration
22
+ * @returns a `LogSink` that writes to stdout/stderr
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * const sink = consoleSink({ theme: 'dark', minimumLevel: 'debug' });
27
+ * ```
28
+ */
29
+ export declare function consoleSink(options?: ConsoleSinkOptions): LogSink;
@@ -0,0 +1,43 @@
1
+ import { type LogLevelName } from '../LogLevel.js';
2
+ import type { LogSink } from '../Sink.js';
3
+ /**
4
+ * File rotation configuration.
5
+ */
6
+ export interface RotationOptions {
7
+ /** Rotation strategy. */
8
+ strategy: 'size' | 'time' | 'hybrid';
9
+ /** Time-based rotation interval (for `'time'` and `'hybrid'`). */
10
+ interval?: 'hourly' | 'daily';
11
+ /** Maximum file size in bytes before rotation (for `'size'` and `'hybrid'`). */
12
+ maxBytes?: number;
13
+ /** Number of rotated files to retain. @default 10 */
14
+ retainCount?: number;
15
+ }
16
+ /**
17
+ * File sink configuration.
18
+ */
19
+ export interface FileSinkOptions {
20
+ /** Path to the log file. */
21
+ path: string;
22
+ /** Minimum level for this sink. */
23
+ minimumLevel?: LogLevelName;
24
+ /** Rotation configuration. */
25
+ rotation?: RotationOptions;
26
+ }
27
+ /**
28
+ * Creates a file sink that writes CLEF-formatted log events.
29
+ *
30
+ * Supports size-based, time-based, and hybrid rotation strategies.
31
+ *
32
+ * @param options - file sink configuration
33
+ * @returns a `LogSink` that appends to a file
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * const sink = fileSink({
38
+ * path: './logs/app.log',
39
+ * rotation: { strategy: 'time', interval: 'daily', retainCount: 30 },
40
+ * });
41
+ * ```
42
+ */
43
+ export declare function fileSink(options: FileSinkOptions): LogSink;
@@ -0,0 +1,37 @@
1
+ import type { LogSink } from '../Sink.js';
2
+ /**
3
+ * Seq sink configuration.
4
+ */
5
+ export interface SeqSinkOptions {
6
+ /** Base URL of the Seq server (e.g. `http://localhost:5341`). */
7
+ serverUrl: string;
8
+ /** Optional API key sent as `X-Seq-ApiKey` header. */
9
+ apiKey?: string;
10
+ /** Events per batch. @default 100 */
11
+ batchSize?: number;
12
+ /** Max milliseconds between flushes. @default 2000 */
13
+ flushInterval?: number;
14
+ /** Maximum retry attempts. @default 5 */
15
+ maxRetries?: number;
16
+ /** Initial retry delay in ms. @default 1000 */
17
+ retryDelay?: number;
18
+ }
19
+ /**
20
+ * Creates a sink that sends log events to a Seq server via HTTP in CLEF format.
21
+ *
22
+ * Events are batched and sent to `POST {serverUrl}/ingest/clef`. The sink
23
+ * respects Seq's `MinimumLevelAccepted` response to dynamically reduce
24
+ * bandwidth when the server applies level filtering.
25
+ *
26
+ * @param options - Seq connection and batching configuration
27
+ * @returns a `LogSink` that batches and sends events to Seq
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * const sink = seqSink({
32
+ * serverUrl: 'https://seq.mycompany.com',
33
+ * apiKey: process.env.SEQ_API_KEY,
34
+ * });
35
+ * ```
36
+ */
37
+ export declare function seqSink(options: SeqSinkOptions): LogSink;
@@ -0,0 +1,35 @@
1
+ import type { LogEvent } from '../LogEvent.js';
2
+ import { type LogLevelName } from '../LogLevel.js';
3
+ import type { LogSink } from '../Sink.js';
4
+ /**
5
+ * Configuration for a quick custom sink.
6
+ */
7
+ export interface CreateSinkOptions {
8
+ /** Minimum level for this sink. */
9
+ minimumLevel?: LogLevelName;
10
+ /** The emit function that writes events. */
11
+ emit: (events: LogEvent[]) => Promise<void>;
12
+ /** Optional flush function. */
13
+ flush?: () => Promise<void>;
14
+ /** Optional dispose function. */
15
+ dispose?: () => Promise<void>;
16
+ }
17
+ /**
18
+ * Creates a simple `LogSink` from an emit function.
19
+ *
20
+ * @param options - sink configuration with emit function
21
+ * @returns a `LogSink` instance
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * const sink = createSink({
26
+ * minimumLevel: 'error',
27
+ * emit: async (events) => {
28
+ * for (const event of events) {
29
+ * await sendAlert(event.renderedMessage);
30
+ * }
31
+ * },
32
+ * });
33
+ * ```
34
+ */
35
+ export declare function createSink(options: CreateSinkOptions): LogSink;
@@ -0,0 +1,5 @@
1
+ export { BatchingSink, type BatchingSinkOptions } from './BatchingSink.js';
2
+ export { type ConsoleSinkOptions, consoleSink } from './ConsoleSink.js';
3
+ export { type CreateSinkOptions, createSink } from './createSink.js';
4
+ export { type FileSinkOptions, fileSink, type RotationOptions } from './FileSink.js';
5
+ export { type SeqSinkOptions, seqSink } from './SeqSink.js';
@@ -0,0 +1,35 @@
1
+ import type { Logger } from './Logger.js';
2
+ import { correlationIdMiddleware } from './middleware/correlationId.js';
3
+ import { type RequestLoggingOptions, requestLoggingMiddleware } from './middleware/requestLogging.js';
4
+ export interface UseLoggingOptions extends RequestLoggingOptions {
5
+ /**
6
+ * Header name to echo the correlation ID back on the response.
7
+ * Set to `false` to suppress the response header entirely — useful
8
+ * when an OTel `X-Trace-Id` header already serves the traceability
9
+ * purpose and a second ID would confuse consumers.
10
+ *
11
+ * @default 'X-Correlation-Id'
12
+ */
13
+ correlationResponseHeader?: string | false;
14
+ }
15
+ /**
16
+ * Convenience function that returns correlation ID middleware and
17
+ * request logging middleware, ready to spread into `ServerBuilder.use()`.
18
+ *
19
+ * @param logger - the root logger instance
20
+ * @param options - request logging configuration
21
+ * @returns an array of middleware functions
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * const server = new ServerBuilder()
26
+ * .use(...useLogging(logger, {
27
+ * excludePaths: ['/health'],
28
+ * }))
29
+ * .build();
30
+ * ```
31
+ */
32
+ export declare function useLogging(logger: Logger, options?: UseLoggingOptions): [
33
+ ReturnType<typeof correlationIdMiddleware>,
34
+ ReturnType<typeof requestLoggingMiddleware>
35
+ ];
package/package.json ADDED
@@ -0,0 +1,69 @@
1
+ {
2
+ "author": "Andrew Zolotukhin <andrew_zol@cleverbrush.com>",
3
+ "bugs": {
4
+ "url": "https://github.com/cleverbrush/framework/issues",
5
+ "email": "andrew_zol@cleverbrush.com"
6
+ },
7
+ "description": "Enterprise structured logging — Serilog-style message templates, CLEF serialization, production sinks",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "homepage": "https://docs.cleverbrush.com/",
12
+ "keywords": [
13
+ "logging",
14
+ "structured logging",
15
+ "serilog",
16
+ "clef",
17
+ "cleverbrush"
18
+ ],
19
+ "license": "BSD 3-Clause",
20
+ "main": "./dist/index.js",
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/index.d.ts",
24
+ "import": "./dist/index.js"
25
+ },
26
+ "./clickhouse": {
27
+ "types": "./dist/clickhouse.d.ts",
28
+ "import": "./dist/clickhouse.js"
29
+ }
30
+ },
31
+ "sideEffects": false,
32
+ "name": "@cleverbrush/log",
33
+ "readme": "https://github.com/cleverbrush/framework/tree/master/libs/log#readme",
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "github:cleverbrush/framework"
37
+ },
38
+ "scripts": {
39
+ "watch": "tsc --build --watch",
40
+ "build": "tsup && rm -f tsconfig.build.tsbuildinfo && tsc --project tsconfig.build.json --emitDeclarationOnly",
41
+ "clean": "rm -rf dist tsconfig.tsbuildinfo"
42
+ },
43
+ "dependencies": {
44
+ "@cleverbrush/schema": "0.0.0-beta-20260424142030",
45
+ "@cleverbrush/async": "0.0.0-beta-20260424142030"
46
+ },
47
+ "peerDependencies": {
48
+ "@cleverbrush/di": "0.0.0-beta-20260424142030",
49
+ "@cleverbrush/server": "0.0.0-beta-20260424142030",
50
+ "@cleverbrush/knex-clickhouse": "0.0.0-beta-20260424142030"
51
+ },
52
+ "peerDependenciesMeta": {
53
+ "@cleverbrush/di": {
54
+ "optional": true
55
+ },
56
+ "@cleverbrush/server": {
57
+ "optional": true
58
+ },
59
+ "@cleverbrush/knex-clickhouse": {
60
+ "optional": true
61
+ }
62
+ },
63
+ "devDependencies": {
64
+ "@types/node": "^25.4.0"
65
+ },
66
+ "type": "module",
67
+ "types": "./dist/index.d.ts",
68
+ "version": "0.0.0-beta-20260424142030"
69
+ }