@abbacchio/transport 0.1.0 → 0.1.2

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.
@@ -1,99 +1,99 @@
1
- import { Writable } from "stream";
2
- import { AbbacchioClient, type AbbacchioClientOptions } from "../client.js";
3
-
4
- export interface BunyanStreamOptions extends AbbacchioClientOptions {
5
- /** Bunyan log level (optional) */
6
- level?: number | string;
7
- }
8
-
9
- /**
10
- * Bunyan stream for Abbacchio.
11
- * Implements the Node.js Writable stream interface for Bunyan.
12
- *
13
- * @example
14
- * ```typescript
15
- * import bunyan from "bunyan";
16
- * import { bunyanStream } from "@abbacchio/client/transports/bunyan";
17
- *
18
- * const logger = bunyan.createLogger({
19
- * name: "myapp",
20
- * streams: [
21
- * { stream: process.stdout },
22
- * bunyanStream({
23
- * url: "http://localhost:4000/api/logs",
24
- * channel: "my-app",
25
- * secretKey: "optional-encryption-key",
26
- * }),
27
- * ],
28
- * });
29
- *
30
- * logger.info("Hello from Bunyan!");
31
- * ```
32
- */
33
- export class AbbacchioBunyanStream extends Writable {
34
- private client: AbbacchioClient;
35
- public level?: number | string;
36
-
37
- constructor(opts: BunyanStreamOptions = {}) {
38
- super({ objectMode: true });
39
- this.client = new AbbacchioClient(opts);
40
- this.level = opts.level;
41
- }
42
-
43
- /**
44
- * Writable stream _write method - called for each log entry
45
- */
46
- _write(
47
- chunk: Record<string, unknown>,
48
- _encoding: BufferEncoding,
49
- callback: (error?: Error | null) => void
50
- ): void {
51
- try {
52
- // Transform Bunyan format to Abbacchio format
53
- const log = this.transformLog(chunk);
54
- this.client.add(log);
55
- callback();
56
- } catch (err) {
57
- callback(err as Error);
58
- }
59
- }
60
-
61
- /**
62
- * Transform Bunyan log format to a normalized format
63
- */
64
- private transformLog(record: Record<string, unknown>): Record<string, unknown> {
65
- const { name, hostname, pid, level, msg, time, v, ...rest } = record;
66
-
67
- return {
68
- level: level as number,
69
- msg,
70
- time: time ? new Date(time as string).getTime() : Date.now(),
71
- name,
72
- hostname,
73
- pid,
74
- ...rest,
75
- };
76
- }
77
-
78
- /**
79
- * Close the stream
80
- */
81
- _final(callback: (error?: Error | null) => void): void {
82
- this.client.close().then(() => callback()).catch(callback);
83
- }
84
- }
85
-
86
- /**
87
- * Factory function to create a Bunyan stream
88
- * Returns an object with stream and optional level for Bunyan's streams array
89
- */
90
- export function bunyanStream(opts?: BunyanStreamOptions): { stream: AbbacchioBunyanStream; level?: number | string; type: "raw" } {
91
- const stream = new AbbacchioBunyanStream(opts);
92
- return {
93
- stream,
94
- level: opts?.level,
95
- type: "raw",
96
- };
97
- }
98
-
99
- export default bunyanStream;
1
+ import { Writable } from "stream";
2
+ import { AbbacchioClient, type AbbacchioClientOptions } from "../client.js";
3
+
4
+ export interface BunyanStreamOptions extends AbbacchioClientOptions {
5
+ /** Bunyan log level (optional) */
6
+ level?: number | string;
7
+ }
8
+
9
+ /**
10
+ * Bunyan stream for Abbacchio.
11
+ * Implements the Node.js Writable stream interface for Bunyan.
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * import bunyan from "bunyan";
16
+ * import { bunyanStream } from "@abbacchio/client/transports/bunyan";
17
+ *
18
+ * const logger = bunyan.createLogger({
19
+ * name: "myapp",
20
+ * streams: [
21
+ * { stream: process.stdout },
22
+ * bunyanStream({
23
+ * url: "http://localhost:4000/api/logs",
24
+ * channel: "my-app",
25
+ * secretKey: "optional-encryption-key",
26
+ * }),
27
+ * ],
28
+ * });
29
+ *
30
+ * logger.info("Hello from Bunyan!");
31
+ * ```
32
+ */
33
+ export class AbbacchioBunyanStream extends Writable {
34
+ private client: AbbacchioClient;
35
+ public level?: number | string;
36
+
37
+ constructor(opts: BunyanStreamOptions = {}) {
38
+ super({ objectMode: true });
39
+ this.client = new AbbacchioClient(opts);
40
+ this.level = opts.level;
41
+ }
42
+
43
+ /**
44
+ * Writable stream _write method - called for each log entry
45
+ */
46
+ _write(
47
+ chunk: Record<string, unknown>,
48
+ _encoding: BufferEncoding,
49
+ callback: (error?: Error | null) => void
50
+ ): void {
51
+ try {
52
+ // Transform Bunyan format to Abbacchio format
53
+ const log = this.transformLog(chunk);
54
+ this.client.add(log);
55
+ callback();
56
+ } catch (err) {
57
+ callback(err as Error);
58
+ }
59
+ }
60
+
61
+ /**
62
+ * Transform Bunyan log format to a normalized format
63
+ */
64
+ private transformLog(record: Record<string, unknown>): Record<string, unknown> {
65
+ const { name, hostname, pid, level, msg, time, v, ...rest } = record;
66
+
67
+ return {
68
+ level: level as number,
69
+ msg,
70
+ time: time ? new Date(time as string).getTime() : Date.now(),
71
+ name,
72
+ hostname,
73
+ pid,
74
+ ...rest,
75
+ };
76
+ }
77
+
78
+ /**
79
+ * Close the stream
80
+ */
81
+ _final(callback: (error?: Error | null) => void): void {
82
+ this.client.close().then(() => callback()).catch(callback);
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Factory function to create a Bunyan stream
88
+ * Returns an object with stream and optional level for Bunyan's streams array
89
+ */
90
+ export function bunyanStream(opts?: BunyanStreamOptions): { stream: AbbacchioBunyanStream; level?: number | string; type: "raw" } {
91
+ const stream = new AbbacchioBunyanStream(opts);
92
+ return {
93
+ stream,
94
+ level: opts?.level,
95
+ type: "raw",
96
+ };
97
+ }
98
+
99
+ export default bunyanStream;
@@ -1,147 +1,147 @@
1
- import { AbbacchioClient, type AbbacchioClientOptions } from "../client.js";
2
-
3
- export interface ConsoleInterceptorOptions extends AbbacchioClientOptions {
4
- /** Which console methods to intercept. Defaults to all. */
5
- methods?: ("log" | "info" | "warn" | "error" | "debug")[];
6
- /** Whether to still output to original console. Defaults to true. */
7
- passthrough?: boolean;
8
- }
9
-
10
- type ConsoleMethod = "log" | "info" | "warn" | "error" | "debug";
11
-
12
- const methodToLevel: Record<ConsoleMethod, number> = {
13
- debug: 20,
14
- log: 30,
15
- info: 30,
16
- warn: 40,
17
- error: 50,
18
- };
19
-
20
- /**
21
- * Console interceptor for Abbacchio.
22
- * Intercepts console.log/info/warn/error/debug calls and sends them to Abbacchio.
23
- *
24
- * @example
25
- * ```typescript
26
- * import { interceptConsole, restoreConsole } from "@abbacchio/client/transports/console";
27
- *
28
- * // Start intercepting console calls
29
- * interceptConsole({
30
- * url: "http://localhost:4000/api/logs",
31
- * channel: "my-app",
32
- * secretKey: "optional-encryption-key",
33
- * passthrough: true, // Still log to console
34
- * });
35
- *
36
- * console.log("This will be sent to Abbacchio!");
37
- * console.error("Errors too!");
38
- *
39
- * // Stop intercepting when done
40
- * restoreConsole();
41
- * ```
42
- */
43
-
44
- // Store original console methods
45
- const originalConsole: Record<ConsoleMethod, (...args: unknown[]) => void> = {
46
- log: console.log.bind(console),
47
- info: console.info.bind(console),
48
- warn: console.warn.bind(console),
49
- error: console.error.bind(console),
50
- debug: console.debug.bind(console),
51
- };
52
-
53
- let activeClient: AbbacchioClient | null = null;
54
- let activeOptions: ConsoleInterceptorOptions | null = null;
55
-
56
- /**
57
- * Format console arguments into a message string
58
- */
59
- function formatArgs(args: unknown[]): string {
60
- return args
61
- .map((arg) => {
62
- if (typeof arg === "string") return arg;
63
- if (arg instanceof Error) return `${arg.name}: ${arg.message}\n${arg.stack}`;
64
- try {
65
- return JSON.stringify(arg);
66
- } catch {
67
- return String(arg);
68
- }
69
- })
70
- .join(" ");
71
- }
72
-
73
- /**
74
- * Create an intercepted console method
75
- */
76
- function createInterceptedMethod(
77
- method: ConsoleMethod,
78
- client: AbbacchioClient,
79
- passthrough: boolean
80
- ): (...args: unknown[]) => void {
81
- return (...args: unknown[]) => {
82
- // Send to Abbacchio
83
- const log = {
84
- level: methodToLevel[method],
85
- msg: formatArgs(args),
86
- time: Date.now(),
87
- method,
88
- };
89
- client.add(log);
90
-
91
- // Optionally pass through to original console
92
- if (passthrough) {
93
- originalConsole[method](...args);
94
- }
95
- };
96
- }
97
-
98
- /**
99
- * Start intercepting console calls
100
- */
101
- export function interceptConsole(opts: ConsoleInterceptorOptions = {}): void {
102
- // Restore any existing interception first
103
- if (activeClient) {
104
- restoreConsole();
105
- }
106
-
107
- const methods = opts.methods || ["log", "info", "warn", "error", "debug"];
108
- const passthrough = opts.passthrough !== false;
109
-
110
- activeClient = new AbbacchioClient(opts);
111
- activeOptions = opts;
112
-
113
- // Replace console methods
114
- for (const method of methods) {
115
- (console as unknown as Record<string, unknown>)[method] = createInterceptedMethod(
116
- method,
117
- activeClient,
118
- passthrough
119
- );
120
- }
121
- }
122
-
123
- /**
124
- * Stop intercepting console calls and restore original behavior
125
- */
126
- export function restoreConsole(): void {
127
- // Restore original console methods
128
- for (const method of Object.keys(originalConsole) as ConsoleMethod[]) {
129
- (console as unknown as Record<string, unknown>)[method] = originalConsole[method];
130
- }
131
-
132
- // Flush and close client
133
- if (activeClient) {
134
- activeClient.close();
135
- activeClient = null;
136
- activeOptions = null;
137
- }
138
- }
139
-
140
- /**
141
- * Get the active client (for testing)
142
- */
143
- export function getActiveClient(): AbbacchioClient | null {
144
- return activeClient;
145
- }
146
-
147
- export default interceptConsole;
1
+ import { AbbacchioClient, type AbbacchioClientOptions } from "../client.js";
2
+
3
+ export interface ConsoleInterceptorOptions extends AbbacchioClientOptions {
4
+ /** Which console methods to intercept. Defaults to all. */
5
+ methods?: ("log" | "info" | "warn" | "error" | "debug")[];
6
+ /** Whether to still output to original console. Defaults to true. */
7
+ passthrough?: boolean;
8
+ }
9
+
10
+ type ConsoleMethod = "log" | "info" | "warn" | "error" | "debug";
11
+
12
+ const methodToLevel: Record<ConsoleMethod, number> = {
13
+ debug: 20,
14
+ log: 30,
15
+ info: 30,
16
+ warn: 40,
17
+ error: 50,
18
+ };
19
+
20
+ /**
21
+ * Console interceptor for Abbacchio.
22
+ * Intercepts console.log/info/warn/error/debug calls and sends them to Abbacchio.
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * import { interceptConsole, restoreConsole } from "@abbacchio/client/transports/console";
27
+ *
28
+ * // Start intercepting console calls
29
+ * interceptConsole({
30
+ * url: "http://localhost:4000/api/logs",
31
+ * channel: "my-app",
32
+ * secretKey: "optional-encryption-key",
33
+ * passthrough: true, // Still log to console
34
+ * });
35
+ *
36
+ * console.log("This will be sent to Abbacchio!");
37
+ * console.error("Errors too!");
38
+ *
39
+ * // Stop intercepting when done
40
+ * restoreConsole();
41
+ * ```
42
+ */
43
+
44
+ // Store original console methods
45
+ const originalConsole: Record<ConsoleMethod, (...args: unknown[]) => void> = {
46
+ log: console.log.bind(console),
47
+ info: console.info.bind(console),
48
+ warn: console.warn.bind(console),
49
+ error: console.error.bind(console),
50
+ debug: console.debug.bind(console),
51
+ };
52
+
53
+ let activeClient: AbbacchioClient | null = null;
54
+ let activeOptions: ConsoleInterceptorOptions | null = null;
55
+
56
+ /**
57
+ * Format console arguments into a message string
58
+ */
59
+ function formatArgs(args: unknown[]): string {
60
+ return args
61
+ .map((arg) => {
62
+ if (typeof arg === "string") return arg;
63
+ if (arg instanceof Error) return `${arg.name}: ${arg.message}\n${arg.stack}`;
64
+ try {
65
+ return JSON.stringify(arg);
66
+ } catch {
67
+ return String(arg);
68
+ }
69
+ })
70
+ .join(" ");
71
+ }
72
+
73
+ /**
74
+ * Create an intercepted console method
75
+ */
76
+ function createInterceptedMethod(
77
+ method: ConsoleMethod,
78
+ client: AbbacchioClient,
79
+ passthrough: boolean
80
+ ): (...args: unknown[]) => void {
81
+ return (...args: unknown[]) => {
82
+ // Send to Abbacchio
83
+ const log = {
84
+ level: methodToLevel[method],
85
+ msg: formatArgs(args),
86
+ time: Date.now(),
87
+ method,
88
+ };
89
+ client.add(log);
90
+
91
+ // Optionally pass through to original console
92
+ if (passthrough) {
93
+ originalConsole[method](...args);
94
+ }
95
+ };
96
+ }
97
+
98
+ /**
99
+ * Start intercepting console calls
100
+ */
101
+ export function interceptConsole(opts: ConsoleInterceptorOptions = {}): void {
102
+ // Restore any existing interception first
103
+ if (activeClient) {
104
+ restoreConsole();
105
+ }
106
+
107
+ const methods = opts.methods || ["log", "info", "warn", "error", "debug"];
108
+ const passthrough = opts.passthrough !== false;
109
+
110
+ activeClient = new AbbacchioClient(opts);
111
+ activeOptions = opts;
112
+
113
+ // Replace console methods
114
+ for (const method of methods) {
115
+ (console as unknown as Record<string, unknown>)[method] = createInterceptedMethod(
116
+ method,
117
+ activeClient,
118
+ passthrough
119
+ );
120
+ }
121
+ }
122
+
123
+ /**
124
+ * Stop intercepting console calls and restore original behavior
125
+ */
126
+ export function restoreConsole(): void {
127
+ // Restore original console methods
128
+ for (const method of Object.keys(originalConsole) as ConsoleMethod[]) {
129
+ (console as unknown as Record<string, unknown>)[method] = originalConsole[method];
130
+ }
131
+
132
+ // Flush and close client
133
+ if (activeClient) {
134
+ activeClient.close();
135
+ activeClient = null;
136
+ activeOptions = null;
137
+ }
138
+ }
139
+
140
+ /**
141
+ * Get the active client (for testing)
142
+ */
143
+ export function getActiveClient(): AbbacchioClient | null {
144
+ return activeClient;
145
+ }
146
+
147
+ export default interceptConsole;
@@ -1,15 +1,15 @@
1
- // Pino transport
2
- export { default as pinoTransport } from "./pino.js";
3
- export type { PinoTransportOptions } from "./pino.js";
4
-
5
- // Winston transport
6
- export { winstonTransport, AbbacchioWinstonTransport } from "./winston.js";
7
- export type { WinstonTransportOptions } from "./winston.js";
8
-
9
- // Bunyan stream
10
- export { bunyanStream, AbbacchioBunyanStream } from "./bunyan.js";
11
- export type { BunyanStreamOptions } from "./bunyan.js";
12
-
13
- // Console interceptor
14
- export { interceptConsole, restoreConsole, getActiveClient } from "./console.js";
15
- export type { ConsoleInterceptorOptions } from "./console.js";
1
+ // Pino transport
2
+ export { default as pinoTransport } from "./pino.js";
3
+ export type { PinoTransportOptions } from "./pino.js";
4
+
5
+ // Winston transport
6
+ export { winstonTransport, AbbacchioWinstonTransport } from "./winston.js";
7
+ export type { WinstonTransportOptions } from "./winston.js";
8
+
9
+ // Bunyan stream
10
+ export { bunyanStream, AbbacchioBunyanStream } from "./bunyan.js";
11
+ export type { BunyanStreamOptions } from "./bunyan.js";
12
+
13
+ // Console interceptor
14
+ export { interceptConsole, restoreConsole, getActiveClient } from "./console.js";
15
+ export type { ConsoleInterceptorOptions } from "./console.js";
@@ -1,49 +1,49 @@
1
- import build from "pino-abstract-transport";
2
- import { AbbacchioClient, type AbbacchioClientOptions } from "../client.js";
3
-
4
- export interface PinoTransportOptions extends AbbacchioClientOptions {}
5
-
6
- /**
7
- * Pino transport for Abbacchio.
8
- *
9
- * @example
10
- * ```typescript
11
- * import pino from "pino";
12
- *
13
- * const logger = pino({
14
- * transport: {
15
- * target: "@abbacchio/client/transports/pino",
16
- * options: {
17
- * url: "http://localhost:4000/api/logs",
18
- * channel: "my-app",
19
- * secretKey: "optional-encryption-key",
20
- * },
21
- * },
22
- * });
23
- *
24
- * logger.info("Hello from Pino!");
25
- * ```
26
- */
27
- export default async function pinoTransport(opts: PinoTransportOptions = {}) {
28
- const client = new AbbacchioClient(opts);
29
-
30
- return build(
31
- async function (source) {
32
- for await (const obj of source) {
33
- client.add(obj);
34
- }
35
- // Flush remaining on close
36
- await client.flush();
37
- },
38
- {
39
- async close() {
40
- await client.close();
41
- },
42
- }
43
- );
44
- }
45
-
46
- /**
47
- * Named export for programmatic usage
48
- */
49
- export { pinoTransport };
1
+ import build from "pino-abstract-transport";
2
+ import { AbbacchioClient, type AbbacchioClientOptions } from "../client.js";
3
+
4
+ export interface PinoTransportOptions extends AbbacchioClientOptions {}
5
+
6
+ /**
7
+ * Pino transport for Abbacchio.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import pino from "pino";
12
+ *
13
+ * const logger = pino({
14
+ * transport: {
15
+ * target: "@abbacchio/client/transports/pino",
16
+ * options: {
17
+ * url: "http://localhost:4000/api/logs",
18
+ * channel: "my-app",
19
+ * secretKey: "optional-encryption-key",
20
+ * },
21
+ * },
22
+ * });
23
+ *
24
+ * logger.info("Hello from Pino!");
25
+ * ```
26
+ */
27
+ export default async function pinoTransport(opts: PinoTransportOptions = {}) {
28
+ const client = new AbbacchioClient(opts);
29
+
30
+ return build(
31
+ async function (source) {
32
+ for await (const obj of source) {
33
+ client.add(obj);
34
+ }
35
+ // Flush remaining on close
36
+ await client.flush();
37
+ },
38
+ {
39
+ async close() {
40
+ await client.close();
41
+ },
42
+ }
43
+ );
44
+ }
45
+
46
+ /**
47
+ * Named export for programmatic usage
48
+ */
49
+ export { pinoTransport };