@loglayer/transport-central 0.0.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.
- package/LICENSE +21 -0
- package/dist/index.cjs +79 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +62 -0
- package/dist/index.d.mts +62 -0
- package/dist/index.mjs +76 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Theo Gravity
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
let _loglayer_transport = require("@loglayer/transport");
|
|
3
|
+
let _loglayer_transport_http = require("@loglayer/transport-http");
|
|
4
|
+
|
|
5
|
+
//#region src/LogLayerCentralTransport.ts
|
|
6
|
+
/**
|
|
7
|
+
* LogLayer transport for sending logs to the Central log aggregation server.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
/** Default port for the Central log aggregation server */
|
|
11
|
+
const DEFAULT_PORT = 9800;
|
|
12
|
+
/** Default base URL for the Central log aggregation server */
|
|
13
|
+
const DEFAULT_BASE_URL = `http://localhost:${DEFAULT_PORT}`;
|
|
14
|
+
/** Extracts metadata from the data object (everything except context, err, and error keys). */
|
|
15
|
+
function extractMetadata(data) {
|
|
16
|
+
const { context, err, error, ...metadata } = data;
|
|
17
|
+
return Object.keys(metadata).length > 0 ? metadata : void 0;
|
|
18
|
+
}
|
|
19
|
+
/** Extracts the context object from the data object. */
|
|
20
|
+
function extractContext(data) {
|
|
21
|
+
const context = data.context;
|
|
22
|
+
if (context && typeof context === "object" && !Array.isArray(context)) return context;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* LogLayer transport that sends logs to the Central log aggregation server.
|
|
26
|
+
* Extends HttpTransport so batching, retries, compression, and rate-limiting
|
|
27
|
+
* work out of the box.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* import { LogLayer } from "loglayer";
|
|
32
|
+
* import { LogLayerCentralTransport } from "@loglayer/transport-central";
|
|
33
|
+
*
|
|
34
|
+
* const log = new LogLayer({
|
|
35
|
+
* transport: new LogLayerCentralTransport({
|
|
36
|
+
* baseUrl: "http://localhost:9800",
|
|
37
|
+
* service: "my-app",
|
|
38
|
+
* instanceId: "instance-1",
|
|
39
|
+
* }),
|
|
40
|
+
* });
|
|
41
|
+
*
|
|
42
|
+
* log.info("Application started");
|
|
43
|
+
* log.withMetadata({ userId: 123 }).error("User not found");
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
var LogLayerCentralTransport = class extends _loglayer_transport_http.HttpTransport {
|
|
47
|
+
constructor(config) {
|
|
48
|
+
const { baseUrl, service, instanceId, tags, ...httpOptions } = config;
|
|
49
|
+
super({
|
|
50
|
+
...httpOptions,
|
|
51
|
+
url: `${baseUrl ?? DEFAULT_BASE_URL}/api/logs`,
|
|
52
|
+
batchMode: "array",
|
|
53
|
+
payloadTemplate: ({ logLevel, message, data, hasData, error, groups }) => {
|
|
54
|
+
return JSON.stringify({
|
|
55
|
+
service,
|
|
56
|
+
message,
|
|
57
|
+
level: logLevel in _loglayer_transport.LogLevelPriority ? logLevel : _loglayer_transport.LogLevel.info,
|
|
58
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
59
|
+
instanceId,
|
|
60
|
+
metadata: hasData && data ? extractMetadata(data) : void 0,
|
|
61
|
+
context: hasData && data ? extractContext(data) : void 0,
|
|
62
|
+
error: error ? {
|
|
63
|
+
name: error.name,
|
|
64
|
+
message: error.message,
|
|
65
|
+
stack: error.stack
|
|
66
|
+
} : void 0,
|
|
67
|
+
groups: groups && groups.length > 0 ? groups : void 0,
|
|
68
|
+
tags: tags && tags.length > 0 ? tags : void 0
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
//#endregion
|
|
76
|
+
exports.DEFAULT_BASE_URL = DEFAULT_BASE_URL;
|
|
77
|
+
exports.DEFAULT_PORT = DEFAULT_PORT;
|
|
78
|
+
exports.LogLayerCentralTransport = LogLayerCentralTransport;
|
|
79
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["HttpTransport","LogLevelPriority","LogLevel"],"sources":["../src/LogLayerCentralTransport.ts"],"sourcesContent":["/**\n * LogLayer transport for sending logs to the Central log aggregation server.\n * @module\n */\n\nimport { LogLevel, LogLevelPriority } from \"@loglayer/transport\";\nimport { HttpTransport, type HttpTransportConfig } from \"@loglayer/transport-http\";\n\n/** Default port for the Central log aggregation server */\nexport const DEFAULT_PORT = 9800;\n\n/** Default base URL for the Central log aggregation server */\nexport const DEFAULT_BASE_URL = `http://localhost:${DEFAULT_PORT}`;\n\n/**\n * Configuration options for the Central transport.\n * All HttpTransport options (batching, retries, compression, etc.) are supported\n * via the `httpOptions` field.\n */\nexport interface LogLayerCentralTransportConfig\n extends Omit<HttpTransportConfig, \"url\" | \"payloadTemplate\" | \"batchMode\"> {\n /**\n * The base URL of the Central log aggregation server.\n * @default \"http://localhost:9800\"\n */\n baseUrl?: string;\n\n /**\n * The service name to attach to all logs.\n * This identifies the source application in the Central server.\n */\n service: string;\n\n /**\n * Optional instance ID to differentiate between multiple instances\n * of the same service.\n */\n instanceId?: string;\n\n /**\n * Static tags to attach to all logs sent through this transport.\n * Tags are key:value strings (e.g., [\\\"env:prod\\\", \\\"region:us-east\\\"]).\n */\n tags?: string[];\n}\n\n/** Extracts metadata from the data object (everything except context, err, and error keys). */\nfunction extractMetadata(data: Record<string, unknown>): Record<string, unknown> | undefined {\n const { context, err, error, ...metadata } = data;\n return Object.keys(metadata).length > 0 ? metadata : undefined;\n}\n\n/** Extracts the context object from the data object. */\nfunction extractContext(data: Record<string, unknown>): Record<string, unknown> | undefined {\n const context = data.context;\n if (context && typeof context === \"object\" && !Array.isArray(context)) {\n return context as Record<string, unknown>;\n }\n return undefined;\n}\n\n/**\n * LogLayer transport that sends logs to the Central log aggregation server.\n * Extends HttpTransport so batching, retries, compression, and rate-limiting\n * work out of the box.\n *\n * @example\n * ```typescript\n * import { LogLayer } from \"loglayer\";\n * import { LogLayerCentralTransport } from \"@loglayer/transport-central\";\n *\n * const log = new LogLayer({\n * transport: new LogLayerCentralTransport({\n * baseUrl: \"http://localhost:9800\",\n * service: \"my-app\",\n * instanceId: \"instance-1\",\n * }),\n * });\n *\n * log.info(\"Application started\");\n * log.withMetadata({ userId: 123 }).error(\"User not found\");\n * ```\n */\nexport class LogLayerCentralTransport extends HttpTransport {\n constructor(config: LogLayerCentralTransportConfig) {\n const { baseUrl, service, instanceId, tags, ...httpOptions } = config;\n\n super({\n ...httpOptions,\n url: `${baseUrl ?? DEFAULT_BASE_URL}/api/logs`,\n // Central's POST /api/logs accepts both a single object and a JSON array.\n batchMode: \"array\",\n payloadTemplate: ({ logLevel, message, data, hasData, error, groups }) => {\n return JSON.stringify({\n service,\n message,\n level: logLevel in LogLevelPriority ? logLevel : LogLevel.info,\n timestamp: new Date().toISOString(),\n instanceId,\n metadata: hasData && data ? extractMetadata(data) : undefined,\n context: hasData && data ? extractContext(data) : undefined,\n error: error ? { name: error.name, message: error.message, stack: error.stack } : undefined,\n groups: groups && groups.length > 0 ? groups : undefined,\n tags: tags && tags.length > 0 ? tags : undefined,\n });\n },\n });\n }\n}\n"],"mappings":";;;;;;;;;;AASA,MAAa,eAAe;;AAG5B,MAAa,mBAAmB,oBAAoB;;AAmCpD,SAAS,gBAAgB,MAAoE;CAC3F,MAAM,EAAE,SAAS,KAAK,OAAO,GAAG,aAAa;AAC7C,QAAO,OAAO,KAAK,SAAS,CAAC,SAAS,IAAI,WAAW;;;AAIvD,SAAS,eAAe,MAAoE;CAC1F,MAAM,UAAU,KAAK;AACrB,KAAI,WAAW,OAAO,YAAY,YAAY,CAAC,MAAM,QAAQ,QAAQ,CACnE,QAAO;;;;;;;;;;;;;;;;;;;;;;;;AA2BX,IAAa,2BAAb,cAA8CA,uCAAc;CAC1D,YAAY,QAAwC;EAClD,MAAM,EAAE,SAAS,SAAS,YAAY,MAAM,GAAG,gBAAgB;AAE/D,QAAM;GACJ,GAAG;GACH,KAAK,GAAG,WAAW,iBAAiB;GAEpC,WAAW;GACX,kBAAkB,EAAE,UAAU,SAAS,MAAM,SAAS,OAAO,aAAa;AACxE,WAAO,KAAK,UAAU;KACpB;KACA;KACA,OAAO,YAAYC,uCAAmB,WAAWC,6BAAS;KAC1D,4BAAW,IAAI,MAAM,EAAC,aAAa;KACnC;KACA,UAAU,WAAW,OAAO,gBAAgB,KAAK,GAAG;KACpD,SAAS,WAAW,OAAO,eAAe,KAAK,GAAG;KAClD,OAAO,QAAQ;MAAE,MAAM,MAAM;MAAM,SAAS,MAAM;MAAS,OAAO,MAAM;MAAO,GAAG;KAClF,QAAQ,UAAU,OAAO,SAAS,IAAI,SAAS;KAC/C,MAAM,QAAQ,KAAK,SAAS,IAAI,OAAO;KACxC,CAAC;;GAEL,CAAC"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { HttpTransport, HttpTransportConfig } from "@loglayer/transport-http";
|
|
2
|
+
|
|
3
|
+
//#region src/LogLayerCentralTransport.d.ts
|
|
4
|
+
/** Default port for the Central log aggregation server */
|
|
5
|
+
declare const DEFAULT_PORT = 9800;
|
|
6
|
+
/** Default base URL for the Central log aggregation server */
|
|
7
|
+
declare const DEFAULT_BASE_URL = "http://localhost:9800";
|
|
8
|
+
/**
|
|
9
|
+
* Configuration options for the Central transport.
|
|
10
|
+
* All HttpTransport options (batching, retries, compression, etc.) are supported
|
|
11
|
+
* via the `httpOptions` field.
|
|
12
|
+
*/
|
|
13
|
+
interface LogLayerCentralTransportConfig extends Omit<HttpTransportConfig, "url" | "payloadTemplate" | "batchMode"> {
|
|
14
|
+
/**
|
|
15
|
+
* The base URL of the Central log aggregation server.
|
|
16
|
+
* @default "http://localhost:9800"
|
|
17
|
+
*/
|
|
18
|
+
baseUrl?: string;
|
|
19
|
+
/**
|
|
20
|
+
* The service name to attach to all logs.
|
|
21
|
+
* This identifies the source application in the Central server.
|
|
22
|
+
*/
|
|
23
|
+
service: string;
|
|
24
|
+
/**
|
|
25
|
+
* Optional instance ID to differentiate between multiple instances
|
|
26
|
+
* of the same service.
|
|
27
|
+
*/
|
|
28
|
+
instanceId?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Static tags to attach to all logs sent through this transport.
|
|
31
|
+
* Tags are key:value strings (e.g., [\"env:prod\", \"region:us-east\"]).
|
|
32
|
+
*/
|
|
33
|
+
tags?: string[];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* LogLayer transport that sends logs to the Central log aggregation server.
|
|
37
|
+
* Extends HttpTransport so batching, retries, compression, and rate-limiting
|
|
38
|
+
* work out of the box.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* import { LogLayer } from "loglayer";
|
|
43
|
+
* import { LogLayerCentralTransport } from "@loglayer/transport-central";
|
|
44
|
+
*
|
|
45
|
+
* const log = new LogLayer({
|
|
46
|
+
* transport: new LogLayerCentralTransport({
|
|
47
|
+
* baseUrl: "http://localhost:9800",
|
|
48
|
+
* service: "my-app",
|
|
49
|
+
* instanceId: "instance-1",
|
|
50
|
+
* }),
|
|
51
|
+
* });
|
|
52
|
+
*
|
|
53
|
+
* log.info("Application started");
|
|
54
|
+
* log.withMetadata({ userId: 123 }).error("User not found");
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
declare class LogLayerCentralTransport extends HttpTransport {
|
|
58
|
+
constructor(config: LogLayerCentralTransportConfig);
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
export { DEFAULT_BASE_URL, DEFAULT_PORT, LogLayerCentralTransport, type LogLayerCentralTransportConfig };
|
|
62
|
+
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { HttpTransport, HttpTransportConfig } from "@loglayer/transport-http";
|
|
2
|
+
|
|
3
|
+
//#region src/LogLayerCentralTransport.d.ts
|
|
4
|
+
/** Default port for the Central log aggregation server */
|
|
5
|
+
declare const DEFAULT_PORT = 9800;
|
|
6
|
+
/** Default base URL for the Central log aggregation server */
|
|
7
|
+
declare const DEFAULT_BASE_URL = "http://localhost:9800";
|
|
8
|
+
/**
|
|
9
|
+
* Configuration options for the Central transport.
|
|
10
|
+
* All HttpTransport options (batching, retries, compression, etc.) are supported
|
|
11
|
+
* via the `httpOptions` field.
|
|
12
|
+
*/
|
|
13
|
+
interface LogLayerCentralTransportConfig extends Omit<HttpTransportConfig, "url" | "payloadTemplate" | "batchMode"> {
|
|
14
|
+
/**
|
|
15
|
+
* The base URL of the Central log aggregation server.
|
|
16
|
+
* @default "http://localhost:9800"
|
|
17
|
+
*/
|
|
18
|
+
baseUrl?: string;
|
|
19
|
+
/**
|
|
20
|
+
* The service name to attach to all logs.
|
|
21
|
+
* This identifies the source application in the Central server.
|
|
22
|
+
*/
|
|
23
|
+
service: string;
|
|
24
|
+
/**
|
|
25
|
+
* Optional instance ID to differentiate between multiple instances
|
|
26
|
+
* of the same service.
|
|
27
|
+
*/
|
|
28
|
+
instanceId?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Static tags to attach to all logs sent through this transport.
|
|
31
|
+
* Tags are key:value strings (e.g., [\"env:prod\", \"region:us-east\"]).
|
|
32
|
+
*/
|
|
33
|
+
tags?: string[];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* LogLayer transport that sends logs to the Central log aggregation server.
|
|
37
|
+
* Extends HttpTransport so batching, retries, compression, and rate-limiting
|
|
38
|
+
* work out of the box.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* import { LogLayer } from "loglayer";
|
|
43
|
+
* import { LogLayerCentralTransport } from "@loglayer/transport-central";
|
|
44
|
+
*
|
|
45
|
+
* const log = new LogLayer({
|
|
46
|
+
* transport: new LogLayerCentralTransport({
|
|
47
|
+
* baseUrl: "http://localhost:9800",
|
|
48
|
+
* service: "my-app",
|
|
49
|
+
* instanceId: "instance-1",
|
|
50
|
+
* }),
|
|
51
|
+
* });
|
|
52
|
+
*
|
|
53
|
+
* log.info("Application started");
|
|
54
|
+
* log.withMetadata({ userId: 123 }).error("User not found");
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
declare class LogLayerCentralTransport extends HttpTransport {
|
|
58
|
+
constructor(config: LogLayerCentralTransportConfig);
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
61
|
+
export { DEFAULT_BASE_URL, DEFAULT_PORT, LogLayerCentralTransport, type LogLayerCentralTransportConfig };
|
|
62
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { LogLevel, LogLevelPriority } from "@loglayer/transport";
|
|
2
|
+
import { HttpTransport } from "@loglayer/transport-http";
|
|
3
|
+
|
|
4
|
+
//#region src/LogLayerCentralTransport.ts
|
|
5
|
+
/**
|
|
6
|
+
* LogLayer transport for sending logs to the Central log aggregation server.
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
/** Default port for the Central log aggregation server */
|
|
10
|
+
const DEFAULT_PORT = 9800;
|
|
11
|
+
/** Default base URL for the Central log aggregation server */
|
|
12
|
+
const DEFAULT_BASE_URL = `http://localhost:${DEFAULT_PORT}`;
|
|
13
|
+
/** Extracts metadata from the data object (everything except context, err, and error keys). */
|
|
14
|
+
function extractMetadata(data) {
|
|
15
|
+
const { context, err, error, ...metadata } = data;
|
|
16
|
+
return Object.keys(metadata).length > 0 ? metadata : void 0;
|
|
17
|
+
}
|
|
18
|
+
/** Extracts the context object from the data object. */
|
|
19
|
+
function extractContext(data) {
|
|
20
|
+
const context = data.context;
|
|
21
|
+
if (context && typeof context === "object" && !Array.isArray(context)) return context;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* LogLayer transport that sends logs to the Central log aggregation server.
|
|
25
|
+
* Extends HttpTransport so batching, retries, compression, and rate-limiting
|
|
26
|
+
* work out of the box.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* import { LogLayer } from "loglayer";
|
|
31
|
+
* import { LogLayerCentralTransport } from "@loglayer/transport-central";
|
|
32
|
+
*
|
|
33
|
+
* const log = new LogLayer({
|
|
34
|
+
* transport: new LogLayerCentralTransport({
|
|
35
|
+
* baseUrl: "http://localhost:9800",
|
|
36
|
+
* service: "my-app",
|
|
37
|
+
* instanceId: "instance-1",
|
|
38
|
+
* }),
|
|
39
|
+
* });
|
|
40
|
+
*
|
|
41
|
+
* log.info("Application started");
|
|
42
|
+
* log.withMetadata({ userId: 123 }).error("User not found");
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
var LogLayerCentralTransport = class extends HttpTransport {
|
|
46
|
+
constructor(config) {
|
|
47
|
+
const { baseUrl, service, instanceId, tags, ...httpOptions } = config;
|
|
48
|
+
super({
|
|
49
|
+
...httpOptions,
|
|
50
|
+
url: `${baseUrl ?? DEFAULT_BASE_URL}/api/logs`,
|
|
51
|
+
batchMode: "array",
|
|
52
|
+
payloadTemplate: ({ logLevel, message, data, hasData, error, groups }) => {
|
|
53
|
+
return JSON.stringify({
|
|
54
|
+
service,
|
|
55
|
+
message,
|
|
56
|
+
level: logLevel in LogLevelPriority ? logLevel : LogLevel.info,
|
|
57
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
58
|
+
instanceId,
|
|
59
|
+
metadata: hasData && data ? extractMetadata(data) : void 0,
|
|
60
|
+
context: hasData && data ? extractContext(data) : void 0,
|
|
61
|
+
error: error ? {
|
|
62
|
+
name: error.name,
|
|
63
|
+
message: error.message,
|
|
64
|
+
stack: error.stack
|
|
65
|
+
} : void 0,
|
|
66
|
+
groups: groups && groups.length > 0 ? groups : void 0,
|
|
67
|
+
tags: tags && tags.length > 0 ? tags : void 0
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
//#endregion
|
|
75
|
+
export { DEFAULT_BASE_URL, DEFAULT_PORT, LogLayerCentralTransport };
|
|
76
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/LogLayerCentralTransport.ts"],"sourcesContent":["/**\n * LogLayer transport for sending logs to the Central log aggregation server.\n * @module\n */\n\nimport { LogLevel, LogLevelPriority } from \"@loglayer/transport\";\nimport { HttpTransport, type HttpTransportConfig } from \"@loglayer/transport-http\";\n\n/** Default port for the Central log aggregation server */\nexport const DEFAULT_PORT = 9800;\n\n/** Default base URL for the Central log aggregation server */\nexport const DEFAULT_BASE_URL = `http://localhost:${DEFAULT_PORT}`;\n\n/**\n * Configuration options for the Central transport.\n * All HttpTransport options (batching, retries, compression, etc.) are supported\n * via the `httpOptions` field.\n */\nexport interface LogLayerCentralTransportConfig\n extends Omit<HttpTransportConfig, \"url\" | \"payloadTemplate\" | \"batchMode\"> {\n /**\n * The base URL of the Central log aggregation server.\n * @default \"http://localhost:9800\"\n */\n baseUrl?: string;\n\n /**\n * The service name to attach to all logs.\n * This identifies the source application in the Central server.\n */\n service: string;\n\n /**\n * Optional instance ID to differentiate between multiple instances\n * of the same service.\n */\n instanceId?: string;\n\n /**\n * Static tags to attach to all logs sent through this transport.\n * Tags are key:value strings (e.g., [\\\"env:prod\\\", \\\"region:us-east\\\"]).\n */\n tags?: string[];\n}\n\n/** Extracts metadata from the data object (everything except context, err, and error keys). */\nfunction extractMetadata(data: Record<string, unknown>): Record<string, unknown> | undefined {\n const { context, err, error, ...metadata } = data;\n return Object.keys(metadata).length > 0 ? metadata : undefined;\n}\n\n/** Extracts the context object from the data object. */\nfunction extractContext(data: Record<string, unknown>): Record<string, unknown> | undefined {\n const context = data.context;\n if (context && typeof context === \"object\" && !Array.isArray(context)) {\n return context as Record<string, unknown>;\n }\n return undefined;\n}\n\n/**\n * LogLayer transport that sends logs to the Central log aggregation server.\n * Extends HttpTransport so batching, retries, compression, and rate-limiting\n * work out of the box.\n *\n * @example\n * ```typescript\n * import { LogLayer } from \"loglayer\";\n * import { LogLayerCentralTransport } from \"@loglayer/transport-central\";\n *\n * const log = new LogLayer({\n * transport: new LogLayerCentralTransport({\n * baseUrl: \"http://localhost:9800\",\n * service: \"my-app\",\n * instanceId: \"instance-1\",\n * }),\n * });\n *\n * log.info(\"Application started\");\n * log.withMetadata({ userId: 123 }).error(\"User not found\");\n * ```\n */\nexport class LogLayerCentralTransport extends HttpTransport {\n constructor(config: LogLayerCentralTransportConfig) {\n const { baseUrl, service, instanceId, tags, ...httpOptions } = config;\n\n super({\n ...httpOptions,\n url: `${baseUrl ?? DEFAULT_BASE_URL}/api/logs`,\n // Central's POST /api/logs accepts both a single object and a JSON array.\n batchMode: \"array\",\n payloadTemplate: ({ logLevel, message, data, hasData, error, groups }) => {\n return JSON.stringify({\n service,\n message,\n level: logLevel in LogLevelPriority ? logLevel : LogLevel.info,\n timestamp: new Date().toISOString(),\n instanceId,\n metadata: hasData && data ? extractMetadata(data) : undefined,\n context: hasData && data ? extractContext(data) : undefined,\n error: error ? { name: error.name, message: error.message, stack: error.stack } : undefined,\n groups: groups && groups.length > 0 ? groups : undefined,\n tags: tags && tags.length > 0 ? tags : undefined,\n });\n },\n });\n }\n}\n"],"mappings":";;;;;;;;;AASA,MAAa,eAAe;;AAG5B,MAAa,mBAAmB,oBAAoB;;AAmCpD,SAAS,gBAAgB,MAAoE;CAC3F,MAAM,EAAE,SAAS,KAAK,OAAO,GAAG,aAAa;AAC7C,QAAO,OAAO,KAAK,SAAS,CAAC,SAAS,IAAI,WAAW;;;AAIvD,SAAS,eAAe,MAAoE;CAC1F,MAAM,UAAU,KAAK;AACrB,KAAI,WAAW,OAAO,YAAY,YAAY,CAAC,MAAM,QAAQ,QAAQ,CACnE,QAAO;;;;;;;;;;;;;;;;;;;;;;;;AA2BX,IAAa,2BAAb,cAA8C,cAAc;CAC1D,YAAY,QAAwC;EAClD,MAAM,EAAE,SAAS,SAAS,YAAY,MAAM,GAAG,gBAAgB;AAE/D,QAAM;GACJ,GAAG;GACH,KAAK,GAAG,WAAW,iBAAiB;GAEpC,WAAW;GACX,kBAAkB,EAAE,UAAU,SAAS,MAAM,SAAS,OAAO,aAAa;AACxE,WAAO,KAAK,UAAU;KACpB;KACA;KACA,OAAO,YAAY,mBAAmB,WAAW,SAAS;KAC1D,4BAAW,IAAI,MAAM,EAAC,aAAa;KACnC;KACA,UAAU,WAAW,OAAO,gBAAgB,KAAK,GAAG;KACpD,SAAS,WAAW,OAAO,eAAe,KAAK,GAAG;KAClD,OAAO,QAAQ;MAAE,MAAM,MAAM;MAAM,SAAS,MAAM;MAAS,OAAO,MAAM;MAAO,GAAG;KAClF,QAAQ,UAAU,OAAO,SAAS,IAAI,SAAS;KAC/C,MAAM,QAAQ,KAAK,SAAS,IAAI,OAAO;KACxC,CAAC;;GAEL,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@loglayer/transport-central",
|
|
3
|
+
"description": "LogLayer Central transport for the LogLayer logging library.",
|
|
4
|
+
"version": "0.0.2",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.mjs",
|
|
8
|
+
"exports": {
|
|
9
|
+
"import": {
|
|
10
|
+
"types": "./dist/index.d.mts",
|
|
11
|
+
"import": "./dist/index.mjs"
|
|
12
|
+
},
|
|
13
|
+
"require": {
|
|
14
|
+
"types": "./dist/index.d.cts",
|
|
15
|
+
"require": "./dist/index.cjs"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"types": "./dist/index.d.mts",
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://github.com/loglayer/loglayer.git",
|
|
24
|
+
"directory": "packages/transports/central"
|
|
25
|
+
},
|
|
26
|
+
"author": "Theo Gravity <theo@suteki.nu>",
|
|
27
|
+
"keywords": [
|
|
28
|
+
"logging",
|
|
29
|
+
"log",
|
|
30
|
+
"loglayer",
|
|
31
|
+
"central",
|
|
32
|
+
"loglayer-central",
|
|
33
|
+
"transport"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@loglayer/transport": "3.0.2",
|
|
37
|
+
"@loglayer/transport-http": "2.1.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/node": "25.2.3",
|
|
41
|
+
"tsdown": "0.20.3",
|
|
42
|
+
"typescript": "5.9.3",
|
|
43
|
+
"vitest": "4.0.18",
|
|
44
|
+
"@internal/tsconfig": "2.1.0",
|
|
45
|
+
"loglayer": "9.1.0"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"loglayer": ">=9.1.0"
|
|
49
|
+
},
|
|
50
|
+
"bugs": "https://github.com/loglayer/loglayer/issues",
|
|
51
|
+
"engines": {
|
|
52
|
+
"node": ">=18"
|
|
53
|
+
},
|
|
54
|
+
"files": [
|
|
55
|
+
"dist"
|
|
56
|
+
],
|
|
57
|
+
"homepage": "https://loglayer.dev",
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "tsdown src/index.ts",
|
|
60
|
+
"test": "vitest --run",
|
|
61
|
+
"clean": "rm -rf .turbo node_modules dist",
|
|
62
|
+
"lint": "biome check --no-errors-on-unmatched --write --unsafe src",
|
|
63
|
+
"lint:staged": "biome check --no-errors-on-unmatched --write --unsafe --staged src",
|
|
64
|
+
"verify-types": "tsc --noEmit"
|
|
65
|
+
}
|
|
66
|
+
}
|