@abbacchio/transport 0.1.0
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/dist/client.d.ts +66 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +123 -0
- package/dist/client.js.map +1 -0
- package/dist/encrypt.d.ts +53 -0
- package/dist/encrypt.d.ts.map +1 -0
- package/dist/encrypt.js +97 -0
- package/dist/encrypt.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/transports/bunyan.d.ts +58 -0
- package/dist/transports/bunyan.d.ts.map +1 -0
- package/dist/transports/bunyan.js +84 -0
- package/dist/transports/bunyan.js.map +1 -0
- package/dist/transports/console.d.ts +21 -0
- package/dist/transports/console.d.ts.map +1 -0
- package/dist/transports/console.js +119 -0
- package/dist/transports/console.js.map +1 -0
- package/dist/transports/index.d.ts +9 -0
- package/dist/transports/index.d.ts.map +1 -0
- package/dist/transports/index.js +9 -0
- package/dist/transports/index.js.map +1 -0
- package/dist/transports/pino.d.ts +31 -0
- package/dist/transports/pino.d.ts.map +1 -0
- package/dist/transports/pino.js +42 -0
- package/dist/transports/pino.js.map +1 -0
- package/dist/transports/winston.d.ts +55 -0
- package/dist/transports/winston.d.ts.map +1 -0
- package/dist/transports/winston.js +85 -0
- package/dist/transports/winston.js.map +1 -0
- package/package.json +56 -0
- package/src/client.ts +148 -0
- package/src/encrypt.ts +112 -0
- package/src/index.ts +19 -0
- package/src/transports/bunyan.ts +99 -0
- package/src/transports/console.ts +147 -0
- package/src/transports/index.ts +15 -0
- package/src/transports/pino.ts +49 -0
- package/src/transports/winston.ts +100 -0
- package/tsconfig.json +19 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { AbbacchioClient } from "../client.js";
|
|
2
|
+
const methodToLevel = {
|
|
3
|
+
debug: 20,
|
|
4
|
+
log: 30,
|
|
5
|
+
info: 30,
|
|
6
|
+
warn: 40,
|
|
7
|
+
error: 50,
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Console interceptor for Abbacchio.
|
|
11
|
+
* Intercepts console.log/info/warn/error/debug calls and sends them to Abbacchio.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import { interceptConsole, restoreConsole } from "@abbacchio/client/transports/console";
|
|
16
|
+
*
|
|
17
|
+
* // Start intercepting console calls
|
|
18
|
+
* interceptConsole({
|
|
19
|
+
* url: "http://localhost:4000/api/logs",
|
|
20
|
+
* channel: "my-app",
|
|
21
|
+
* secretKey: "optional-encryption-key",
|
|
22
|
+
* passthrough: true, // Still log to console
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* console.log("This will be sent to Abbacchio!");
|
|
26
|
+
* console.error("Errors too!");
|
|
27
|
+
*
|
|
28
|
+
* // Stop intercepting when done
|
|
29
|
+
* restoreConsole();
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
// Store original console methods
|
|
33
|
+
const originalConsole = {
|
|
34
|
+
log: console.log.bind(console),
|
|
35
|
+
info: console.info.bind(console),
|
|
36
|
+
warn: console.warn.bind(console),
|
|
37
|
+
error: console.error.bind(console),
|
|
38
|
+
debug: console.debug.bind(console),
|
|
39
|
+
};
|
|
40
|
+
let activeClient = null;
|
|
41
|
+
let activeOptions = null;
|
|
42
|
+
/**
|
|
43
|
+
* Format console arguments into a message string
|
|
44
|
+
*/
|
|
45
|
+
function formatArgs(args) {
|
|
46
|
+
return args
|
|
47
|
+
.map((arg) => {
|
|
48
|
+
if (typeof arg === "string")
|
|
49
|
+
return arg;
|
|
50
|
+
if (arg instanceof Error)
|
|
51
|
+
return `${arg.name}: ${arg.message}\n${arg.stack}`;
|
|
52
|
+
try {
|
|
53
|
+
return JSON.stringify(arg);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
return String(arg);
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
.join(" ");
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Create an intercepted console method
|
|
63
|
+
*/
|
|
64
|
+
function createInterceptedMethod(method, client, passthrough) {
|
|
65
|
+
return (...args) => {
|
|
66
|
+
// Send to Abbacchio
|
|
67
|
+
const log = {
|
|
68
|
+
level: methodToLevel[method],
|
|
69
|
+
msg: formatArgs(args),
|
|
70
|
+
time: Date.now(),
|
|
71
|
+
method,
|
|
72
|
+
};
|
|
73
|
+
client.add(log);
|
|
74
|
+
// Optionally pass through to original console
|
|
75
|
+
if (passthrough) {
|
|
76
|
+
originalConsole[method](...args);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Start intercepting console calls
|
|
82
|
+
*/
|
|
83
|
+
export function interceptConsole(opts = {}) {
|
|
84
|
+
// Restore any existing interception first
|
|
85
|
+
if (activeClient) {
|
|
86
|
+
restoreConsole();
|
|
87
|
+
}
|
|
88
|
+
const methods = opts.methods || ["log", "info", "warn", "error", "debug"];
|
|
89
|
+
const passthrough = opts.passthrough !== false;
|
|
90
|
+
activeClient = new AbbacchioClient(opts);
|
|
91
|
+
activeOptions = opts;
|
|
92
|
+
// Replace console methods
|
|
93
|
+
for (const method of methods) {
|
|
94
|
+
console[method] = createInterceptedMethod(method, activeClient, passthrough);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Stop intercepting console calls and restore original behavior
|
|
99
|
+
*/
|
|
100
|
+
export function restoreConsole() {
|
|
101
|
+
// Restore original console methods
|
|
102
|
+
for (const method of Object.keys(originalConsole)) {
|
|
103
|
+
console[method] = originalConsole[method];
|
|
104
|
+
}
|
|
105
|
+
// Flush and close client
|
|
106
|
+
if (activeClient) {
|
|
107
|
+
activeClient.close();
|
|
108
|
+
activeClient = null;
|
|
109
|
+
activeOptions = null;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Get the active client (for testing)
|
|
114
|
+
*/
|
|
115
|
+
export function getActiveClient() {
|
|
116
|
+
return activeClient;
|
|
117
|
+
}
|
|
118
|
+
export default interceptConsole;
|
|
119
|
+
//# sourceMappingURL=console.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"console.js","sourceRoot":"","sources":["../../src/transports/console.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAA+B,MAAM,cAAc,CAAC;AAW5E,MAAM,aAAa,GAAkC;IACnD,KAAK,EAAE,EAAE;IACT,GAAG,EAAE,EAAE;IACP,IAAI,EAAE,EAAE;IACR,IAAI,EAAE,EAAE;IACR,KAAK,EAAE,EAAE;CACV,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,iCAAiC;AACjC,MAAM,eAAe,GAAwD;IAC3E,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC;IAC9B,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC;IAChC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC;IAChC,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC;IAClC,KAAK,EAAE,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC;CACnC,CAAC;AAEF,IAAI,YAAY,GAA2B,IAAI,CAAC;AAChD,IAAI,aAAa,GAAqC,IAAI,CAAC;AAE3D;;GAEG;AACH,SAAS,UAAU,CAAC,IAAe;IACjC,OAAO,IAAI;SACR,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE;QACX,IAAI,OAAO,GAAG,KAAK,QAAQ;YAAE,OAAO,GAAG,CAAC;QACxC,IAAI,GAAG,YAAY,KAAK;YAAE,OAAO,GAAG,GAAG,CAAC,IAAI,KAAK,GAAG,CAAC,OAAO,KAAK,GAAG,CAAC,KAAK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GAAG,uBAAuB,CAC/E,MAAM,EACN,YAAY,EACZ,WAAW,CACZ,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,cAAc;IAC5B,mCAAmC;IACnC,KAAK,MAAM,MAAM,IAAI,MAAM,CAAC,IAAI,CAAC,eAAe,CAAoB,EAAE,CAAC;QACpE,OAA8C,CAAC,MAAM,CAAC,GAAG,eAAe,CAAC,MAAM,CAAC,CAAC;IACpF,CAAC;IAED,yBAAyB;IACzB,IAAI,YAAY,EAAE,CAAC;QACjB,YAAY,CAAC,KAAK,EAAE,CAAC;QACrB,YAAY,GAAG,IAAI,CAAC;QACpB,aAAa,GAAG,IAAI,CAAC;IACvB,CAAC;AACH,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,eAAe;IAC7B,OAAO,YAAY,CAAC;AACtB,CAAC;AAED,eAAe,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { default as pinoTransport } from "./pino.js";
|
|
2
|
+
export type { PinoTransportOptions } from "./pino.js";
|
|
3
|
+
export { winstonTransport, AbbacchioWinstonTransport } from "./winston.js";
|
|
4
|
+
export type { WinstonTransportOptions } from "./winston.js";
|
|
5
|
+
export { bunyanStream, AbbacchioBunyanStream } from "./bunyan.js";
|
|
6
|
+
export type { BunyanStreamOptions } from "./bunyan.js";
|
|
7
|
+
export { interceptConsole, restoreConsole, getActiveClient } from "./console.js";
|
|
8
|
+
export type { ConsoleInterceptorOptions } from "./console.js";
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/transports/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,WAAW,CAAC;AACrD,YAAY,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAGtD,OAAO,EAAE,gBAAgB,EAAE,yBAAyB,EAAE,MAAM,cAAc,CAAC;AAC3E,YAAY,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAG5D,OAAO,EAAE,YAAY,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAClE,YAAY,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAGvD,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AACjF,YAAY,EAAE,yBAAyB,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Pino transport
|
|
2
|
+
export { default as pinoTransport } from "./pino.js";
|
|
3
|
+
// Winston transport
|
|
4
|
+
export { winstonTransport, AbbacchioWinstonTransport } from "./winston.js";
|
|
5
|
+
// Bunyan stream
|
|
6
|
+
export { bunyanStream, AbbacchioBunyanStream } from "./bunyan.js";
|
|
7
|
+
// Console interceptor
|
|
8
|
+
export { interceptConsole, restoreConsole, getActiveClient } from "./console.js";
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/transports/index.ts"],"names":[],"mappings":"AAAA,iBAAiB;AACjB,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,WAAW,CAAC;AAGrD,oBAAoB;AACpB,OAAO,EAAE,gBAAgB,EAAE,yBAAyB,EAAE,MAAM,cAAc,CAAC;AAG3E,gBAAgB;AAChB,OAAO,EAAE,YAAY,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAGlE,sBAAsB;AACtB,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import build from "pino-abstract-transport";
|
|
2
|
+
import { type AbbacchioClientOptions } from "../client.js";
|
|
3
|
+
export interface PinoTransportOptions extends AbbacchioClientOptions {
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Pino transport for Abbacchio.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import pino from "pino";
|
|
11
|
+
*
|
|
12
|
+
* const logger = pino({
|
|
13
|
+
* transport: {
|
|
14
|
+
* target: "@abbacchio/client/transports/pino",
|
|
15
|
+
* options: {
|
|
16
|
+
* url: "http://localhost:4000/api/logs",
|
|
17
|
+
* channel: "my-app",
|
|
18
|
+
* secretKey: "optional-encryption-key",
|
|
19
|
+
* },
|
|
20
|
+
* },
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* logger.info("Hello from Pino!");
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export default function pinoTransport(opts?: PinoTransportOptions): Promise<import("stream").Transform & build.OnUnknown>;
|
|
27
|
+
/**
|
|
28
|
+
* Named export for programmatic usage
|
|
29
|
+
*/
|
|
30
|
+
export { pinoTransport };
|
|
31
|
+
//# sourceMappingURL=pino.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pino.d.ts","sourceRoot":"","sources":["../../src/transports/pino.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,yBAAyB,CAAC;AAC5C,OAAO,EAAmB,KAAK,sBAAsB,EAAE,MAAM,cAAc,CAAC;AAE5E,MAAM,WAAW,oBAAqB,SAAQ,sBAAsB;CAAG;AAEvE;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAA8B,aAAa,CAAC,IAAI,GAAE,oBAAyB,yDAiB1E;AAED;;GAEG;AACH,OAAO,EAAE,aAAa,EAAE,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import build from "pino-abstract-transport";
|
|
2
|
+
import { AbbacchioClient } from "../client.js";
|
|
3
|
+
/**
|
|
4
|
+
* Pino transport for Abbacchio.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import pino from "pino";
|
|
9
|
+
*
|
|
10
|
+
* const logger = pino({
|
|
11
|
+
* transport: {
|
|
12
|
+
* target: "@abbacchio/client/transports/pino",
|
|
13
|
+
* options: {
|
|
14
|
+
* url: "http://localhost:4000/api/logs",
|
|
15
|
+
* channel: "my-app",
|
|
16
|
+
* secretKey: "optional-encryption-key",
|
|
17
|
+
* },
|
|
18
|
+
* },
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* logger.info("Hello from Pino!");
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export default async function pinoTransport(opts = {}) {
|
|
25
|
+
const client = new AbbacchioClient(opts);
|
|
26
|
+
return build(async function (source) {
|
|
27
|
+
for await (const obj of source) {
|
|
28
|
+
client.add(obj);
|
|
29
|
+
}
|
|
30
|
+
// Flush remaining on close
|
|
31
|
+
await client.flush();
|
|
32
|
+
}, {
|
|
33
|
+
async close() {
|
|
34
|
+
await client.close();
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Named export for programmatic usage
|
|
40
|
+
*/
|
|
41
|
+
export { pinoTransport };
|
|
42
|
+
//# sourceMappingURL=pino.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pino.js","sourceRoot":"","sources":["../../src/transports/pino.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,yBAAyB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAA+B,MAAM,cAAc,CAAC;AAI5E;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,CAAC,OAAO,CAAC,KAAK,UAAU,aAAa,CAAC,OAA6B,EAAE;IACzE,MAAM,MAAM,GAAG,IAAI,eAAe,CAAC,IAAI,CAAC,CAAC;IAEzC,OAAO,KAAK,CACV,KAAK,WAAW,MAAM;QACpB,IAAI,KAAK,EAAE,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;YAC/B,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAClB,CAAC;QACD,2BAA2B;QAC3B,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC,EACD;QACE,KAAK,CAAC,KAAK;YACT,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;QACvB,CAAC;KACF,CACF,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,OAAO,EAAE,aAAa,EAAE,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import TransportStream from "winston-transport";
|
|
2
|
+
import { type AbbacchioClientOptions } from "../client.js";
|
|
3
|
+
export interface WinstonTransportOptions extends AbbacchioClientOptions {
|
|
4
|
+
/** Winston log level (optional) */
|
|
5
|
+
level?: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Winston transport for Abbacchio.
|
|
9
|
+
* Extends winston-transport for proper integration.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* import winston from "winston";
|
|
14
|
+
* import { AbbacchioWinstonTransport } from "@abbacchio/client/transports/winston";
|
|
15
|
+
*
|
|
16
|
+
* const logger = winston.createLogger({
|
|
17
|
+
* transports: [
|
|
18
|
+
* new winston.transports.Console(),
|
|
19
|
+
* new AbbacchioWinstonTransport({
|
|
20
|
+
* url: "http://localhost:4000/api/logs",
|
|
21
|
+
* channel: "my-app",
|
|
22
|
+
* secretKey: "optional-encryption-key",
|
|
23
|
+
* }),
|
|
24
|
+
* ],
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* logger.info("Hello from Winston!");
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare class AbbacchioWinstonTransport extends TransportStream {
|
|
31
|
+
private client;
|
|
32
|
+
constructor(opts?: WinstonTransportOptions);
|
|
33
|
+
/**
|
|
34
|
+
* Winston log method - called for each log entry
|
|
35
|
+
*/
|
|
36
|
+
log(info: Record<string, unknown>, callback: () => void): void;
|
|
37
|
+
/**
|
|
38
|
+
* Transform Winston log format to a normalized format
|
|
39
|
+
*/
|
|
40
|
+
private transformLog;
|
|
41
|
+
/**
|
|
42
|
+
* Convert Winston level string to Pino-style number
|
|
43
|
+
*/
|
|
44
|
+
private levelToNumber;
|
|
45
|
+
/**
|
|
46
|
+
* Close the transport
|
|
47
|
+
*/
|
|
48
|
+
close(): void;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Factory function to create a Winston transport
|
|
52
|
+
*/
|
|
53
|
+
export declare function winstonTransport(opts?: WinstonTransportOptions): AbbacchioWinstonTransport;
|
|
54
|
+
export default winstonTransport;
|
|
55
|
+
//# sourceMappingURL=winston.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"winston.d.ts","sourceRoot":"","sources":["../../src/transports/winston.ts"],"names":[],"mappings":"AAAA,OAAO,eAAe,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAmB,KAAK,sBAAsB,EAAE,MAAM,cAAc,CAAC;AAE5E,MAAM,WAAW,uBAAwB,SAAQ,sBAAsB;IACrE,mCAAmC;IACnC,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,qBAAa,yBAA0B,SAAQ,eAAe;IAC5D,OAAO,CAAC,MAAM,CAAkB;gBAEpB,IAAI,GAAE,uBAA4B;IAK9C;;OAEG;IACH,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,QAAQ,EAAE,MAAM,IAAI,GAAG,IAAI;IAY9D;;OAEG;IACH,OAAO,CAAC,YAAY;IAWpB;;OAEG;IACH,OAAO,CAAC,aAAa;IAarB;;OAEG;IACH,KAAK,IAAI,IAAI;CAGd;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,CAAC,EAAE,uBAAuB,GAAG,yBAAyB,CAE1F;AAED,eAAe,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import TransportStream from "winston-transport";
|
|
2
|
+
import { AbbacchioClient } from "../client.js";
|
|
3
|
+
/**
|
|
4
|
+
* Winston transport for Abbacchio.
|
|
5
|
+
* Extends winston-transport for proper integration.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import winston from "winston";
|
|
10
|
+
* import { AbbacchioWinstonTransport } from "@abbacchio/client/transports/winston";
|
|
11
|
+
*
|
|
12
|
+
* const logger = winston.createLogger({
|
|
13
|
+
* transports: [
|
|
14
|
+
* new winston.transports.Console(),
|
|
15
|
+
* new AbbacchioWinstonTransport({
|
|
16
|
+
* url: "http://localhost:4000/api/logs",
|
|
17
|
+
* channel: "my-app",
|
|
18
|
+
* secretKey: "optional-encryption-key",
|
|
19
|
+
* }),
|
|
20
|
+
* ],
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* logger.info("Hello from Winston!");
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export class AbbacchioWinstonTransport extends TransportStream {
|
|
27
|
+
client;
|
|
28
|
+
constructor(opts = {}) {
|
|
29
|
+
super({ level: opts.level });
|
|
30
|
+
this.client = new AbbacchioClient(opts);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Winston log method - called for each log entry
|
|
34
|
+
*/
|
|
35
|
+
log(info, callback) {
|
|
36
|
+
setImmediate(() => {
|
|
37
|
+
this.emit("logged", info);
|
|
38
|
+
});
|
|
39
|
+
// Transform Winston format to Abbacchio format
|
|
40
|
+
const log = this.transformLog(info);
|
|
41
|
+
this.client.add(log);
|
|
42
|
+
callback();
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Transform Winston log format to a normalized format
|
|
46
|
+
*/
|
|
47
|
+
transformLog(info) {
|
|
48
|
+
const { level, message, timestamp, ...rest } = info;
|
|
49
|
+
return {
|
|
50
|
+
level: this.levelToNumber(level),
|
|
51
|
+
msg: message,
|
|
52
|
+
time: timestamp ? new Date(timestamp).getTime() : Date.now(),
|
|
53
|
+
...rest,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Convert Winston level string to Pino-style number
|
|
58
|
+
*/
|
|
59
|
+
levelToNumber(level) {
|
|
60
|
+
const levels = {
|
|
61
|
+
error: 50,
|
|
62
|
+
warn: 40,
|
|
63
|
+
info: 30,
|
|
64
|
+
http: 30,
|
|
65
|
+
verbose: 20,
|
|
66
|
+
debug: 20,
|
|
67
|
+
silly: 10,
|
|
68
|
+
};
|
|
69
|
+
return levels[level] || 30;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Close the transport
|
|
73
|
+
*/
|
|
74
|
+
close() {
|
|
75
|
+
this.client.close();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Factory function to create a Winston transport
|
|
80
|
+
*/
|
|
81
|
+
export function winstonTransport(opts) {
|
|
82
|
+
return new AbbacchioWinstonTransport(opts);
|
|
83
|
+
}
|
|
84
|
+
export default winstonTransport;
|
|
85
|
+
//# sourceMappingURL=winston.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"winston.js","sourceRoot":"","sources":["../../src/transports/winston.ts"],"names":[],"mappings":"AAAA,OAAO,eAAe,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,eAAe,EAA+B,MAAM,cAAc,CAAC;AAO5E;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,OAAO,yBAA0B,SAAQ,eAAe;IACpD,MAAM,CAAkB;IAEhC,YAAY,OAAgC,EAAE;QAC5C,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC;QAC7B,IAAI,CAAC,MAAM,GAAG,IAAI,eAAe,CAAC,IAAI,CAAC,CAAC;IAC1C,CAAC;IAED;;OAEG;IACH,GAAG,CAAC,IAA6B,EAAE,QAAoB;QACrD,YAAY,CAAC,GAAG,EAAE;YAChB,I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|
|
2
|
+
"name": "@abbacchio/transport",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Abbacchio transport - Log transports for Pino, Winston, Bunyan, and console",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./transports/pino": {
|
|
14
|
+
"types": "./dist/transports/pino.d.ts",
|
|
15
|
+
"default": "./dist/transports/pino.js"
|
|
16
|
+
},
|
|
17
|
+
"./transports/winston": {
|
|
18
|
+
"types": "./dist/transports/winston.d.ts",
|
|
19
|
+
"default": "./dist/transports/winston.js"
|
|
20
|
+
},
|
|
21
|
+
"./transports/bunyan": {
|
|
22
|
+
"types": "./dist/transports/bunyan.d.ts",
|
|
23
|
+
"default": "./dist/transports/bunyan.js"
|
|
24
|
+
},
|
|
25
|
+
"./transports/console": {
|
|
26
|
+
"types": "./dist/transports/console.d.ts",
|
|
27
|
+
"default": "./dist/transports/console.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsc",
|
|
32
|
+
"dev": "tsc --watch"
|
|
33
|
+
},
|
|
34
|
+
"keywords": [
|
|
35
|
+
"pino",
|
|
36
|
+
"winston",
|
|
37
|
+
"bunyan",
|
|
38
|
+
"logs",
|
|
39
|
+
"logging",
|
|
40
|
+
"transport",
|
|
41
|
+
"client"
|
|
42
|
+
],
|
|
43
|
+
"author": "",
|
|
44
|
+
"license": "MIT",
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"pino-abstract-transport": "^3.0.0",
|
|
47
|
+
"winston-transport": "^4.9.0"
|
|
48
|
+
},
|
|
49
|
+
"devDependencies": {
|
|
50
|
+
"@types/node": "^22.10.5",
|
|
51
|
+
"typescript": "^5.7.2"
|
|
52
|
+
},
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": ">=18"
|
|
55
|
+
}
|
|
56
|
+
}
|
package/src/client.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { encrypt } from "./encrypt.js";
|
|
2
|
+
|
|
3
|
+
export interface AbbacchioClientOptions {
|
|
4
|
+
/** Server URL endpoint */
|
|
5
|
+
url?: string;
|
|
6
|
+
/** Secret key for encryption. If provided, logs will be encrypted before sending */
|
|
7
|
+
secretKey?: string;
|
|
8
|
+
/** Channel/app name for multi-app support. Defaults to 'default' */
|
|
9
|
+
channel?: string;
|
|
10
|
+
/** Number of logs to batch before sending. Defaults to 10 */
|
|
11
|
+
batchSize?: number;
|
|
12
|
+
/** Interval in ms between flushes. Defaults to 1000 */
|
|
13
|
+
interval?: number;
|
|
14
|
+
/** Additional headers to send with requests */
|
|
15
|
+
headers?: Record<string, string>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Shared HTTP client for all Abbacchio transports.
|
|
20
|
+
* Handles batching, encryption, and HTTP communication.
|
|
21
|
+
*/
|
|
22
|
+
export class AbbacchioClient {
|
|
23
|
+
private url: string;
|
|
24
|
+
private secretKey?: string;
|
|
25
|
+
private channel?: string;
|
|
26
|
+
private batchSize: number;
|
|
27
|
+
private interval: number;
|
|
28
|
+
private headers: Record<string, string>;
|
|
29
|
+
|
|
30
|
+
private buffer: unknown[] = [];
|
|
31
|
+
private timer: ReturnType<typeof setTimeout> | null = null;
|
|
32
|
+
|
|
33
|
+
constructor(options: AbbacchioClientOptions = {}) {
|
|
34
|
+
this.url = options.url || "http://localhost:4000/api/logs";
|
|
35
|
+
this.secretKey = options.secretKey;
|
|
36
|
+
this.channel = options.channel;
|
|
37
|
+
this.batchSize = options.batchSize || 10;
|
|
38
|
+
this.interval = options.interval || 1000;
|
|
39
|
+
this.headers = options.headers || {};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Process a log entry (encrypt if secretKey is provided)
|
|
44
|
+
*/
|
|
45
|
+
private processLog(log: unknown): unknown {
|
|
46
|
+
if (this.secretKey) {
|
|
47
|
+
return { encrypted: encrypt(JSON.stringify(log), this.secretKey) };
|
|
48
|
+
}
|
|
49
|
+
return log;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Add a log to the buffer and trigger send if needed
|
|
54
|
+
*/
|
|
55
|
+
add(log: unknown): void {
|
|
56
|
+
this.buffer.push(this.processLog(log));
|
|
57
|
+
|
|
58
|
+
if (this.buffer.length >= this.batchSize) {
|
|
59
|
+
this.flush();
|
|
60
|
+
} else {
|
|
61
|
+
this.scheduleSend();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Add multiple logs at once
|
|
67
|
+
*/
|
|
68
|
+
addBatch(logs: unknown[]): void {
|
|
69
|
+
for (const log of logs) {
|
|
70
|
+
this.buffer.push(this.processLog(log));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (this.buffer.length >= this.batchSize) {
|
|
74
|
+
this.flush();
|
|
75
|
+
} else {
|
|
76
|
+
this.scheduleSend();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Send logs immediately without batching
|
|
82
|
+
*/
|
|
83
|
+
async send(logs: unknown[]): Promise<void> {
|
|
84
|
+
const processedLogs = logs.map(log => this.processLog(log));
|
|
85
|
+
await this.sendToServer(processedLogs);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Schedule a send after the interval
|
|
90
|
+
*/
|
|
91
|
+
private scheduleSend(): void {
|
|
92
|
+
if (this.timer) return;
|
|
93
|
+
this.timer = setTimeout(() => {
|
|
94
|
+
this.timer = null;
|
|
95
|
+
this.flush();
|
|
96
|
+
}, this.interval);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Flush the buffer and send to server
|
|
101
|
+
*/
|
|
102
|
+
async flush(): Promise<void> {
|
|
103
|
+
if (this.buffer.length === 0) return;
|
|
104
|
+
|
|
105
|
+
const toSend = this.buffer;
|
|
106
|
+
this.buffer = [];
|
|
107
|
+
|
|
108
|
+
await this.sendToServer(toSend);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Send logs to the Abbacchio server
|
|
113
|
+
*/
|
|
114
|
+
private async sendToServer(logs: unknown[]): Promise<void> {
|
|
115
|
+
try {
|
|
116
|
+
await fetch(this.url, {
|
|
117
|
+
method: "POST",
|
|
118
|
+
headers: {
|
|
119
|
+
"Content-Type": "application/json",
|
|
120
|
+
"X-Encrypted": this.secretKey ? "true" : "false",
|
|
121
|
+
...(this.channel ? { "X-Channel": this.channel } : {}),
|
|
122
|
+
...this.headers,
|
|
123
|
+
},
|
|
124
|
+
body: JSON.stringify({ logs }),
|
|
125
|
+
});
|
|
126
|
+
} catch {
|
|
127
|
+
// Silently fail - don't break the app if Abbacchio server is down
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Close the client and flush any remaining logs
|
|
133
|
+
*/
|
|
134
|
+
async close(): Promise<void> {
|
|
135
|
+
if (this.timer) {
|
|
136
|
+
clearTimeout(this.timer);
|
|
137
|
+
this.timer = null;
|
|
138
|
+
}
|
|
139
|
+
await this.flush();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Create a new Abbacchio client instance
|
|
145
|
+
*/
|
|
146
|
+
export function createClient(options?: AbbacchioClientOptions): AbbacchioClient {
|
|
147
|
+
return new AbbacchioClient(options);
|
|
148
|
+
}
|