@logtape/adaptor-log4js 2.0.0-dev.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.
@@ -0,0 +1,232 @@
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+
23
+ //#endregion
24
+ const __logtape_logtape = __toESM(require("@logtape/logtape"));
25
+ const node_util = __toESM(require("node:util"));
26
+
27
+ //#region src/mod.ts
28
+ const DEFAULT_LEVELS_MAP = {
29
+ "trace": "trace",
30
+ "debug": "debug",
31
+ "info": "info",
32
+ "warning": "warn",
33
+ "error": "error",
34
+ "fatal": "fatal"
35
+ };
36
+ const DEFAULT_CATEGORY_MAPPER = (category) => category.length === 0 ? "" : category.join(".");
37
+ /**
38
+ * Creates a LogTape sink that forwards log records to a log4js logger.
39
+ *
40
+ * This function creates a sink function that can be used with LogTape's
41
+ * configuration system. The sink will format LogTape log records and
42
+ * forward them to the provided log4js logger instance or create one
43
+ * based on the LogTape category.
44
+ *
45
+ * @example Basic usage with default log4js logger
46
+ * ```typescript
47
+ * import log4js from "log4js";
48
+ * import { configure } from "@logtape/logtape";
49
+ * import { getLog4jsSink } from "@logtape/adaptor-log4js";
50
+ *
51
+ * log4js.configure({
52
+ * appenders: { out: { type: "stdout" } },
53
+ * categories: { default: { appenders: ["out"], level: "info" } }
54
+ * });
55
+ *
56
+ * await configure({
57
+ * sinks: {
58
+ * log4js: getLog4jsSink()
59
+ * },
60
+ * loggers: [
61
+ * { category: ["myapp"], sinks: ["log4js"] }
62
+ * ]
63
+ * });
64
+ * ```
65
+ *
66
+ * @example With custom log4js logger
67
+ * ```typescript
68
+ * import log4js from "log4js";
69
+ * import { getLog4jsSink } from "@logtape/adaptor-log4js";
70
+ *
71
+ * const logger = log4js.getLogger("custom");
72
+ * const sink = getLog4jsSink(logger);
73
+ * ```
74
+ *
75
+ * @example With custom options
76
+ * ```typescript
77
+ * const sink = getLog4jsSink(undefined, {
78
+ * categoryMapper: (cat) => cat.join("::"),
79
+ * contextStrategy: "args",
80
+ * levelsMap: {
81
+ * "trace": "debug",
82
+ * "debug": "debug",
83
+ * "info": "info",
84
+ * "warning": "warn",
85
+ * "error": "error",
86
+ * "fatal": "fatal"
87
+ * }
88
+ * });
89
+ * ```
90
+ *
91
+ * @param log4jsModule The log4js module instance. If not provided, log4js will be imported dynamically.
92
+ * @param logger The log4js logger instance to forward logs to. If not provided,
93
+ * a logger will be created for each LogTape category using log4js.getLogger().
94
+ * @param options Configuration options for the sink behavior.
95
+ * @returns A sink function that can be used with LogTape's configure() function.
96
+ * @since 2.0.0
97
+ */
98
+ function getLog4jsSink(log4jsModule, logger, options = {}) {
99
+ const { levelsMap = DEFAULT_LEVELS_MAP, categoryMapper = DEFAULT_CATEGORY_MAPPER, valueFormatter = node_util.inspect } = options;
100
+ const contextStrategy = options.contextStrategy ?? "mdc";
101
+ const contextPreservation = contextStrategy === "mdc" && "contextPreservation" in options ? options.contextPreservation ?? "preserve" : "preserve";
102
+ const loggerCache = /* @__PURE__ */ new Map();
103
+ const getLoggerForCategory = (category) => {
104
+ if (logger) return logger;
105
+ const categoryStr = categoryMapper(category);
106
+ if (loggerCache.has(categoryStr)) return loggerCache.get(categoryStr);
107
+ if (!log4jsModule) throw new Error("log4js module must be provided when not using a fixed logger");
108
+ const newLogger = categoryStr ? log4jsModule.getLogger(categoryStr) : log4jsModule.getLogger();
109
+ loggerCache.set(categoryStr, newLogger);
110
+ return newLogger;
111
+ };
112
+ return (record) => {
113
+ const targetLogger = getLoggerForCategory(record.category);
114
+ const level = levelsMap[record.level];
115
+ let message = "";
116
+ for (let i = 0; i < record.message.length; i += 2) {
117
+ message += record.message[i];
118
+ if (i + 1 < record.message.length) message += valueFormatter(record.message[i + 1]);
119
+ }
120
+ if (contextStrategy === "mdc") {
121
+ const propertiesToAdd = Object.entries(record.properties);
122
+ if (contextPreservation === "preserve") {
123
+ propertiesToAdd.forEach(([key, value]) => {
124
+ targetLogger.addContext(key, value);
125
+ });
126
+ targetLogger[level](message);
127
+ propertiesToAdd.forEach(([key]) => {
128
+ targetLogger.removeContext(key);
129
+ });
130
+ } else if (contextPreservation === "merge") {
131
+ propertiesToAdd.forEach(([key, value]) => {
132
+ targetLogger.addContext(key, value);
133
+ });
134
+ targetLogger[level](message);
135
+ } else {
136
+ targetLogger.clearContext();
137
+ propertiesToAdd.forEach(([key, value]) => {
138
+ targetLogger.addContext(key, value);
139
+ });
140
+ targetLogger[level](message);
141
+ }
142
+ } else targetLogger[level](message, record.properties);
143
+ };
144
+ }
145
+ /**
146
+ * Automatically configures LogTape to route all logs to log4js.
147
+ *
148
+ * This is a convenience function that automatically sets up LogTape to forward
149
+ * all log records to log4js. By default, it creates loggers based on LogTape
150
+ * categories using log4js.getLogger(), but you can provide a custom logger
151
+ * as the second parameter.
152
+ *
153
+ * @param log4jsModule The log4js module instance. If not provided, log4js will be imported dynamically.
154
+ * @param logger Optional log4js logger instance to use for all logs.
155
+ * @param options Configuration options for the log4js sink behavior.
156
+ *
157
+ * @example Basic auto-configuration
158
+ * ```typescript
159
+ * import log4js from "log4js";
160
+ * import { install } from "@logtape/adaptor-log4js";
161
+ *
162
+ * log4js.configure({
163
+ * appenders: { out: { type: "stdout" } },
164
+ * categories: { default: { appenders: ["out"], level: "info" } }
165
+ * });
166
+ *
167
+ * // Automatically route all LogTape logs to log4js
168
+ * install(log4js);
169
+ *
170
+ * // Now any LogTape-enabled library will log through log4js
171
+ * import { getLogger } from "@logtape/logtape";
172
+ * const logger = getLogger("my-app");
173
+ * logger.info("This will be logged through log4js");
174
+ * ```
175
+ *
176
+ * @example Auto-configuration with custom logger
177
+ * ```typescript
178
+ * import log4js from "log4js";
179
+ * import { install } from "@logtape/adaptor-log4js";
180
+ *
181
+ * const customLogger = log4js.getLogger("myapp");
182
+ *
183
+ * // Install with custom logger
184
+ * install(log4js, customLogger);
185
+ * ```
186
+ *
187
+ * @example Auto-configuration with custom options
188
+ * ```typescript
189
+ * import log4js from "log4js";
190
+ * import { install } from "@logtape/adaptor-log4js";
191
+ *
192
+ * install(log4js, undefined, {
193
+ * categoryMapper: (cat) => cat.join("::"),
194
+ * contextStrategy: "args"
195
+ * });
196
+ * ```
197
+ *
198
+ * @since 2.0.0
199
+ */
200
+ function install(log4jsModule, logger, options) {
201
+ (0, __logtape_logtape.configureSync)({
202
+ sinks: { log4js: getLog4jsSink(log4jsModule, logger, options) },
203
+ loggers: [{
204
+ category: ["logtape", "meta"],
205
+ sinks: ["log4js"],
206
+ lowestLevel: "warning"
207
+ }, {
208
+ category: [],
209
+ sinks: ["log4js"]
210
+ }]
211
+ });
212
+ }
213
+
214
+ //#endregion
215
+ Object.defineProperty(exports, '__toESM', {
216
+ enumerable: true,
217
+ get: function () {
218
+ return __toESM;
219
+ }
220
+ });
221
+ Object.defineProperty(exports, 'getLog4jsSink', {
222
+ enumerable: true,
223
+ get: function () {
224
+ return getLog4jsSink;
225
+ }
226
+ });
227
+ Object.defineProperty(exports, 'install', {
228
+ enumerable: true,
229
+ get: function () {
230
+ return install;
231
+ }
232
+ });
package/package.json ADDED
@@ -0,0 +1,79 @@
1
+ {
2
+ "name": "@logtape/adaptor-log4js",
3
+ "version": "2.0.0-dev.0",
4
+ "description": "log4js adapter for LogTape logging library",
5
+ "keywords": [
6
+ "logging",
7
+ "log",
8
+ "logger",
9
+ "log4js",
10
+ "adapter",
11
+ "logtape",
12
+ "sink"
13
+ ],
14
+ "license": "MIT",
15
+ "author": {
16
+ "name": "Hong Minhee",
17
+ "email": "hong@minhee.org",
18
+ "url": "https://hongminhee.org/"
19
+ },
20
+ "homepage": "https://logtape.org/",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "git+https://github.com/dahlia/logtape.git",
24
+ "directory": "packages/adaptor-log4js/"
25
+ },
26
+ "bugs": {
27
+ "url": "https://github.com/dahlia/logtape/issues"
28
+ },
29
+ "funding": [
30
+ "https://github.com/sponsors/dahlia"
31
+ ],
32
+ "type": "module",
33
+ "module": "./dist/mod.js",
34
+ "main": "./dist/mod.cjs",
35
+ "types": "./dist/mod.d.ts",
36
+ "exports": {
37
+ ".": {
38
+ "types": {
39
+ "import": "./dist/mod.d.ts",
40
+ "require": "./dist/mod.d.cts"
41
+ },
42
+ "import": "./dist/mod.js",
43
+ "require": "./dist/mod.cjs"
44
+ },
45
+ "./install": {
46
+ "types": {
47
+ "import": "./dist/install.d.ts",
48
+ "require": "./dist/install.d.cts"
49
+ },
50
+ "import": "./dist/install.js",
51
+ "require": "./dist/install.cjs"
52
+ },
53
+ "./package.json": "./package.json"
54
+ },
55
+ "sideEffects": false,
56
+ "files": [
57
+ "dist/"
58
+ ],
59
+ "peerDependencies": {
60
+ "log4js": "^6.0.0",
61
+ "@logtape/logtape": "^2.0.0"
62
+ },
63
+ "devDependencies": {
64
+ "@alinea/suite": "^0.6.3",
65
+ "@std/assert": "npm:@jsr/std__assert@^1.0.13",
66
+ "@std/async": "npm:@jsr/std__async@^1.0.13",
67
+ "log4js": "^6.9.1",
68
+ "tsdown": "^0.12.7",
69
+ "typescript": "^5.8.3"
70
+ },
71
+ "scripts": {
72
+ "build": "tsdown",
73
+ "prepublish": "tsdown",
74
+ "test": "tsdown && node --experimental-transform-types --test",
75
+ "test:bun": "tsdown && bun test",
76
+ "test:deno": "deno test --allow-env --allow-sys",
77
+ "test-all": "tsdown && node --experimental-transform-types --test && bun test && deno test"
78
+ }
79
+ }