@abbacchio/transport 0.1.1 → 0.1.3
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.
- package/README.md +121 -0
- package/package.json +16 -2
- package/pino-transport.cjs +30 -0
- package/src/client.ts +148 -148
- package/src/encrypt.ts +112 -112
- package/src/index.ts +19 -19
- package/src/transports/bunyan.ts +99 -99
- package/src/transports/console.ts +147 -147
- package/src/transports/index.ts +15 -15
- package/src/transports/pino.ts +49 -49
- package/src/transports/winston.ts +100 -100
- package/tsconfig.json +19 -19
|
@@ -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;
|
package/src/transports/index.ts
CHANGED
|
@@ -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";
|
package/src/transports/pino.ts
CHANGED
|
@@ -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 };
|
|
@@ -1,100 +1,100 @@
|
|
|
1
|
-
import TransportStream from "winston-transport";
|
|
2
|
-
import { AbbacchioClient, type AbbacchioClientOptions } from "../client.js";
|
|
3
|
-
|
|
4
|
-
export interface WinstonTransportOptions extends AbbacchioClientOptions {
|
|
5
|
-
/** Winston log level (optional) */
|
|
6
|
-
level?: string;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Winston transport for Abbacchio.
|
|
11
|
-
* Extends winston-transport for proper integration.
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```typescript
|
|
15
|
-
* import winston from "winston";
|
|
16
|
-
* import { AbbacchioWinstonTransport } from "@abbacchio/client/transports/winston";
|
|
17
|
-
*
|
|
18
|
-
* const logger = winston.createLogger({
|
|
19
|
-
* transports: [
|
|
20
|
-
* new winston.transports.Console(),
|
|
21
|
-
* new AbbacchioWinstonTransport({
|
|
22
|
-
* url: "http://localhost:4000/api/logs",
|
|
23
|
-
* channel: "my-app",
|
|
24
|
-
* secretKey: "optional-encryption-key",
|
|
25
|
-
* }),
|
|
26
|
-
* ],
|
|
27
|
-
* });
|
|
28
|
-
*
|
|
29
|
-
* logger.info("Hello from Winston!");
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
export class AbbacchioWinstonTransport extends TransportStream {
|
|
33
|
-
private client: AbbacchioClient;
|
|
34
|
-
|
|
35
|
-
constructor(opts: WinstonTransportOptions = {}) {
|
|
36
|
-
super({ level: opts.level });
|
|
37
|
-
this.client = new AbbacchioClient(opts);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Winston log method - called for each log entry
|
|
42
|
-
*/
|
|
43
|
-
log(info: Record<string, unknown>, callback: () => void): void {
|
|
44
|
-
setImmediate(() => {
|
|
45
|
-
this.emit("logged", info);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
// Transform Winston format to Abbacchio format
|
|
49
|
-
const log = this.transformLog(info);
|
|
50
|
-
this.client.add(log);
|
|
51
|
-
|
|
52
|
-
callback();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Transform Winston log format to a normalized format
|
|
57
|
-
*/
|
|
58
|
-
private transformLog(info: Record<string, unknown>): Record<string, unknown> {
|
|
59
|
-
const { level, message, timestamp, ...rest } = info;
|
|
60
|
-
|
|
61
|
-
return {
|
|
62
|
-
level: this.levelToNumber(level as string),
|
|
63
|
-
msg: message,
|
|
64
|
-
time: timestamp ? new Date(timestamp as string).getTime() : Date.now(),
|
|
65
|
-
...rest,
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Convert Winston level string to Pino-style number
|
|
71
|
-
*/
|
|
72
|
-
private levelToNumber(level: string): number {
|
|
73
|
-
const levels: Record<string, number> = {
|
|
74
|
-
error: 50,
|
|
75
|
-
warn: 40,
|
|
76
|
-
info: 30,
|
|
77
|
-
http: 30,
|
|
78
|
-
verbose: 20,
|
|
79
|
-
debug: 20,
|
|
80
|
-
silly: 10,
|
|
81
|
-
};
|
|
82
|
-
return levels[level] || 30;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Close the transport
|
|
87
|
-
*/
|
|
88
|
-
close(): void {
|
|
89
|
-
this.client.close();
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Factory function to create a Winston transport
|
|
95
|
-
*/
|
|
96
|
-
export function winstonTransport(opts?: WinstonTransportOptions): AbbacchioWinstonTransport {
|
|
97
|
-
return new AbbacchioWinstonTransport(opts);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export default winstonTransport;
|
|
1
|
+
import TransportStream from "winston-transport";
|
|
2
|
+
import { AbbacchioClient, type AbbacchioClientOptions } from "../client.js";
|
|
3
|
+
|
|
4
|
+
export interface WinstonTransportOptions extends AbbacchioClientOptions {
|
|
5
|
+
/** Winston log level (optional) */
|
|
6
|
+
level?: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Winston transport for Abbacchio.
|
|
11
|
+
* Extends winston-transport for proper integration.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import winston from "winston";
|
|
16
|
+
* import { AbbacchioWinstonTransport } from "@abbacchio/client/transports/winston";
|
|
17
|
+
*
|
|
18
|
+
* const logger = winston.createLogger({
|
|
19
|
+
* transports: [
|
|
20
|
+
* new winston.transports.Console(),
|
|
21
|
+
* new AbbacchioWinstonTransport({
|
|
22
|
+
* url: "http://localhost:4000/api/logs",
|
|
23
|
+
* channel: "my-app",
|
|
24
|
+
* secretKey: "optional-encryption-key",
|
|
25
|
+
* }),
|
|
26
|
+
* ],
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* logger.info("Hello from Winston!");
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export class AbbacchioWinstonTransport extends TransportStream {
|
|
33
|
+
private client: AbbacchioClient;
|
|
34
|
+
|
|
35
|
+
constructor(opts: WinstonTransportOptions = {}) {
|
|
36
|
+
super({ level: opts.level });
|
|
37
|
+
this.client = new AbbacchioClient(opts);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Winston log method - called for each log entry
|
|
42
|
+
*/
|
|
43
|
+
log(info: Record<string, unknown>, callback: () => void): void {
|
|
44
|
+
setImmediate(() => {
|
|
45
|
+
this.emit("logged", info);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// Transform Winston format to Abbacchio format
|
|
49
|
+
const log = this.transformLog(info);
|
|
50
|
+
this.client.add(log);
|
|
51
|
+
|
|
52
|
+
callback();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Transform Winston log format to a normalized format
|
|
57
|
+
*/
|
|
58
|
+
private transformLog(info: Record<string, unknown>): Record<string, unknown> {
|
|
59
|
+
const { level, message, timestamp, ...rest } = info;
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
level: this.levelToNumber(level as string),
|
|
63
|
+
msg: message,
|
|
64
|
+
time: timestamp ? new Date(timestamp as string).getTime() : Date.now(),
|
|
65
|
+
...rest,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Convert Winston level string to Pino-style number
|
|
71
|
+
*/
|
|
72
|
+
private levelToNumber(level: string): number {
|
|
73
|
+
const levels: Record<string, number> = {
|
|
74
|
+
error: 50,
|
|
75
|
+
warn: 40,
|
|
76
|
+
info: 30,
|
|
77
|
+
http: 30,
|
|
78
|
+
verbose: 20,
|
|
79
|
+
debug: 20,
|
|
80
|
+
silly: 10,
|
|
81
|
+
};
|
|
82
|
+
return levels[level] || 30;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Close the transport
|
|
87
|
+
*/
|
|
88
|
+
close(): void {
|
|
89
|
+
this.client.close();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Factory function to create a Winston transport
|
|
95
|
+
*/
|
|
96
|
+
export function winstonTransport(opts?: WinstonTransportOptions): AbbacchioWinstonTransport {
|
|
97
|
+
return new AbbacchioWinstonTransport(opts);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export default winstonTransport;
|
package/tsconfig.json
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2022",
|
|
4
|
-
"module": "NodeNext",
|
|
5
|
-
"moduleResolution": "NodeNext",
|
|
6
|
-
"lib": ["ES2022"],
|
|
7
|
-
"outDir": "./dist",
|
|
8
|
-
"rootDir": "./src",
|
|
9
|
-
"strict": true,
|
|
10
|
-
"esModuleInterop": true,
|
|
11
|
-
"skipLibCheck": true,
|
|
12
|
-
"forceConsistentCasingInFileNames": true,
|
|
13
|
-
"declaration": true,
|
|
14
|
-
"declarationMap": true,
|
|
15
|
-
"sourceMap": true
|
|
16
|
-
},
|
|
17
|
-
"include": ["src/**/*"],
|
|
18
|
-
"exclude": ["node_modules", "dist"]
|
|
19
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "NodeNext",
|
|
5
|
+
"moduleResolution": "NodeNext",
|
|
6
|
+
"lib": ["ES2022"],
|
|
7
|
+
"outDir": "./dist",
|
|
8
|
+
"rootDir": "./src",
|
|
9
|
+
"strict": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"forceConsistentCasingInFileNames": true,
|
|
13
|
+
"declaration": true,
|
|
14
|
+
"declarationMap": true,
|
|
15
|
+
"sourceMap": true
|
|
16
|
+
},
|
|
17
|
+
"include": ["src/**/*"],
|
|
18
|
+
"exclude": ["node_modules", "dist"]
|
|
19
|
+
}
|