@murky-web/simplelog 0.1.0 → 1.0.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/README.md +78 -34
- package/dist/logger_factory.d.ts +1 -0
- package/dist/logger_factory.js +6 -3
- package/dist/logger_factory.js.map +1 -1
- package/oxlint/index.mjs +26 -0
- package/oxlint/no-ad-hoc-console-fallback.mjs +65 -0
- package/oxlint/no-console-alias.mjs +69 -0
- package/oxlint/prefer-child-logger.mjs +77 -0
- package/oxlint/prefer-hono-context-logger.mjs +113 -0
- package/oxlint/prefer-runtime-entry.mjs +50 -0
- package/oxlint/prefer-simplelog.mjs +64 -0
- package/oxlint/require-logger-name.mjs +85 -0
- package/oxlint/utils.mjs +211 -0
- package/package.json +74 -68
package/README.md
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
1
|
# SimpleLog
|
|
2
2
|
|
|
3
|
-
`SimpleLog` is a lightweight and flexible logging utility for Node.js, Bun,
|
|
3
|
+
`SimpleLog` is a lightweight and flexible logging utility for Node.js, Bun,
|
|
4
|
+
Deno, and fetch-style edge runtimes. It provides various log levels, performance
|
|
5
|
+
benchmarking, and the ability to log messages to both the console and a file.
|
|
4
6
|
|
|
5
7
|
## Features
|
|
6
8
|
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
9
|
+
- Multiple log levels: `DEBUG`, `INFO`, `WARN`, `ERROR`
|
|
10
|
+
- Performance benchmarking
|
|
11
|
+
- Log messages to console and/or file
|
|
12
|
+
- Colorized console output
|
|
13
|
+
- Nested loggers for hierarchical logging
|
|
14
|
+
- Request-scoped logging for Hono servers
|
|
15
|
+
- Optional OpenTelemetry trace correlation
|
|
14
16
|
|
|
15
17
|
## Installation
|
|
16
18
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
```bash
|
|
20
|
+
bun add @murky-web/simplelog
|
|
21
|
+
```
|
|
19
22
|
|
|
20
23
|
## Usage
|
|
21
24
|
|
|
@@ -84,26 +87,34 @@ childLogger.info("This is a message from the child logger");
|
|
|
84
87
|
|
|
85
88
|
## Logger Options
|
|
86
89
|
|
|
87
|
-
-
|
|
88
|
-
-
|
|
89
|
-
-
|
|
90
|
-
-
|
|
91
|
-
-
|
|
90
|
+
- `writeToFile`: Boolean indicating whether to write logs to a file.
|
|
91
|
+
- `logFilePath`: Path to the log file.
|
|
92
|
+
- `logLevelThreshold`: Minimum log level for logging.
|
|
93
|
+
- `valueColorMode`: `"level" | "syntax" | "off"` for console value coloring.
|
|
94
|
+
- `includeOpenTelemetryContext`: Adds active `trace_id` and `span_id` to each
|
|
95
|
+
log line when an OpenTelemetry span is active.
|
|
92
96
|
|
|
93
97
|
`valueColorMode` modes:
|
|
94
98
|
|
|
95
99
|
- `level`: current default, all console pieces use the log-level color
|
|
96
|
-
- `syntax`: extra values use type-based colors like strings, numbers, booleans,
|
|
100
|
+
- `syntax`: extra values use type-based colors like strings, numbers, booleans,
|
|
101
|
+
nullish values, symbols, and objects
|
|
97
102
|
- `off`: console output stays plain
|
|
98
103
|
|
|
99
|
-
`includeOpenTelemetryContext` is intentionally opt-in. When enabled, `simplelog`
|
|
104
|
+
`includeOpenTelemetryContext` is intentionally opt-in. When enabled, `simplelog`
|
|
105
|
+
reads the current active span from `@opentelemetry/api` and appends `trace_id`
|
|
106
|
+
and `span_id` to console and file output. It does not install an OpenTelemetry
|
|
107
|
+
SDK or exporter for you.
|
|
100
108
|
|
|
101
109
|
## Log Levels
|
|
102
110
|
|
|
103
|
-
-
|
|
104
|
-
|
|
105
|
-
-
|
|
106
|
-
-
|
|
111
|
+
- `DEBUG`: Detailed information, typically of interest only when diagnosing
|
|
112
|
+
problems.
|
|
113
|
+
- `INFO`: Confirmation that things are working as expected.
|
|
114
|
+
- `WARN`: An indication that something unexpected happened, or indicative of
|
|
115
|
+
some problem in the near future.
|
|
116
|
+
- `ERROR`: Error events of considerable importance that will prevent normal
|
|
117
|
+
program execution.
|
|
107
118
|
|
|
108
119
|
## Runtime Variants
|
|
109
120
|
|
|
@@ -111,14 +122,18 @@ childLogger.info("This is a message from the child logger");
|
|
|
111
122
|
- `@murky-web/simplelog/node`: explicit Node entry
|
|
112
123
|
- `@murky-web/simplelog/bun`: Bun entry
|
|
113
124
|
- `@murky-web/simplelog/deno`: Deno entry
|
|
114
|
-
- `@murky-web/simplelog/web`: runtime-neutral console logger for fetch-style
|
|
125
|
+
- `@murky-web/simplelog/web`: runtime-neutral console logger for fetch-style
|
|
126
|
+
runtimes
|
|
115
127
|
- `@murky-web/simplelog/hono`: Hono middleware for request-scoped logging
|
|
116
128
|
|
|
117
|
-
For Bun, file appends intentionally use Bun's `node:fs` compatibility layer
|
|
129
|
+
For Bun, file appends intentionally use Bun's `node:fs` compatibility layer
|
|
130
|
+
because the native `Bun.file(...).writer()` flow overwrites existing file
|
|
131
|
+
contents instead of appending in our local runtime check.
|
|
118
132
|
|
|
119
133
|
## Hono Middleware
|
|
120
134
|
|
|
121
|
-
For Hono-based servers and metaframeworks, prefer the runtime-neutral `web`
|
|
135
|
+
For Hono-based servers and metaframeworks, prefer the runtime-neutral `web`
|
|
136
|
+
logger together with the Hono middleware entry:
|
|
122
137
|
|
|
123
138
|
```typescript
|
|
124
139
|
import { Hono } from "hono";
|
|
@@ -144,7 +159,8 @@ app.get("/", (c) => {
|
|
|
144
159
|
});
|
|
145
160
|
```
|
|
146
161
|
|
|
147
|
-
The middleware exposes `c.var.logger`, and it logs request completion with
|
|
162
|
+
The middleware exposes `c.var.logger`, and it logs request completion with
|
|
163
|
+
method, path, status, duration, request ID, and Hono's runtime key.
|
|
148
164
|
|
|
149
165
|
This adapter is intentionally thin:
|
|
150
166
|
|
|
@@ -153,7 +169,8 @@ This adapter is intentionally thin:
|
|
|
153
169
|
- it works across Node, Bun, Deno, Cloudflare, and Netlify style runtimes
|
|
154
170
|
- it does not own your tracing SDK or framework-specific SSR handoff
|
|
155
171
|
|
|
156
|
-
If you want a custom request logger name, you can provide one directly or
|
|
172
|
+
If you want a custom request logger name, you can provide one directly or
|
|
173
|
+
resolve it per request:
|
|
157
174
|
|
|
158
175
|
```typescript
|
|
159
176
|
app.use(
|
|
@@ -166,6 +183,23 @@ app.use(
|
|
|
166
183
|
);
|
|
167
184
|
```
|
|
168
185
|
|
|
186
|
+
## Oxlint Integration
|
|
187
|
+
|
|
188
|
+
`@murky-web/simplelog/oxlint` exports the SimpleLog-specific Oxlint JS plugin.
|
|
189
|
+
|
|
190
|
+
Current rules:
|
|
191
|
+
|
|
192
|
+
- `simplelog/prefer-simplelog`
|
|
193
|
+
- `simplelog/prefer-child-logger`
|
|
194
|
+
- `simplelog/no-ad-hoc-console-fallback`
|
|
195
|
+
- `simplelog/no-console-alias`
|
|
196
|
+
- `simplelog/prefer-hono-context-logger`
|
|
197
|
+
- `simplelog/prefer-runtime-entry`
|
|
198
|
+
- `simplelog/require-logger-name`
|
|
199
|
+
|
|
200
|
+
If you use `@murky-web/config`, running `web-dev-config init --oxc --simplelog`
|
|
201
|
+
wires this preset automatically.
|
|
202
|
+
|
|
169
203
|
## OpenTelemetry Integration
|
|
170
204
|
|
|
171
205
|
`simplelog` is OpenTelemetry-aware, but not OpenTelemetry-owning.
|
|
@@ -173,8 +207,10 @@ app.use(
|
|
|
173
207
|
That means:
|
|
174
208
|
|
|
175
209
|
- `simplelog` reads the active span from `@opentelemetry/api`
|
|
176
|
-
- `simplelog` appends `trace_id` and `span_id` when
|
|
177
|
-
|
|
210
|
+
- `simplelog` appends `trace_id` and `span_id` when
|
|
211
|
+
`includeOpenTelemetryContext` is enabled
|
|
212
|
+
- your application or metaframework is still responsible for installing the
|
|
213
|
+
OpenTelemetry SDK, context manager, instrumentations, and exporters
|
|
178
214
|
|
|
179
215
|
Minimal logger setup:
|
|
180
216
|
|
|
@@ -187,13 +223,17 @@ const logger = new Logger("server", undefined, {
|
|
|
187
223
|
});
|
|
188
224
|
```
|
|
189
225
|
|
|
190
|
-
Once your runtime has an active span, log lines automatically include the
|
|
226
|
+
Once your runtime has an active span, log lines automatically include the
|
|
227
|
+
current trace correlation data:
|
|
191
228
|
|
|
192
229
|
```text
|
|
193
230
|
2026-03-18T22:00:00.000Z [INFO] [server -> request] [trace_id=... span_id=...] - Request completed
|
|
194
231
|
```
|
|
195
232
|
|
|
196
|
-
`simplelog` deliberately does not bootstrap tracing for you. In production, that
|
|
233
|
+
`simplelog` deliberately does not bootstrap tracing for you. In production, that
|
|
234
|
+
usually belongs in the metaframework or host integration layer, because the
|
|
235
|
+
exact setup depends on your runtime, deployment target, and exporter
|
|
236
|
+
infrastructure.
|
|
197
237
|
|
|
198
238
|
## Hono + OpenTelemetry Together
|
|
199
239
|
|
|
@@ -201,16 +241,20 @@ For a Hono-based metaframework, the usual shape is:
|
|
|
201
241
|
|
|
202
242
|
1. Your framework boots OpenTelemetry for the current runtime.
|
|
203
243
|
2. Hono request handling creates or activates a request span.
|
|
204
|
-
3. `createHonoLoggerMiddleware()` attaches a request-scoped logger to
|
|
244
|
+
3. `createHonoLoggerMiddleware()` attaches a request-scoped logger to
|
|
245
|
+
`c.var.logger`.
|
|
205
246
|
4. `simplelog` reads the active span and appends `trace_id` and `span_id`.
|
|
206
247
|
|
|
207
248
|
That gives you:
|
|
208
249
|
|
|
209
250
|
- one consistent logger API on server and client
|
|
210
251
|
- request-scoped child loggers during SSR and server work
|
|
211
|
-
- trace-correlated logs that continue to work with real OpenTelemetry
|
|
252
|
+
- trace-correlated logs that continue to work with real OpenTelemetry
|
|
253
|
+
infrastructure later
|
|
212
254
|
|
|
213
|
-
For Node-only servers where file logging matters, you can still use the default
|
|
255
|
+
For Node-only servers where file logging matters, you can still use the default
|
|
256
|
+
`@murky-web/simplelog` entry. For truly cross-runtime Hono usage,
|
|
257
|
+
`@murky-web/simplelog/web` is the safer default.
|
|
214
258
|
|
|
215
259
|
## Example
|
|
216
260
|
|
|
@@ -225,7 +269,7 @@ const logger = new Logger("AppLogger", undefined, {
|
|
|
225
269
|
|
|
226
270
|
logger.info("Application started");
|
|
227
271
|
logger.debug(
|
|
228
|
-
"This debug message will not be logged because the threshold is INFO"
|
|
272
|
+
"This debug message will not be logged because the threshold is INFO",
|
|
229
273
|
);
|
|
230
274
|
logger.warn("This is a warning");
|
|
231
275
|
logger.error("This is an error");
|
package/dist/logger_factory.d.ts
CHANGED
package/dist/logger_factory.js
CHANGED
|
@@ -324,15 +324,18 @@ var LoggerBase = class {
|
|
|
324
324
|
log(level, message, args, stackTrace) {
|
|
325
325
|
if (!shouldLog(level, this.logLevelThreshold)) return;
|
|
326
326
|
const payload = createLogPayload(level, this.getFullLoggerName(), message, args, this.includeOpenTelemetryContext, this.valueColorMode, stackTrace);
|
|
327
|
-
|
|
328
|
-
this.runtime.console.log(payload.consoleLogMessage, ...payload.consoleArgs);
|
|
329
|
-
this.logStackTrace(payload.plainStackTrace, payload.consoleStackTrace);
|
|
327
|
+
this.emitPayload(payload);
|
|
330
328
|
}
|
|
331
329
|
logStackTrace(plainStackTrace, coloredStackTrace) {
|
|
332
330
|
if (plainStackTrace.length === EMPTY_FUNCTION_NAME_LENGTH) return;
|
|
333
331
|
writeLogLine(this.runtime, this.writeToFile, this.logFilePath, plainStackTrace);
|
|
334
332
|
this.runtime.console.error(coloredStackTrace);
|
|
335
333
|
}
|
|
334
|
+
emitPayload(payload) {
|
|
335
|
+
writeLogLine(this.runtime, this.writeToFile, this.logFilePath, payload.plainLogMessage);
|
|
336
|
+
this.runtime.console.log(payload.consoleLogMessage, ...payload.consoleArgs);
|
|
337
|
+
this.logStackTrace(payload.plainStackTrace, payload.consoleStackTrace);
|
|
338
|
+
}
|
|
336
339
|
mergeLoggers(otherLogger) {
|
|
337
340
|
return new this.LoggerConstructor(this.name, otherLogger, this.getInheritedOptions());
|
|
338
341
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger_factory.js","names":[],"sources":["../src/logger_factory.ts"],"sourcesContent":["import { trace } from \"@opentelemetry/api\";\n\nconst ANSI_RESET_COLOR = 0;\nconst ANSI_GREEN = 32;\nconst ANSI_YELLOW = 33;\nconst ANSI_RED = 31;\nconst ANSI_CYAN = 36;\nconst ANSI_BLUE = 34;\nconst ANSI_MAGENTA = 35;\nconst ANSI_GRAY = 90;\nconst EMPTY_FUNCTION_NAME_LENGTH = 0;\nconst JSON_INDENTATION = 2;\nconst STACK_TRACE_HEADER_LINE_COUNT = 2;\nconst ROOT_STACK_TRACE_INDEX = 0;\nconst DEFAULT_LOG_LEVEL = \"DEBUG\";\nconst DEFAULT_STACK_TRACE = \"\";\nconst FALLBACK_OBJECT_STRING = \"[Object]\";\nconst EMPTY_OPEN_TELEMETRY_CONTEXT: OpenTelemetryContext = {\n spanId: \"\",\n traceId: \"\",\n};\nconst CAPTURE_STACK_TRACE_MESSAGE = \"simplelog\";\nconst FUNCTION_LABEL_PREFIX = \"[Function \";\nconst FUNCTION_LABEL_SUFFIX = \"]\";\nconst ANONYMOUS_FUNCTION_LABEL = \"[Function anonymous]\";\nconst LOGGER_NAME_SEPARATOR = \" -> \";\nconst LOG_FILE_LINE_BREAK = \"\\n\";\nconst NODE_MODULES_SEGMENT = \"node_modules\";\n\ntype LogLevel = \"DEBUG\" | \"INFO\" | \"WARN\" | \"ERROR\";\n\ntype OpenTelemetryContext = Readonly<{\n spanId: string;\n traceId: string;\n}>;\n\ntype ValueColorMode = \"level\" | \"off\" | \"syntax\";\n\ntype LoggerOptions = Readonly<{\n includeOpenTelemetryContext?: boolean;\n logFilePath?: string;\n logLevelThreshold?: LogLevel;\n valueColorMode?: ValueColorMode;\n writeToFile?: boolean;\n}>;\n\ntype LoggerConsole = Readonly<{\n error: (...args: readonly unknown[]) => void;\n log: (...args: readonly unknown[]) => void;\n}>;\n\ntype LoggerRuntime = Readonly<{\n appendTextFileSync?: (filePath: string, content: string) => void;\n console: LoggerConsole;\n}>;\n\ntype LoggerParent = Readonly<{\n getFullLoggerName: () => string;\n getInheritedOptions: () => LoggerOptions;\n}>;\n\ntype LoggerConstructor = new (\n name: string,\n parent?: LoggerParent,\n options?: LoggerOptions,\n) => LoggerBase;\n\ntype ResolvedLoggerOptions = Readonly<{\n includeOpenTelemetryContext: boolean;\n logFilePath: string;\n logLevelThreshold: LogLevel;\n valueColorMode: ValueColorMode;\n writeToFile: boolean;\n}>;\n\ntype LogPayload = Readonly<{\n consoleArgs: readonly string[];\n consoleLogMessage: string;\n consoleStackTrace: string;\n plainLogMessage: string;\n plainStackTrace: string;\n}>;\n\ntype PlainLogParts = Readonly<{\n openTelemetryContext?: OpenTelemetryContext;\n plainArgs: readonly StringifiedArgument[];\n plainLogMessage: string;\n plainStackTrace: string;\n timestamp: string;\n}>;\n\ntype StringifiablePrimitive = bigint | boolean | number | string | symbol;\n\ntype StringifiedArgument = Readonly<{\n kind: StringifiedArgumentKind;\n text: string;\n}>;\n\ntype StringifiedArgumentKind =\n | \"boolean\"\n | \"function\"\n | \"nullish\"\n | \"number\"\n | \"object\"\n | \"string\"\n | \"symbol\";\n\nconst LOG_LEVEL_COLORS: Readonly<Record<LogLevel, number>> = {\n DEBUG: ANSI_CYAN,\n ERROR: ANSI_RED,\n INFO: ANSI_GREEN,\n WARN: ANSI_YELLOW,\n};\n\nconst LOG_LEVEL_PRIORITY: Readonly<Record<LogLevel, number>> = {\n DEBUG: 1,\n ERROR: 4,\n INFO: 2,\n WARN: 3,\n};\n\nfunction colorize(text: string, colorCode: number): string {\n return `\\u001B[${colorCode}m${text}\\u001B[${ANSI_RESET_COLOR}m`;\n}\n\nfunction createLoggerConsole(): LoggerConsole {\n return {\n error(...args: readonly unknown[]): void {\n globalThis.console.error(...args);\n },\n log(...args: readonly unknown[]): void {\n globalThis.console.log(...args);\n },\n };\n}\n\nfunction buildDefaultLogFilePath(loggerName: string): string {\n return `logs/${loggerName}.log`;\n}\n\nfunction resolveLoggerOptions(\n loggerName: string,\n options: LoggerOptions,\n): ResolvedLoggerOptions {\n return {\n includeOpenTelemetryContext: options.includeOpenTelemetryContext ?? false,\n logFilePath: options.logFilePath ?? buildDefaultLogFilePath(loggerName),\n logLevelThreshold: options.logLevelThreshold ?? DEFAULT_LOG_LEVEL,\n valueColorMode: options.valueColorMode ?? \"level\",\n writeToFile: options.writeToFile ?? false,\n };\n}\n\nfunction captureStackTrace(message = CAPTURE_STACK_TRACE_MESSAGE): string {\n return new Error(message).stack ?? DEFAULT_STACK_TRACE;\n}\n\nfunction minimizeStackTrace(stackTrace: string): string {\n const lines = stackTrace.split(LOG_FILE_LINE_BREAK);\n\n if (lines.length <= STACK_TRACE_HEADER_LINE_COUNT) {\n return stackTrace;\n }\n\n const filteredLines = lines.filter((line, index) => {\n if (index === ROOT_STACK_TRACE_INDEX) {\n return true;\n }\n\n return !line.includes(NODE_MODULES_SEGMENT);\n });\n\n return filteredLines.join(LOG_FILE_LINE_BREAK);\n}\n\nfunction isPrimitiveArgument(\n argument: unknown,\n): argument is StringifiablePrimitive {\n return (\n typeof argument === \"bigint\" ||\n typeof argument === \"boolean\" ||\n typeof argument === \"number\" ||\n typeof argument === \"string\" ||\n typeof argument === \"symbol\"\n );\n}\n\nfunction stringifyPrimitiveArgument(argument: StringifiablePrimitive): StringifiedArgument {\n if (typeof argument === \"string\") {\n return {\n kind: \"string\",\n text: argument,\n };\n }\n\n if (typeof argument === \"symbol\") {\n return {\n kind: \"symbol\",\n text: argument.toString(),\n };\n }\n\n if (typeof argument === \"boolean\") {\n return {\n kind: \"boolean\",\n text: `${argument}`,\n };\n }\n\n return {\n kind: \"number\",\n text: `${argument}`,\n };\n}\n\nfunction stringifyFunctionArgument(functionName: string): StringifiedArgument {\n if (functionName.length === EMPTY_FUNCTION_NAME_LENGTH) {\n return {\n kind: \"function\",\n text: ANONYMOUS_FUNCTION_LABEL,\n };\n }\n\n return {\n kind: \"function\",\n text: `${FUNCTION_LABEL_PREFIX}${functionName}${FUNCTION_LABEL_SUFFIX}`,\n };\n}\n\nfunction identityJsonReplacer(_key: string, value: unknown): unknown {\n return value;\n}\n\nfunction stringifyJsonArgument(argument: unknown): StringifiedArgument {\n try {\n const stringifiedArgument = JSON.stringify(\n argument,\n identityJsonReplacer,\n JSON_INDENTATION,\n );\n\n if (typeof stringifiedArgument !== \"string\") {\n return {\n kind: \"object\",\n text: FALLBACK_OBJECT_STRING,\n };\n }\n\n return {\n kind: \"object\",\n text: stringifiedArgument,\n };\n } catch {\n return {\n kind: \"object\",\n text: FALLBACK_OBJECT_STRING,\n };\n }\n}\n\nfunction stringifyArgument(argument: unknown): StringifiedArgument {\n if (typeof argument === \"undefined\") {\n return {\n kind: \"nullish\",\n text: \"undefined\",\n };\n }\n\n if (argument === null) {\n return {\n kind: \"nullish\",\n text: \"null\",\n };\n }\n\n if (isPrimitiveArgument(argument)) {\n return stringifyPrimitiveArgument(argument);\n }\n\n if (typeof argument === \"function\") {\n return stringifyFunctionArgument(argument.name);\n }\n\n return stringifyJsonArgument(argument);\n}\n\nfunction stringifyArguments(\n argumentsList: readonly unknown[],\n): readonly StringifiedArgument[] {\n return argumentsList.map((argument) => {\n return stringifyArgument(argument);\n });\n}\n\nfunction buildLogMessage(\n timestamp: string,\n levelLabel: string,\n loggerName: string,\n openTelemetryContextLabel: string,\n message: string,\n): string {\n return `${timestamp} ${levelLabel} [${loggerName}]${openTelemetryContextLabel} - ${message}`;\n}\n\nfunction resolveStackTrace(stackTrace?: string): string {\n if (typeof stackTrace === \"string\") {\n return minimizeStackTrace(stackTrace);\n }\n\n return DEFAULT_STACK_TRACE;\n}\n\nfunction buildColoredLogMessage(\n level: LogLevel,\n timestamp: string,\n loggerName: string,\n openTelemetryContextLabel: string,\n message: string,\n): string {\n const colorCode = LOG_LEVEL_COLORS[level];\n\n return buildLogMessage(\n colorize(timestamp, colorCode),\n colorize(`[${level}]`, colorCode),\n colorize(loggerName, colorCode),\n openTelemetryContextLabel,\n colorize(message, colorCode),\n );\n}\n\nfunction formatOpenTelemetryContextLabel(\n openTelemetryContext?: OpenTelemetryContext,\n): string {\n if (\n typeof openTelemetryContext !== \"object\" ||\n openTelemetryContext.spanId.length === EMPTY_FUNCTION_NAME_LENGTH ||\n openTelemetryContext.traceId.length === EMPTY_FUNCTION_NAME_LENGTH\n ) {\n return \"\";\n }\n\n return ` [trace_id=${openTelemetryContext.traceId} span_id=${openTelemetryContext.spanId}]`;\n}\n\nfunction colorizeOpenTelemetryContextLabel(\n level: LogLevel,\n openTelemetryContext: OpenTelemetryContext | undefined,\n valueColorMode: ValueColorMode,\n): string {\n const plainOpenTelemetryContextLabel = formatOpenTelemetryContextLabel(\n openTelemetryContext,\n );\n\n if (\n valueColorMode === \"off\" ||\n plainOpenTelemetryContextLabel.length === EMPTY_FUNCTION_NAME_LENGTH\n ) {\n return plainOpenTelemetryContextLabel;\n }\n\n if (valueColorMode === \"syntax\") {\n return colorize(plainOpenTelemetryContextLabel, ANSI_GRAY);\n }\n\n return colorize(plainOpenTelemetryContextLabel, LOG_LEVEL_COLORS[level]);\n}\n\nfunction getSyntaxColorCode(kind: StringifiedArgumentKind): number {\n switch (kind) {\n case \"boolean\": {\n return ANSI_YELLOW;\n }\n case \"function\": {\n return ANSI_MAGENTA;\n }\n case \"nullish\": {\n return ANSI_GRAY;\n }\n case \"number\": {\n return ANSI_CYAN;\n }\n case \"object\": {\n return ANSI_BLUE;\n }\n case \"string\": {\n return ANSI_GREEN;\n }\n case \"symbol\": {\n return ANSI_MAGENTA;\n }\n default: {\n return ANSI_GREEN;\n }\n }\n}\n\nfunction colorizeArguments(\n argumentsList: readonly StringifiedArgument[],\n level: LogLevel,\n valueColorMode: ValueColorMode,\n): readonly string[] {\n if (valueColorMode === \"off\") {\n return argumentsList.map((argument) => {\n return argument.text;\n });\n }\n\n if (valueColorMode === \"syntax\") {\n return argumentsList.map((argument) => {\n return colorize(argument.text, getSyntaxColorCode(argument.kind));\n });\n }\n\n const colorCode = LOG_LEVEL_COLORS[level];\n\n return argumentsList.map((argument) => {\n return colorize(argument.text, colorCode);\n });\n}\n\nfunction buildPlainConsoleLogMessage(\n level: LogLevel,\n timestamp: string,\n loggerName: string,\n openTelemetryContext: OpenTelemetryContext | undefined,\n message: string,\n): string {\n return buildLogMessage(\n timestamp,\n `[${level}]`,\n loggerName,\n formatOpenTelemetryContextLabel(openTelemetryContext),\n message,\n );\n}\n\nfunction buildConsoleLogMessage(\n level: LogLevel,\n timestamp: string,\n loggerName: string,\n openTelemetryContext: OpenTelemetryContext | undefined,\n message: string,\n valueColorMode: ValueColorMode,\n): string {\n if (valueColorMode === \"off\") {\n return buildPlainConsoleLogMessage(\n level,\n timestamp,\n loggerName,\n openTelemetryContext,\n message,\n );\n }\n\n return buildColoredLogMessage(\n level,\n timestamp,\n loggerName,\n colorizeOpenTelemetryContextLabel(\n level,\n openTelemetryContext,\n valueColorMode,\n ),\n message,\n );\n}\n\nfunction buildConsoleStackTrace(\n stackTrace: string,\n level: LogLevel,\n valueColorMode: ValueColorMode,\n): string {\n if (valueColorMode === \"off\") {\n return stackTrace;\n }\n\n return colorize(stackTrace, LOG_LEVEL_COLORS[level]);\n}\n\nfunction createConsolePayload(\n level: LogLevel,\n timestamp: string,\n loggerName: string,\n openTelemetryContext: OpenTelemetryContext | undefined,\n message: string,\n plainArgs: readonly StringifiedArgument[],\n plainStackTrace: string,\n valueColorMode: ValueColorMode,\n): Pick<LogPayload, \"consoleArgs\" | \"consoleLogMessage\" | \"consoleStackTrace\"> {\n return {\n consoleArgs: colorizeArguments(plainArgs, level, valueColorMode),\n consoleLogMessage: buildConsoleLogMessage(\n level,\n timestamp,\n loggerName,\n openTelemetryContext,\n message,\n valueColorMode,\n ),\n consoleStackTrace: buildConsoleStackTrace(\n plainStackTrace,\n level,\n valueColorMode,\n ),\n };\n}\n\nfunction createPlainLogParts(\n level: LogLevel,\n loggerName: string,\n openTelemetryContext: OpenTelemetryContext | undefined,\n message: string,\n args: readonly unknown[],\n stackTrace?: string,\n): PlainLogParts {\n const plainStackTrace = resolveStackTrace(stackTrace);\n const timestamp = new Date().toISOString();\n\n return {\n openTelemetryContext,\n plainArgs: stringifyArguments(args),\n plainLogMessage: buildLogMessage(\n timestamp,\n `[${level}]`,\n loggerName,\n formatOpenTelemetryContextLabel(openTelemetryContext),\n message,\n ),\n plainStackTrace,\n timestamp,\n };\n}\n\nfunction resolveOpenTelemetryContext(\n includeOpenTelemetryContext: boolean,\n): OpenTelemetryContext {\n if (!includeOpenTelemetryContext) {\n return EMPTY_OPEN_TELEMETRY_CONTEXT;\n }\n\n const activeSpan = trace.getActiveSpan();\n\n if (typeof activeSpan === \"undefined\") {\n return EMPTY_OPEN_TELEMETRY_CONTEXT;\n }\n\n const spanContext = activeSpan.spanContext();\n\n return {\n spanId: spanContext.spanId,\n traceId: spanContext.traceId,\n };\n}\n\nfunction combineLogPayload(\n consolePayload: Pick<\n LogPayload,\n \"consoleArgs\" | \"consoleLogMessage\" | \"consoleStackTrace\"\n >,\n plainLogParts: PlainLogParts,\n): LogPayload {\n return {\n ...consolePayload,\n plainLogMessage: plainLogParts.plainLogMessage,\n plainStackTrace: plainLogParts.plainStackTrace,\n };\n}\n\nfunction createConsolePayloadFromPlainLogParts(\n level: LogLevel,\n loggerName: string,\n message: string,\n plainLogParts: PlainLogParts,\n valueColorMode: ValueColorMode,\n): Pick<LogPayload, \"consoleArgs\" | \"consoleLogMessage\" | \"consoleStackTrace\"> {\n return createConsolePayload(\n level,\n plainLogParts.timestamp,\n loggerName,\n plainLogParts.openTelemetryContext,\n message,\n plainLogParts.plainArgs,\n plainLogParts.plainStackTrace,\n valueColorMode,\n );\n}\n\nfunction createLogPayload(\n level: LogLevel,\n loggerName: string,\n message: string,\n args: readonly unknown[],\n includeOpenTelemetryContext: boolean,\n valueColorMode: ValueColorMode,\n stackTrace?: string,\n): LogPayload {\n const openTelemetryContext = resolveOpenTelemetryContext(\n includeOpenTelemetryContext,\n );\n const plainLogParts = createPlainLogParts(\n level,\n loggerName,\n openTelemetryContext,\n message,\n args,\n stackTrace,\n );\n const consolePayload = createConsolePayloadFromPlainLogParts(\n level,\n loggerName,\n message,\n plainLogParts,\n valueColorMode,\n );\n\n return combineLogPayload(consolePayload, plainLogParts);\n}\n\nfunction writeLogLine(\n runtime: LoggerRuntime,\n writeToFile: boolean,\n logFilePath: string,\n logMessage: string,\n): void {\n if (!writeToFile) {\n return;\n }\n\n const { appendTextFileSync } = runtime;\n\n if (typeof appendTextFileSync !== \"function\") {\n return;\n }\n\n appendTextFileSync(logFilePath, `${logMessage}${LOG_FILE_LINE_BREAK}`);\n}\n\nfunction shouldLog(level: LogLevel, threshold: LogLevel): boolean {\n return LOG_LEVEL_PRIORITY[level] >= LOG_LEVEL_PRIORITY[threshold];\n}\n\nclass LoggerBase {\n private readonly LoggerConstructor: LoggerConstructor;\n private readonly logFilePath: string;\n private readonly logLevelThreshold: LogLevel;\n private readonly name: string;\n private readonly parent?: LoggerParent;\n private readonly performanceBenchmarks = new Map<string, number>();\n private readonly runtime: LoggerRuntime;\n private readonly includeOpenTelemetryContext: boolean;\n private readonly valueColorMode: ValueColorMode;\n private readonly writeToFile: boolean;\n\n protected constructor(\n LoggerConstructor: LoggerConstructor,\n runtime: LoggerRuntime,\n name: string,\n parent?: LoggerParent,\n options: LoggerOptions = {},\n ) {\n const resolvedOptions = resolveLoggerOptions(name, options);\n\n this.LoggerConstructor = LoggerConstructor;\n this.includeOpenTelemetryContext = resolvedOptions.includeOpenTelemetryContext;\n this.logFilePath = resolvedOptions.logFilePath;\n this.logLevelThreshold = resolvedOptions.logLevelThreshold;\n this.name = name;\n this.parent = parent;\n this.runtime = runtime;\n this.valueColorMode = resolvedOptions.valueColorMode;\n this.writeToFile = resolvedOptions.writeToFile;\n }\n\n public assert(\n condition: boolean,\n message: string,\n ...args: readonly unknown[]\n ): void {\n if (condition) {\n return;\n }\n\n const assertionMessage = `Assertion failed: ${message}`;\n\n this.log(\"ERROR\", assertionMessage, args, captureStackTrace(assertionMessage));\n }\n\n public createChildLogger(\n name: string,\n otherLogger?: LoggerParent,\n ): LoggerBase {\n if (typeof otherLogger === \"object\") {\n return new this.LoggerConstructor(\n name,\n this.mergeLoggers(otherLogger),\n this.getInheritedOptions(),\n );\n }\n\n return new this.LoggerConstructor(name, this, this.getInheritedOptions());\n }\n\n public debug(message: string, ...args: readonly unknown[]): void {\n this.log(\"DEBUG\", message, args);\n }\n\n public endPerformanceBenchmark(label: string, message: string): void {\n const startTime = this.performanceBenchmarks.get(label);\n\n if (typeof startTime !== \"number\") {\n this.warn(\n `No start time found for performance test with label \"${label}\"`,\n );\n return;\n }\n\n const elapsedMilliseconds = Date.now() - startTime;\n\n this.debug(`${message} - Elapsed Time: ${elapsedMilliseconds}ms`);\n this.performanceBenchmarks.delete(label);\n }\n\n public error(message: string, ...args: readonly unknown[]): void {\n this.log(\"ERROR\", message, args, captureStackTrace(message));\n }\n\n public getFullLoggerName(): string {\n if (typeof this.parent === \"object\") {\n return `${this.parent.getFullLoggerName()}${LOGGER_NAME_SEPARATOR}${this.name}`;\n }\n\n return this.name;\n }\n\n public getInheritedOptions(): LoggerOptions {\n return {\n includeOpenTelemetryContext: this.includeOpenTelemetryContext,\n logFilePath: this.logFilePath,\n logLevelThreshold: this.logLevelThreshold,\n valueColorMode: this.valueColorMode,\n writeToFile: this.writeToFile,\n };\n }\n\n public info(message: string, ...args: readonly unknown[]): void {\n this.log(\"INFO\", message, args);\n }\n\n public startPerformanceBenchmark(label: string): void {\n this.performanceBenchmarks.set(label, Date.now());\n }\n\n public warn(message: string, ...args: readonly unknown[]): void {\n this.log(\"WARN\", message, args);\n }\n\n private log(\n level: LogLevel,\n message: string,\n args: readonly unknown[],\n stackTrace?: string,\n ): void {\n if (!shouldLog(level, this.logLevelThreshold)) {\n return;\n }\n\n const payload = createLogPayload(\n level,\n this.getFullLoggerName(),\n message,\n args,\n this.includeOpenTelemetryContext,\n this.valueColorMode,\n stackTrace,\n );\n\n writeLogLine(\n this.runtime,\n this.writeToFile,\n this.logFilePath,\n payload.plainLogMessage,\n );\n this.runtime.console.log(payload.consoleLogMessage, ...payload.consoleArgs);\n this.logStackTrace(payload.plainStackTrace, payload.consoleStackTrace);\n }\n\n private logStackTrace(\n plainStackTrace: string,\n coloredStackTrace: string,\n ): void {\n if (plainStackTrace.length === EMPTY_FUNCTION_NAME_LENGTH) {\n return;\n }\n\n writeLogLine(\n this.runtime,\n this.writeToFile,\n this.logFilePath,\n plainStackTrace,\n );\n this.runtime.console.error(coloredStackTrace);\n }\n\n private mergeLoggers(otherLogger: LoggerParent): LoggerParent {\n return new this.LoggerConstructor(\n this.name,\n otherLogger,\n this.getInheritedOptions(),\n );\n }\n}\n\nexport { LoggerBase, createLoggerConsole };\nexport type {\n LogLevel,\n LoggerConsole,\n LoggerConstructor,\n LoggerOptions,\n LoggerParent,\n LoggerRuntime,\n ValueColorMode,\n};\n"],"mappings":";;AAEA,MAAM,mBAAmB;AACzB,MAAM,aAAa;AACnB,MAAM,cAAc;AACpB,MAAM,WAAW;AACjB,MAAM,YAAY;AAClB,MAAM,YAAY;AAClB,MAAM,eAAe;AACrB,MAAM,YAAY;AAClB,MAAM,6BAA6B;AACnC,MAAM,mBAAmB;AACzB,MAAM,gCAAgC;AACtC,MAAM,yBAAyB;AAC/B,MAAM,oBAAoB;AAC1B,MAAM,sBAAsB;AAC5B,MAAM,yBAAyB;AAC/B,MAAM,+BAAqD;CACzD,QAAQ;CACR,SAAS;CACV;AACD,MAAM,8BAA8B;AACpC,MAAM,wBAAwB;AAC9B,MAAM,wBAAwB;AAC9B,MAAM,2BAA2B;AACjC,MAAM,wBAAwB;AAC9B,MAAM,sBAAsB;AAC5B,MAAM,uBAAuB;AAgF7B,MAAM,mBAAuD;CAC3D,OAAO;CACP,OAAO;CACP,MAAM;CACN,MAAM;CACP;AAED,MAAM,qBAAyD;CAC7D,OAAO;CACP,OAAO;CACP,MAAM;CACN,MAAM;CACP;AAED,SAAS,SAAS,MAAc,WAA2B;AACzD,QAAO,UAAU,UAAU,GAAG,KAAK,SAAS,iBAAiB;;AAG/D,S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GAAG,MAAgC;AAC/D,OAAK,IAAI,SAAS,SAAS,MAAM,kBAAkB,QAAQ,CAAC;;CAG9D,oBAAmC;AACjC,MAAI,OAAO,KAAK,WAAW,SACzB,QAAO,GAAG,KAAK,OAAO,mBAAmB,GAAG,wBAAwB,KAAK;AAG3E,SAAO,KAAK;;CAGd,sBAA4C;AAC1C,SAAO;GACL,6BAA6B,KAAK;GAClC,aAAa,KAAK;GAClB,mBAAmB,KAAK;GACxB,gBAAgB,KAAK;GACrB,aAAa,KAAK;GACnB;;CAGH,KAAY,SAAiB,GAAG,MAAgC;AAC9D,OAAK,IAAI,QAAQ,SAAS,KAAK;;CAGjC,0BAAiC,OAAqB;AACpD,OAAK,sBAAsB,IAAI,OAAO,KAAK,KAAK,CAAC;;CAGnD,KAAY,SAAiB,GAAG,MAAgC;AAC9D,OAAK,IAAI,QAAQ,SAAS,KAAK;;CAGjC,IACE,OACA,SACA,MACA,YACM;AACN,MAAI,CAAC,UAAU,OAAO,KAAK,kBAAkB,CAC3C;EAGF,MAAM,UAAU,iBACd,OACA,KAAK,mBAAmB,EACxB,SACA,MACA,KAAK,6BACL,KAAK,gBACL,WACD;AAED,eACE,KAAK,SACL,KAAK,aACL,KAAK,aACL,QAAQ,gBACT;AACD,OAAK,QAAQ,QAAQ,IAAI,QAAQ,mBAAmB,GAAG,QAAQ,YAAY;AAC3E,OAAK,cAAc,QAAQ,iBAAiB,QAAQ,kBAAkB;;CAGxE,cACE,iBACA,mBACM;AACN,MAAI,gBAAgB,WAAW,2BAC7B;AAGF,eACE,KAAK,SACL,KAAK,aACL,KAAK,aACL,gBACD;AACD,OAAK,QAAQ,QAAQ,MAAM,kBAAkB;;CAG/C,aAAqB,aAAyC;AAC5D,SAAO,IAAI,KAAK,kBACd,KAAK,MACL,aACA,KAAK,qBAAqB,CAC3B"}
|
|
1
|
+
{"version":3,"file":"logger_factory.js","names":[],"sources":["../src/logger_factory.ts"],"sourcesContent":["import { trace } from \"@opentelemetry/api\";\n\nconst ANSI_RESET_COLOR = 0;\nconst ANSI_GREEN = 32;\nconst ANSI_YELLOW = 33;\nconst ANSI_RED = 31;\nconst ANSI_CYAN = 36;\nconst ANSI_BLUE = 34;\nconst ANSI_MAGENTA = 35;\nconst ANSI_GRAY = 90;\nconst EMPTY_FUNCTION_NAME_LENGTH = 0;\nconst JSON_INDENTATION = 2;\nconst STACK_TRACE_HEADER_LINE_COUNT = 2;\nconst ROOT_STACK_TRACE_INDEX = 0;\nconst DEFAULT_LOG_LEVEL = \"DEBUG\";\nconst DEFAULT_STACK_TRACE = \"\";\nconst FALLBACK_OBJECT_STRING = \"[Object]\";\nconst EMPTY_OPEN_TELEMETRY_CONTEXT: OpenTelemetryContext = {\n spanId: \"\",\n traceId: \"\",\n};\nconst CAPTURE_STACK_TRACE_MESSAGE = \"simplelog\";\nconst FUNCTION_LABEL_PREFIX = \"[Function \";\nconst FUNCTION_LABEL_SUFFIX = \"]\";\nconst ANONYMOUS_FUNCTION_LABEL = \"[Function anonymous]\";\nconst LOGGER_NAME_SEPARATOR = \" -> \";\nconst LOG_FILE_LINE_BREAK = \"\\n\";\nconst NODE_MODULES_SEGMENT = \"node_modules\";\n\ntype LogLevel = \"DEBUG\" | \"INFO\" | \"WARN\" | \"ERROR\";\n\ntype OpenTelemetryContext = Readonly<{\n spanId: string;\n traceId: string;\n}>;\n\ntype ValueColorMode = \"level\" | \"off\" | \"syntax\";\n\ntype LoggerOptions = Readonly<{\n includeOpenTelemetryContext?: boolean;\n logFilePath?: string;\n logLevelThreshold?: LogLevel;\n valueColorMode?: ValueColorMode;\n writeToFile?: boolean;\n}>;\n\ntype LoggerConsole = Readonly<{\n error: (...args: readonly unknown[]) => void;\n log: (...args: readonly unknown[]) => void;\n}>;\n\ntype LoggerRuntime = Readonly<{\n appendTextFileSync?: (filePath: string, content: string) => void;\n console: LoggerConsole;\n}>;\n\ntype LoggerParent = Readonly<{\n getFullLoggerName: () => string;\n getInheritedOptions: () => LoggerOptions;\n}>;\n\ntype LoggerConstructor = new (\n name: string,\n parent?: LoggerParent,\n options?: LoggerOptions,\n) => LoggerBase;\n\ntype ResolvedLoggerOptions = Readonly<{\n includeOpenTelemetryContext: boolean;\n logFilePath: string;\n logLevelThreshold: LogLevel;\n valueColorMode: ValueColorMode;\n writeToFile: boolean;\n}>;\n\ntype LogPayload = Readonly<{\n consoleArgs: readonly string[];\n consoleLogMessage: string;\n consoleStackTrace: string;\n plainLogMessage: string;\n plainStackTrace: string;\n}>;\n\ntype PlainLogParts = Readonly<{\n openTelemetryContext?: OpenTelemetryContext;\n plainArgs: readonly StringifiedArgument[];\n plainLogMessage: string;\n plainStackTrace: string;\n timestamp: string;\n}>;\n\ntype StringifiablePrimitive = bigint | boolean | number | string | symbol;\n\ntype StringifiedArgument = Readonly<{\n kind: StringifiedArgumentKind;\n text: string;\n}>;\n\ntype StringifiedArgumentKind =\n | \"boolean\"\n | \"function\"\n | \"nullish\"\n | \"number\"\n | \"object\"\n | \"string\"\n | \"symbol\";\n\nconst LOG_LEVEL_COLORS: Readonly<Record<LogLevel, number>> = {\n DEBUG: ANSI_CYAN,\n ERROR: ANSI_RED,\n INFO: ANSI_GREEN,\n WARN: ANSI_YELLOW,\n};\n\nconst LOG_LEVEL_PRIORITY: Readonly<Record<LogLevel, number>> = {\n DEBUG: 1,\n ERROR: 4,\n INFO: 2,\n WARN: 3,\n};\n\nfunction colorize(text: string, colorCode: number): string {\n return `\\u001B[${colorCode}m${text}\\u001B[${ANSI_RESET_COLOR}m`;\n}\n\nfunction createLoggerConsole(): LoggerConsole {\n return {\n error(...args: readonly unknown[]): void {\n globalThis.console.error(...args);\n },\n log(...args: readonly unknown[]): void {\n globalThis.console.log(...args);\n },\n };\n}\n\nfunction buildDefaultLogFilePath(loggerName: string): string {\n return `logs/${loggerName}.log`;\n}\n\nfunction resolveLoggerOptions(\n loggerName: string,\n options: LoggerOptions,\n): ResolvedLoggerOptions {\n return {\n includeOpenTelemetryContext: options.includeOpenTelemetryContext ?? false,\n logFilePath: options.logFilePath ?? buildDefaultLogFilePath(loggerName),\n logLevelThreshold: options.logLevelThreshold ?? DEFAULT_LOG_LEVEL,\n valueColorMode: options.valueColorMode ?? \"level\",\n writeToFile: options.writeToFile ?? false,\n };\n}\n\nfunction captureStackTrace(message = CAPTURE_STACK_TRACE_MESSAGE): string {\n return new Error(message).stack ?? DEFAULT_STACK_TRACE;\n}\n\nfunction minimizeStackTrace(stackTrace: string): string {\n const lines = stackTrace.split(LOG_FILE_LINE_BREAK);\n\n if (lines.length <= STACK_TRACE_HEADER_LINE_COUNT) {\n return stackTrace;\n }\n\n const filteredLines = lines.filter((line, index) => {\n if (index === ROOT_STACK_TRACE_INDEX) {\n return true;\n }\n\n return !line.includes(NODE_MODULES_SEGMENT);\n });\n\n return filteredLines.join(LOG_FILE_LINE_BREAK);\n}\n\nfunction isPrimitiveArgument(\n argument: unknown,\n): argument is StringifiablePrimitive {\n return (\n typeof argument === \"bigint\" ||\n typeof argument === \"boolean\" ||\n typeof argument === \"number\" ||\n typeof argument === \"string\" ||\n typeof argument === \"symbol\"\n );\n}\n\nfunction stringifyPrimitiveArgument(argument: StringifiablePrimitive): StringifiedArgument {\n if (typeof argument === \"string\") {\n return {\n kind: \"string\",\n text: argument,\n };\n }\n\n if (typeof argument === \"symbol\") {\n return {\n kind: \"symbol\",\n text: argument.toString(),\n };\n }\n\n if (typeof argument === \"boolean\") {\n return {\n kind: \"boolean\",\n text: `${argument}`,\n };\n }\n\n return {\n kind: \"number\",\n text: `${argument}`,\n };\n}\n\nfunction stringifyFunctionArgument(functionName: string): StringifiedArgument {\n if (functionName.length === EMPTY_FUNCTION_NAME_LENGTH) {\n return {\n kind: \"function\",\n text: ANONYMOUS_FUNCTION_LABEL,\n };\n }\n\n return {\n kind: \"function\",\n text: `${FUNCTION_LABEL_PREFIX}${functionName}${FUNCTION_LABEL_SUFFIX}`,\n };\n}\n\nfunction identityJsonReplacer(_key: string, value: unknown): unknown {\n return value;\n}\n\nfunction stringifyJsonArgument(argument: unknown): StringifiedArgument {\n try {\n const stringifiedArgument = JSON.stringify(\n argument,\n identityJsonReplacer,\n JSON_INDENTATION,\n );\n\n if (typeof stringifiedArgument !== \"string\") {\n return {\n kind: \"object\",\n text: FALLBACK_OBJECT_STRING,\n };\n }\n\n return {\n kind: \"object\",\n text: stringifiedArgument,\n };\n } catch {\n return {\n kind: \"object\",\n text: FALLBACK_OBJECT_STRING,\n };\n }\n}\n\nfunction stringifyArgument(argument: unknown): StringifiedArgument {\n if (typeof argument === \"undefined\") {\n return {\n kind: \"nullish\",\n text: \"undefined\",\n };\n }\n\n if (argument === null) {\n return {\n kind: \"nullish\",\n text: \"null\",\n };\n }\n\n if (isPrimitiveArgument(argument)) {\n return stringifyPrimitiveArgument(argument);\n }\n\n if (typeof argument === \"function\") {\n return stringifyFunctionArgument(argument.name);\n }\n\n return stringifyJsonArgument(argument);\n}\n\nfunction stringifyArguments(\n argumentsList: readonly unknown[],\n): readonly StringifiedArgument[] {\n return argumentsList.map((argument) => {\n return stringifyArgument(argument);\n });\n}\n\nfunction buildLogMessage(\n timestamp: string,\n levelLabel: string,\n loggerName: string,\n openTelemetryContextLabel: string,\n message: string,\n): string {\n return `${timestamp} ${levelLabel} [${loggerName}]${openTelemetryContextLabel} - ${message}`;\n}\n\nfunction resolveStackTrace(stackTrace?: string): string {\n if (typeof stackTrace === \"string\") {\n return minimizeStackTrace(stackTrace);\n }\n\n return DEFAULT_STACK_TRACE;\n}\n\nfunction buildColoredLogMessage(\n level: LogLevel,\n timestamp: string,\n loggerName: string,\n openTelemetryContextLabel: string,\n message: string,\n): string {\n const colorCode = LOG_LEVEL_COLORS[level];\n\n return buildLogMessage(\n colorize(timestamp, colorCode),\n colorize(`[${level}]`, colorCode),\n colorize(loggerName, colorCode),\n openTelemetryContextLabel,\n colorize(message, colorCode),\n );\n}\n\nfunction formatOpenTelemetryContextLabel(\n openTelemetryContext?: OpenTelemetryContext,\n): string {\n if (\n typeof openTelemetryContext !== \"object\" ||\n openTelemetryContext.spanId.length === EMPTY_FUNCTION_NAME_LENGTH ||\n openTelemetryContext.traceId.length === EMPTY_FUNCTION_NAME_LENGTH\n ) {\n return \"\";\n }\n\n return ` [trace_id=${openTelemetryContext.traceId} span_id=${openTelemetryContext.spanId}]`;\n}\n\nfunction colorizeOpenTelemetryContextLabel(\n level: LogLevel,\n openTelemetryContext: OpenTelemetryContext | undefined,\n valueColorMode: ValueColorMode,\n): string {\n const plainOpenTelemetryContextLabel = formatOpenTelemetryContextLabel(\n openTelemetryContext,\n );\n\n if (\n valueColorMode === \"off\" ||\n plainOpenTelemetryContextLabel.length === EMPTY_FUNCTION_NAME_LENGTH\n ) {\n return plainOpenTelemetryContextLabel;\n }\n\n if (valueColorMode === \"syntax\") {\n return colorize(plainOpenTelemetryContextLabel, ANSI_GRAY);\n }\n\n return colorize(plainOpenTelemetryContextLabel, LOG_LEVEL_COLORS[level]);\n}\n\nfunction getSyntaxColorCode(kind: StringifiedArgumentKind): number {\n switch (kind) {\n case \"boolean\": {\n return ANSI_YELLOW;\n }\n case \"function\": {\n return ANSI_MAGENTA;\n }\n case \"nullish\": {\n return ANSI_GRAY;\n }\n case \"number\": {\n return ANSI_CYAN;\n }\n case \"object\": {\n return ANSI_BLUE;\n }\n case \"string\": {\n return ANSI_GREEN;\n }\n case \"symbol\": {\n return ANSI_MAGENTA;\n }\n default: {\n return ANSI_GREEN;\n }\n }\n}\n\nfunction colorizeArguments(\n argumentsList: readonly StringifiedArgument[],\n level: LogLevel,\n valueColorMode: ValueColorMode,\n): readonly string[] {\n if (valueColorMode === \"off\") {\n return argumentsList.map((argument) => {\n return argument.text;\n });\n }\n\n if (valueColorMode === \"syntax\") {\n return argumentsList.map((argument) => {\n return colorize(argument.text, getSyntaxColorCode(argument.kind));\n });\n }\n\n const colorCode = LOG_LEVEL_COLORS[level];\n\n return argumentsList.map((argument) => {\n return colorize(argument.text, colorCode);\n });\n}\n\nfunction buildPlainConsoleLogMessage(\n level: LogLevel,\n timestamp: string,\n loggerName: string,\n openTelemetryContext: OpenTelemetryContext | undefined,\n message: string,\n): string {\n return buildLogMessage(\n timestamp,\n `[${level}]`,\n loggerName,\n formatOpenTelemetryContextLabel(openTelemetryContext),\n message,\n );\n}\n\nfunction buildConsoleLogMessage(\n level: LogLevel,\n timestamp: string,\n loggerName: string,\n openTelemetryContext: OpenTelemetryContext | undefined,\n message: string,\n valueColorMode: ValueColorMode,\n): string {\n if (valueColorMode === \"off\") {\n return buildPlainConsoleLogMessage(\n level,\n timestamp,\n loggerName,\n openTelemetryContext,\n message,\n );\n }\n\n return buildColoredLogMessage(\n level,\n timestamp,\n loggerName,\n colorizeOpenTelemetryContextLabel(\n level,\n openTelemetryContext,\n valueColorMode,\n ),\n message,\n );\n}\n\nfunction buildConsoleStackTrace(\n stackTrace: string,\n level: LogLevel,\n valueColorMode: ValueColorMode,\n): string {\n if (valueColorMode === \"off\") {\n return stackTrace;\n }\n\n return colorize(stackTrace, LOG_LEVEL_COLORS[level]);\n}\n\nfunction createConsolePayload(\n level: LogLevel,\n timestamp: string,\n loggerName: string,\n openTelemetryContext: OpenTelemetryContext | undefined,\n message: string,\n plainArgs: readonly StringifiedArgument[],\n plainStackTrace: string,\n valueColorMode: ValueColorMode,\n): Pick<LogPayload, \"consoleArgs\" | \"consoleLogMessage\" | \"consoleStackTrace\"> {\n return {\n consoleArgs: colorizeArguments(plainArgs, level, valueColorMode),\n consoleLogMessage: buildConsoleLogMessage(\n level,\n timestamp,\n loggerName,\n openTelemetryContext,\n message,\n valueColorMode,\n ),\n consoleStackTrace: buildConsoleStackTrace(\n plainStackTrace,\n level,\n valueColorMode,\n ),\n };\n}\n\nfunction createPlainLogParts(\n level: LogLevel,\n loggerName: string,\n openTelemetryContext: OpenTelemetryContext | undefined,\n message: string,\n args: readonly unknown[],\n stackTrace?: string,\n): PlainLogParts {\n const plainStackTrace = resolveStackTrace(stackTrace);\n const timestamp = new Date().toISOString();\n\n return {\n openTelemetryContext,\n plainArgs: stringifyArguments(args),\n plainLogMessage: buildLogMessage(\n timestamp,\n `[${level}]`,\n loggerName,\n formatOpenTelemetryContextLabel(openTelemetryContext),\n message,\n ),\n plainStackTrace,\n timestamp,\n };\n}\n\nfunction resolveOpenTelemetryContext(\n includeOpenTelemetryContext: boolean,\n): OpenTelemetryContext {\n if (!includeOpenTelemetryContext) {\n return EMPTY_OPEN_TELEMETRY_CONTEXT;\n }\n\n const activeSpan = trace.getActiveSpan();\n\n if (typeof activeSpan === \"undefined\") {\n return EMPTY_OPEN_TELEMETRY_CONTEXT;\n }\n\n const spanContext = activeSpan.spanContext();\n\n return {\n spanId: spanContext.spanId,\n traceId: spanContext.traceId,\n };\n}\n\nfunction combineLogPayload(\n consolePayload: Pick<\n LogPayload,\n \"consoleArgs\" | \"consoleLogMessage\" | \"consoleStackTrace\"\n >,\n plainLogParts: PlainLogParts,\n): LogPayload {\n return {\n ...consolePayload,\n plainLogMessage: plainLogParts.plainLogMessage,\n plainStackTrace: plainLogParts.plainStackTrace,\n };\n}\n\nfunction createConsolePayloadFromPlainLogParts(\n level: LogLevel,\n loggerName: string,\n message: string,\n plainLogParts: PlainLogParts,\n valueColorMode: ValueColorMode,\n): Pick<LogPayload, \"consoleArgs\" | \"consoleLogMessage\" | \"consoleStackTrace\"> {\n return createConsolePayload(\n level,\n plainLogParts.timestamp,\n loggerName,\n plainLogParts.openTelemetryContext,\n message,\n plainLogParts.plainArgs,\n plainLogParts.plainStackTrace,\n valueColorMode,\n );\n}\n\nfunction createLogPayload(\n level: LogLevel,\n loggerName: string,\n message: string,\n args: readonly unknown[],\n includeOpenTelemetryContext: boolean,\n valueColorMode: ValueColorMode,\n stackTrace?: string,\n): LogPayload {\n const openTelemetryContext = resolveOpenTelemetryContext(\n includeOpenTelemetryContext,\n );\n const plainLogParts = createPlainLogParts(\n level,\n loggerName,\n openTelemetryContext,\n message,\n args,\n stackTrace,\n );\n const consolePayload = createConsolePayloadFromPlainLogParts(\n level,\n loggerName,\n message,\n plainLogParts,\n valueColorMode,\n );\n\n return combineLogPayload(consolePayload, plainLogParts);\n}\n\nfunction writeLogLine(\n runtime: LoggerRuntime,\n writeToFile: boolean,\n logFilePath: string,\n logMessage: string,\n): void {\n if (!writeToFile) {\n return;\n }\n\n const { appendTextFileSync } = runtime;\n\n if (typeof appendTextFileSync !== \"function\") {\n return;\n }\n\n appendTextFileSync(logFilePath, `${logMessage}${LOG_FILE_LINE_BREAK}`);\n}\n\nfunction shouldLog(level: LogLevel, threshold: LogLevel): boolean {\n return LOG_LEVEL_PRIORITY[level] >= LOG_LEVEL_PRIORITY[threshold];\n}\n\nclass LoggerBase {\n private readonly LoggerConstructor: LoggerConstructor;\n private readonly logFilePath: string;\n private readonly logLevelThreshold: LogLevel;\n private readonly name: string;\n private readonly parent?: LoggerParent;\n private readonly performanceBenchmarks = new Map<string, number>();\n private readonly runtime: LoggerRuntime;\n private readonly includeOpenTelemetryContext: boolean;\n private readonly valueColorMode: ValueColorMode;\n private readonly writeToFile: boolean;\n\n protected constructor(\n LoggerConstructor: LoggerConstructor,\n runtime: LoggerRuntime,\n name: string,\n parent?: LoggerParent,\n options: LoggerOptions = {},\n ) {\n const resolvedOptions = resolveLoggerOptions(name, options);\n\n this.LoggerConstructor = LoggerConstructor;\n this.includeOpenTelemetryContext = resolvedOptions.includeOpenTelemetryContext;\n this.logFilePath = resolvedOptions.logFilePath;\n this.logLevelThreshold = resolvedOptions.logLevelThreshold;\n this.name = name;\n this.parent = parent;\n this.runtime = runtime;\n this.valueColorMode = resolvedOptions.valueColorMode;\n this.writeToFile = resolvedOptions.writeToFile;\n }\n\n public assert(\n condition: boolean,\n message: string,\n ...args: readonly unknown[]\n ): void {\n if (condition) {\n return;\n }\n\n const assertionMessage = `Assertion failed: ${message}`;\n\n this.log(\"ERROR\", assertionMessage, args, captureStackTrace(assertionMessage));\n }\n\n public createChildLogger(\n name: string,\n otherLogger?: LoggerParent,\n ): LoggerBase {\n if (typeof otherLogger === \"object\") {\n return new this.LoggerConstructor(\n name,\n this.mergeLoggers(otherLogger),\n this.getInheritedOptions(),\n );\n }\n\n return new this.LoggerConstructor(name, this, this.getInheritedOptions());\n }\n\n public debug(message: string, ...args: readonly unknown[]): void {\n this.log(\"DEBUG\", message, args);\n }\n\n public endPerformanceBenchmark(label: string, message: string): void {\n const startTime = this.performanceBenchmarks.get(label);\n\n if (typeof startTime !== \"number\") {\n this.warn(\n `No start time found for performance test with label \"${label}\"`,\n );\n return;\n }\n\n const elapsedMilliseconds = Date.now() - startTime;\n\n this.debug(`${message} - Elapsed Time: ${elapsedMilliseconds}ms`);\n this.performanceBenchmarks.delete(label);\n }\n\n public error(message: string, ...args: readonly unknown[]): void {\n this.log(\"ERROR\", message, args, captureStackTrace(message));\n }\n\n public getFullLoggerName(): string {\n if (typeof this.parent === \"object\") {\n return `${this.parent.getFullLoggerName()}${LOGGER_NAME_SEPARATOR}${this.name}`;\n }\n\n return this.name;\n }\n\n public getInheritedOptions(): LoggerOptions {\n return {\n includeOpenTelemetryContext: this.includeOpenTelemetryContext,\n logFilePath: this.logFilePath,\n logLevelThreshold: this.logLevelThreshold,\n valueColorMode: this.valueColorMode,\n writeToFile: this.writeToFile,\n };\n }\n\n public info(message: string, ...args: readonly unknown[]): void {\n this.log(\"INFO\", message, args);\n }\n\n public startPerformanceBenchmark(label: string): void {\n this.performanceBenchmarks.set(label, Date.now());\n }\n\n public warn(message: string, ...args: readonly unknown[]): void {\n this.log(\"WARN\", message, args);\n }\n\n private log(\n level: LogLevel,\n message: string,\n args: readonly unknown[],\n stackTrace?: string,\n ): void {\n if (!shouldLog(level, this.logLevelThreshold)) {\n return;\n }\n\n const payload = createLogPayload(\n level,\n this.getFullLoggerName(),\n message,\n args,\n this.includeOpenTelemetryContext,\n this.valueColorMode,\n stackTrace,\n );\n\n this.emitPayload(payload);\n }\n\n private logStackTrace(\n plainStackTrace: string,\n coloredStackTrace: string,\n ): void {\n if (plainStackTrace.length === EMPTY_FUNCTION_NAME_LENGTH) {\n return;\n }\n\n writeLogLine(\n this.runtime,\n this.writeToFile,\n this.logFilePath,\n plainStackTrace,\n );\n this.runtime.console.error(coloredStackTrace);\n }\n\n private emitPayload(payload: LogPayload): void {\n writeLogLine(\n this.runtime,\n this.writeToFile,\n this.logFilePath,\n payload.plainLogMessage,\n );\n this.runtime.console.log(payload.consoleLogMessage, ...payload.consoleArgs);\n this.logStackTrace(payload.plainStackTrace, payload.consoleStackTrace);\n }\n\n private mergeLoggers(otherLogger: LoggerParent): LoggerParent {\n return new this.LoggerConstructor(\n this.name,\n otherLogger,\n this.getInheritedOptions(),\n );\n }\n}\n\nexport { LoggerBase, createLoggerConsole };\nexport type {\n LogLevel,\n LoggerConsole,\n LoggerConstructor,\n LoggerOptions,\n LoggerParent,\n LoggerRuntime,\n ValueColorMode,\n};\n"],"mappings":";;AAEA,MAAM,mBAAmB;AACzB,MAAM,aAAa;AACnB,MAAM,cAAc;AACpB,MAAM,WAAW;AACjB,MAAM,YAAY;AAClB,MAAM,YAAY;AAClB,MAAM,eAAe;AACrB,MAAM,YAAY;AAClB,MAAM,6BAA6B;AACnC,MAAM,mBAAmB;AACzB,MAAM,gCAAgC;AACtC,MAAM,yBAAyB;AAC/B,MAAM,oBAAoB;AAC1B,MAAM,sBAAsB;AAC5B,MAAM,yBAAyB;AAC/B,MAAM,+BAAqD;CACzD,QAAQ;CACR,SAAS;CACV;AACD,MAAM,8BAA8B;AACpC,MAAM,wBAAwB;AAC9B,MAAM,wBAAwB;AAC9B,MAAM,2BAA2B;AACjC,MAAM,wBAAwB;AAC9B,MAAM,sBAAsB;AAC5B,MAAM,uBAAuB;AAgF7B,MAAM,mBAAuD;CAC3D,OAAO;CACP,OAAO;CACP,MAAM;CACN,MAAM;CACP;AAED,MAAM,qBAAyD;CAC7D,OAAO;CACP,OAAO;CACP,MAAM;CACN,MAAM;CACP;AAED,SAAS,SAAS,MAAc,WAA2B;AACzD,QAAO,UAAU,UAAU,GAAG,KAAK,SAAS,iBAA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+1,26 @@
|
|
|
1
|
+
import { noAdHocConsoleFallbackRule } from "./no-ad-hoc-console-fallback.mjs";
|
|
2
|
+
import { noConsoleAliasRule } from "./no-console-alias.mjs";
|
|
3
|
+
import { preferChildLoggerRule } from "./prefer-child-logger.mjs";
|
|
4
|
+
import { preferHonoContextLoggerRule } from "./prefer-hono-context-logger.mjs";
|
|
5
|
+
import { preferRuntimeEntryRule } from "./prefer-runtime-entry.mjs";
|
|
6
|
+
import { preferSimplelogRule } from "./prefer-simplelog.mjs";
|
|
7
|
+
import { requireLoggerNameRule } from "./require-logger-name.mjs";
|
|
8
|
+
|
|
9
|
+
const plugin = {
|
|
10
|
+
meta: {
|
|
11
|
+
name: "simplelog",
|
|
12
|
+
},
|
|
13
|
+
rules: {
|
|
14
|
+
"no-ad-hoc-console-fallback": noAdHocConsoleFallbackRule,
|
|
15
|
+
"no-console-alias": noConsoleAliasRule,
|
|
16
|
+
"prefer-child-logger": preferChildLoggerRule,
|
|
17
|
+
"prefer-hono-context-logger": preferHonoContextLoggerRule,
|
|
18
|
+
"prefer-runtime-entry": preferRuntimeEntryRule,
|
|
19
|
+
"prefer-simplelog": preferSimplelogRule,
|
|
20
|
+
"require-logger-name": requireLoggerNameRule,
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// oxlint-disable-next-line import/no-default-export -- Oxlint JS plugins require a default export surface.
|
|
25
|
+
export default plugin;
|
|
26
|
+
export { plugin as simplelogPlugin };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { isConsoleObjectExpression, isLoggerLikeTarget } from "./utils.mjs";
|
|
2
|
+
|
|
3
|
+
export const noAdHocConsoleFallbackRule = {
|
|
4
|
+
meta: {
|
|
5
|
+
type: "problem",
|
|
6
|
+
docs: {
|
|
7
|
+
description:
|
|
8
|
+
"Disallow using console as a fallback or alias in logger-like code paths.",
|
|
9
|
+
},
|
|
10
|
+
schema: [],
|
|
11
|
+
messages: {
|
|
12
|
+
noAdHocConsoleFallback:
|
|
13
|
+
"Do not use console as an ad-hoc logger fallback or alias. Pass a SimpleLog logger or create one intentionally.",
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
create(context) {
|
|
17
|
+
function reportConsoleNode(node) {
|
|
18
|
+
context.report({
|
|
19
|
+
messageId: "noAdHocConsoleFallback",
|
|
20
|
+
node,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
LogicalExpression(node) {
|
|
26
|
+
if (node.operator !== "||" && node.operator !== "??") {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (isConsoleObjectExpression(node.left)) {
|
|
31
|
+
reportConsoleNode(node.left);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (isConsoleObjectExpression(node.right)) {
|
|
35
|
+
reportConsoleNode(node.right);
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
ConditionalExpression(node) {
|
|
39
|
+
if (isConsoleObjectExpression(node.consequent)) {
|
|
40
|
+
reportConsoleNode(node.consequent);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (isConsoleObjectExpression(node.alternate)) {
|
|
44
|
+
reportConsoleNode(node.alternate);
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
VariableDeclarator(node) {
|
|
48
|
+
if (
|
|
49
|
+
isLoggerLikeTarget(node.id) &&
|
|
50
|
+
isConsoleObjectExpression(node.init)
|
|
51
|
+
) {
|
|
52
|
+
reportConsoleNode(node.init);
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
AssignmentExpression(node) {
|
|
56
|
+
if (
|
|
57
|
+
isLoggerLikeTarget(node.left) &&
|
|
58
|
+
isConsoleObjectExpression(node.right)
|
|
59
|
+
) {
|
|
60
|
+
reportConsoleNode(node.right);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
},
|
|
65
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isConsoleBindCall,
|
|
3
|
+
isConsoleMethodReference,
|
|
4
|
+
isConsoleObjectExpression,
|
|
5
|
+
} from "./utils.mjs";
|
|
6
|
+
|
|
7
|
+
export const noConsoleAliasRule = {
|
|
8
|
+
meta: {
|
|
9
|
+
type: "problem",
|
|
10
|
+
docs: {
|
|
11
|
+
description:
|
|
12
|
+
"Disallow aliasing console or console methods in codebases that standardize on SimpleLog.",
|
|
13
|
+
},
|
|
14
|
+
schema: [],
|
|
15
|
+
messages: {
|
|
16
|
+
noConsoleAlias:
|
|
17
|
+
"Do not alias console or console methods. Use a SimpleLog logger instead.",
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
create(context) {
|
|
21
|
+
function report(node) {
|
|
22
|
+
context.report({
|
|
23
|
+
messageId: "noConsoleAlias",
|
|
24
|
+
node,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
VariableDeclarator(node) {
|
|
30
|
+
if (isConsoleObjectExpression(node.init)) {
|
|
31
|
+
report(node.init);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (isConsoleMethodReference(node.init)) {
|
|
36
|
+
report(node.init);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (isConsoleBindCall(node.init)) {
|
|
41
|
+
report(node.init);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (
|
|
46
|
+
node.id?.type === "ObjectPattern" &&
|
|
47
|
+
isConsoleObjectExpression(node.init)
|
|
48
|
+
) {
|
|
49
|
+
report(node.init);
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
AssignmentExpression(node) {
|
|
53
|
+
if (isConsoleObjectExpression(node.right)) {
|
|
54
|
+
report(node.right);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (isConsoleMethodReference(node.right)) {
|
|
59
|
+
report(node.right);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (isConsoleBindCall(node.right)) {
|
|
64
|
+
report(node.right);
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
},
|
|
69
|
+
};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { isInsideFunctionScope, isLoggerImportSource } from "./utils.mjs";
|
|
2
|
+
|
|
3
|
+
export const preferChildLoggerRule = {
|
|
4
|
+
meta: {
|
|
5
|
+
type: "suggestion",
|
|
6
|
+
docs: {
|
|
7
|
+
description:
|
|
8
|
+
"Prefer deriving child loggers from an existing SimpleLog logger inside functions and request handlers.",
|
|
9
|
+
},
|
|
10
|
+
schema: [],
|
|
11
|
+
messages: {
|
|
12
|
+
preferChildLogger:
|
|
13
|
+
"Prefer logger.createChildLogger(...) over creating a new root Logger inside a nested function scope.",
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
create(context) {
|
|
17
|
+
const loggerConstructors = new Set();
|
|
18
|
+
const loggerNamespaces = new Set();
|
|
19
|
+
|
|
20
|
+
return {
|
|
21
|
+
ImportDeclaration(node) {
|
|
22
|
+
if (
|
|
23
|
+
node.source?.type !== "Literal" ||
|
|
24
|
+
typeof node.source.value !== "string" ||
|
|
25
|
+
!isLoggerImportSource(node.source.value)
|
|
26
|
+
) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
for (const specifier of node.specifiers ?? []) {
|
|
31
|
+
if (specifier.type === "ImportSpecifier") {
|
|
32
|
+
if (
|
|
33
|
+
specifier.imported?.type === "Identifier" &&
|
|
34
|
+
specifier.imported.name === "Logger"
|
|
35
|
+
) {
|
|
36
|
+
loggerConstructors.add(specifier.local.name);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (specifier.type === "ImportNamespaceSpecifier") {
|
|
41
|
+
loggerNamespaces.add(specifier.local.name);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
NewExpression(node) {
|
|
46
|
+
if (!isInsideFunctionScope(node)) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (
|
|
51
|
+
node.callee?.type === "Identifier" &&
|
|
52
|
+
loggerConstructors.has(node.callee.name)
|
|
53
|
+
) {
|
|
54
|
+
context.report({
|
|
55
|
+
messageId: "preferChildLogger",
|
|
56
|
+
node: node.callee,
|
|
57
|
+
});
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (
|
|
62
|
+
node.callee?.type === "MemberExpression" &&
|
|
63
|
+
!node.callee.computed &&
|
|
64
|
+
node.callee.object?.type === "Identifier" &&
|
|
65
|
+
loggerNamespaces.has(node.callee.object.name) &&
|
|
66
|
+
node.callee.property?.type === "Identifier" &&
|
|
67
|
+
node.callee.property.name === "Logger"
|
|
68
|
+
) {
|
|
69
|
+
context.report({
|
|
70
|
+
messageId: "preferChildLogger",
|
|
71
|
+
node: node.callee.property,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
},
|
|
77
|
+
};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SIMPLELOG_LOG_METHODS,
|
|
3
|
+
functionHasContextParam,
|
|
4
|
+
getEnclosingFunction,
|
|
5
|
+
} from "./utils.mjs";
|
|
6
|
+
|
|
7
|
+
export const preferHonoContextLoggerRule = {
|
|
8
|
+
meta: {
|
|
9
|
+
type: "suggestion",
|
|
10
|
+
docs: {
|
|
11
|
+
description:
|
|
12
|
+
"Prefer c.var.logger over a root logger inside Hono request handlers.",
|
|
13
|
+
},
|
|
14
|
+
schema: [],
|
|
15
|
+
messages: {
|
|
16
|
+
preferHonoContextLogger:
|
|
17
|
+
"Prefer c.var.logger inside Hono handlers instead of calling a root logger directly.",
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
create(context) {
|
|
21
|
+
let honoImported = false;
|
|
22
|
+
const rootLoggerBindings = new Set();
|
|
23
|
+
const loggerConstructors = new Set();
|
|
24
|
+
const loggerNamespaces = new Set();
|
|
25
|
+
|
|
26
|
+
function isLoggerConstructor(node) {
|
|
27
|
+
if (node?.type === "Identifier") {
|
|
28
|
+
return loggerConstructors.has(node.name);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return (
|
|
32
|
+
node?.type === "MemberExpression" &&
|
|
33
|
+
!node.computed &&
|
|
34
|
+
node.object?.type === "Identifier" &&
|
|
35
|
+
loggerNamespaces.has(node.object.name) &&
|
|
36
|
+
node.property?.type === "Identifier" &&
|
|
37
|
+
node.property.name === "Logger"
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
ImportDeclaration(node) {
|
|
43
|
+
if (
|
|
44
|
+
node.source?.type === "Literal" &&
|
|
45
|
+
typeof node.source.value === "string" &&
|
|
46
|
+
node.source.value.startsWith("hono")
|
|
47
|
+
) {
|
|
48
|
+
honoImported = true;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (
|
|
52
|
+
node.source?.type !== "Literal" ||
|
|
53
|
+
typeof node.source.value !== "string" ||
|
|
54
|
+
!node.source.value.startsWith("@murky-web/simplelog")
|
|
55
|
+
) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
for (const specifier of node.specifiers ?? []) {
|
|
60
|
+
if (specifier.type === "ImportSpecifier") {
|
|
61
|
+
if (
|
|
62
|
+
specifier.imported?.type === "Identifier" &&
|
|
63
|
+
specifier.imported.name === "Logger"
|
|
64
|
+
) {
|
|
65
|
+
loggerConstructors.add(specifier.local.name);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (specifier.type === "ImportNamespaceSpecifier") {
|
|
70
|
+
loggerNamespaces.add(specifier.local.name);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
VariableDeclarator(node) {
|
|
75
|
+
if (
|
|
76
|
+
node.parent?.type === "VariableDeclaration" &&
|
|
77
|
+
node.parent.parent?.type === "Program" &&
|
|
78
|
+
node.id?.type === "Identifier" &&
|
|
79
|
+
node.init?.type === "NewExpression" &&
|
|
80
|
+
isLoggerConstructor(node.init.callee)
|
|
81
|
+
) {
|
|
82
|
+
rootLoggerBindings.add(node.id.name);
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
CallExpression(node) {
|
|
86
|
+
if (!honoImported) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (
|
|
91
|
+
node.callee?.type !== "MemberExpression" ||
|
|
92
|
+
node.callee.computed ||
|
|
93
|
+
node.callee.object?.type !== "Identifier" ||
|
|
94
|
+
!rootLoggerBindings.has(node.callee.object.name) ||
|
|
95
|
+
node.callee.property?.type !== "Identifier" ||
|
|
96
|
+
!SIMPLELOG_LOG_METHODS.has(node.callee.property.name)
|
|
97
|
+
) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const enclosingFunction = getEnclosingFunction(node);
|
|
102
|
+
if (!functionHasContextParam(enclosingFunction)) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
context.report({
|
|
107
|
+
messageId: "preferHonoContextLogger",
|
|
108
|
+
node: node.callee.object,
|
|
109
|
+
});
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
},
|
|
113
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { isBareSimplelogSource, SIMPLELOG_PACKAGE } from "./utils.mjs";
|
|
2
|
+
|
|
3
|
+
export const preferRuntimeEntryRule = {
|
|
4
|
+
meta: {
|
|
5
|
+
type: "suggestion",
|
|
6
|
+
docs: {
|
|
7
|
+
description:
|
|
8
|
+
"Prefer explicit SimpleLog runtime entries over the package root import.",
|
|
9
|
+
},
|
|
10
|
+
schema: [],
|
|
11
|
+
messages: {
|
|
12
|
+
preferRuntimeEntry: `Prefer an explicit runtime entry over "${SIMPLELOG_PACKAGE}". Use a runtime-specific import like "@murky-web/simplelog/node", "@murky-web/simplelog/web", "@murky-web/simplelog/bun", "@murky-web/simplelog/deno", or "@murky-web/simplelog/hono".`,
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
create(context) {
|
|
16
|
+
function reportSourceNode(node) {
|
|
17
|
+
if (
|
|
18
|
+
node?.type === "Literal" &&
|
|
19
|
+
typeof node.value === "string" &&
|
|
20
|
+
isBareSimplelogSource(node.value)
|
|
21
|
+
) {
|
|
22
|
+
context.report({
|
|
23
|
+
messageId: "preferRuntimeEntry",
|
|
24
|
+
node,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
ImportDeclaration(node) {
|
|
31
|
+
reportSourceNode(node.source);
|
|
32
|
+
},
|
|
33
|
+
ExportNamedDeclaration(node) {
|
|
34
|
+
reportSourceNode(node.source);
|
|
35
|
+
},
|
|
36
|
+
ExportAllDeclaration(node) {
|
|
37
|
+
reportSourceNode(node.source);
|
|
38
|
+
},
|
|
39
|
+
CallExpression(node) {
|
|
40
|
+
if (
|
|
41
|
+
node.callee?.type === "Identifier" &&
|
|
42
|
+
node.callee.name === "require" &&
|
|
43
|
+
node.arguments?.[0]
|
|
44
|
+
) {
|
|
45
|
+
reportSourceNode(node.arguments[0]);
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
},
|
|
50
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
const CONSOLE_METHOD_TO_LOGGER_METHOD = {
|
|
2
|
+
debug: "debug",
|
|
3
|
+
error: "error",
|
|
4
|
+
info: "info",
|
|
5
|
+
log: "info",
|
|
6
|
+
trace: "debug",
|
|
7
|
+
warn: "warn",
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const preferSimplelogRule = {
|
|
11
|
+
meta: {
|
|
12
|
+
type: "suggestion",
|
|
13
|
+
docs: {
|
|
14
|
+
description:
|
|
15
|
+
"Prefer @murky-web/simplelog over direct console method calls.",
|
|
16
|
+
},
|
|
17
|
+
schema: [],
|
|
18
|
+
messages: {
|
|
19
|
+
preferSimplelog:
|
|
20
|
+
"Prefer a SimpleLog logger over direct console.{{consoleMethod}}(). Use logger.{{loggerMethod}}(...) instead.",
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
create(context) {
|
|
24
|
+
return {
|
|
25
|
+
CallExpression(node) {
|
|
26
|
+
if (node.callee?.type !== "MemberExpression") {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (node.callee.computed) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (
|
|
35
|
+
node.callee.object?.type !== "Identifier" ||
|
|
36
|
+
node.callee.object.name !== "console"
|
|
37
|
+
) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (node.callee.property?.type !== "Identifier") {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const consoleMethod = node.callee.property.name;
|
|
46
|
+
const loggerMethod =
|
|
47
|
+
CONSOLE_METHOD_TO_LOGGER_METHOD[consoleMethod];
|
|
48
|
+
|
|
49
|
+
if (!loggerMethod) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
context.report({
|
|
54
|
+
messageId: "preferSimplelog",
|
|
55
|
+
data: {
|
|
56
|
+
consoleMethod,
|
|
57
|
+
loggerMethod,
|
|
58
|
+
},
|
|
59
|
+
node: node.callee.property,
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
export const requireLoggerNameRule = {
|
|
2
|
+
meta: {
|
|
3
|
+
type: "problem",
|
|
4
|
+
docs: {
|
|
5
|
+
description:
|
|
6
|
+
"Require SimpleLog Logger instances to have a non-empty name for useful log output.",
|
|
7
|
+
},
|
|
8
|
+
schema: [],
|
|
9
|
+
messages: {
|
|
10
|
+
requireLoggerName:
|
|
11
|
+
"Provide a non-empty logger name when creating a SimpleLog Logger.",
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
create(context) {
|
|
15
|
+
const loggerConstructors = new Set();
|
|
16
|
+
const loggerNamespaces = new Set();
|
|
17
|
+
|
|
18
|
+
function isLoggerConstructor(node) {
|
|
19
|
+
if (node?.type === "Identifier") {
|
|
20
|
+
return loggerConstructors.has(node.name);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return (
|
|
24
|
+
node?.type === "MemberExpression" &&
|
|
25
|
+
!node.computed &&
|
|
26
|
+
node.object?.type === "Identifier" &&
|
|
27
|
+
loggerNamespaces.has(node.object.name) &&
|
|
28
|
+
node.property?.type === "Identifier" &&
|
|
29
|
+
node.property.name === "Logger"
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
ImportDeclaration(node) {
|
|
35
|
+
if (
|
|
36
|
+
node.source?.type !== "Literal" ||
|
|
37
|
+
typeof node.source.value !== "string" ||
|
|
38
|
+
!node.source.value.startsWith("@murky-web/simplelog")
|
|
39
|
+
) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
for (const specifier of node.specifiers ?? []) {
|
|
44
|
+
if (specifier.type === "ImportSpecifier") {
|
|
45
|
+
if (
|
|
46
|
+
specifier.imported?.type === "Identifier" &&
|
|
47
|
+
specifier.imported.name === "Logger"
|
|
48
|
+
) {
|
|
49
|
+
loggerConstructors.add(specifier.local.name);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (specifier.type === "ImportNamespaceSpecifier") {
|
|
54
|
+
loggerNamespaces.add(specifier.local.name);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
NewExpression(node) {
|
|
59
|
+
if (!isLoggerConstructor(node.callee)) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const [firstArgument] = node.arguments ?? [];
|
|
64
|
+
if (!firstArgument) {
|
|
65
|
+
context.report({
|
|
66
|
+
messageId: "requireLoggerName",
|
|
67
|
+
node: node.callee,
|
|
68
|
+
});
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (
|
|
73
|
+
firstArgument.type === "Literal" &&
|
|
74
|
+
typeof firstArgument.value === "string" &&
|
|
75
|
+
firstArgument.value.trim().length === 0
|
|
76
|
+
) {
|
|
77
|
+
context.report({
|
|
78
|
+
messageId: "requireLoggerName",
|
|
79
|
+
node: firstArgument,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
},
|
|
85
|
+
};
|
package/oxlint/utils.mjs
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
const SIMPLELOG_PACKAGE = "@murky-web/simplelog";
|
|
2
|
+
const SIMPLELOG_LOG_METHODS = new Set([
|
|
3
|
+
"debug",
|
|
4
|
+
"error",
|
|
5
|
+
"info",
|
|
6
|
+
"warn",
|
|
7
|
+
"startPerformanceBenchmark",
|
|
8
|
+
"endPerformanceBenchmark",
|
|
9
|
+
"assert",
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
function isSimplelogSource(source) {
|
|
13
|
+
return (
|
|
14
|
+
typeof source === "string" &&
|
|
15
|
+
(source === SIMPLELOG_PACKAGE ||
|
|
16
|
+
source.startsWith(`${SIMPLELOG_PACKAGE}/`))
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function isBareSimplelogSource(source) {
|
|
21
|
+
return source === SIMPLELOG_PACKAGE;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function isLoggerImportSource(source) {
|
|
25
|
+
return isSimplelogSource(source) && source !== `${SIMPLELOG_PACKAGE}/hono`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function isConsoleObjectExpression(node) {
|
|
29
|
+
if (!node) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (node.type === "Identifier" && node.name === "console") {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (node.type !== "MemberExpression" || node.computed) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
node.object?.type === "Identifier" &&
|
|
43
|
+
(node.object.name === "globalThis" ||
|
|
44
|
+
node.object.name === "global" ||
|
|
45
|
+
node.object.name === "window") &&
|
|
46
|
+
node.property?.type === "Identifier" &&
|
|
47
|
+
node.property.name === "console"
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function isLoggerLikeName(name) {
|
|
52
|
+
return typeof name === "string" && /logger/iu.test(name);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function isConsoleMethodReference(node) {
|
|
56
|
+
if (!node || node.type !== "MemberExpression" || node.computed) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
isConsoleObjectExpression(node.object) &&
|
|
62
|
+
node.property?.type === "Identifier" &&
|
|
63
|
+
typeof node.property.name === "string"
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function isConsoleBindCall(node) {
|
|
68
|
+
if (node?.type !== "CallExpression") {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (
|
|
73
|
+
node.callee?.type !== "MemberExpression" ||
|
|
74
|
+
node.callee.computed ||
|
|
75
|
+
node.callee.property?.type !== "Identifier" ||
|
|
76
|
+
node.callee.property.name !== "bind"
|
|
77
|
+
) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return isConsoleMethodReference(node.callee.object);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function collectAssignedNames(node, names = []) {
|
|
85
|
+
if (!node) {
|
|
86
|
+
return names;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (node.type === "Identifier") {
|
|
90
|
+
names.push(node.name);
|
|
91
|
+
return names;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (node.type === "RestElement") {
|
|
95
|
+
return collectAssignedNames(node.argument, names);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (node.type === "AssignmentPattern") {
|
|
99
|
+
return collectAssignedNames(node.left, names);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (node.type === "ObjectPattern") {
|
|
103
|
+
for (const property of node.properties ?? []) {
|
|
104
|
+
if (property?.type === "Property") {
|
|
105
|
+
collectAssignedNames(property.value, names);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (property?.type === "RestElement") {
|
|
109
|
+
collectAssignedNames(property.argument, names);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return names;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (node.type === "ArrayPattern") {
|
|
117
|
+
for (const element of node.elements ?? []) {
|
|
118
|
+
if (element) {
|
|
119
|
+
collectAssignedNames(element, names);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return names;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (
|
|
127
|
+
node.type === "MemberExpression" &&
|
|
128
|
+
!node.computed &&
|
|
129
|
+
node.property?.type === "Identifier"
|
|
130
|
+
) {
|
|
131
|
+
names.push(node.property.name);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return names;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function isLoggerLikeTarget(node) {
|
|
138
|
+
return collectAssignedNames(node).some((name) => isLoggerLikeName(name));
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function isInsideFunctionScope(node) {
|
|
142
|
+
let current = node.parent;
|
|
143
|
+
|
|
144
|
+
while (current) {
|
|
145
|
+
if (current.type === "Program") {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (
|
|
150
|
+
current.type === "FunctionDeclaration" ||
|
|
151
|
+
current.type === "FunctionExpression" ||
|
|
152
|
+
current.type === "ArrowFunctionExpression"
|
|
153
|
+
) {
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
current = current.parent;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function getEnclosingFunction(node) {
|
|
164
|
+
let current = node.parent;
|
|
165
|
+
|
|
166
|
+
while (current) {
|
|
167
|
+
if (
|
|
168
|
+
current.type === "FunctionDeclaration" ||
|
|
169
|
+
current.type === "FunctionExpression" ||
|
|
170
|
+
current.type === "ArrowFunctionExpression"
|
|
171
|
+
) {
|
|
172
|
+
return current;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
current = current.parent;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function functionHasContextParam(node) {
|
|
182
|
+
if (!node) {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return (node.params ?? []).some((param) => {
|
|
187
|
+
return collectAssignedNames(param).some(
|
|
188
|
+
(name) => name === "c" || name === "ctx" || name === "context",
|
|
189
|
+
);
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function isTopLevelNode(node) {
|
|
194
|
+
return node?.parent?.type === "Program";
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export {
|
|
198
|
+
functionHasContextParam,
|
|
199
|
+
getEnclosingFunction,
|
|
200
|
+
isBareSimplelogSource,
|
|
201
|
+
isConsoleBindCall,
|
|
202
|
+
isConsoleObjectExpression,
|
|
203
|
+
isConsoleMethodReference,
|
|
204
|
+
isInsideFunctionScope,
|
|
205
|
+
isLoggerImportSource,
|
|
206
|
+
isLoggerLikeTarget,
|
|
207
|
+
isTopLevelNode,
|
|
208
|
+
isSimplelogSource,
|
|
209
|
+
SIMPLELOG_LOG_METHODS,
|
|
210
|
+
SIMPLELOG_PACKAGE,
|
|
211
|
+
};
|
package/package.json
CHANGED
|
@@ -1,76 +1,82 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
".": {
|
|
10
|
-
"import": "./dist/logger.js",
|
|
11
|
-
"types": "./dist/logger.d.ts"
|
|
2
|
+
"name": "@murky-web/simplelog",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "A simple wrapper around console to make logging enjoyable again",
|
|
6
|
+
"homepage": "https://github.com/MurkyTheMurloc/web_dev_tools/tree/main/packages/simplelog",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/MurkyTheMurloc/web_dev_tools/issues"
|
|
12
9
|
},
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/MurkyTheMurloc/web_dev_tools.git",
|
|
14
|
+
"directory": "packages/simplelog"
|
|
16
15
|
},
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"oxlint"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"main": "./dist/logger.js",
|
|
23
|
+
"module": "./dist/logger.js",
|
|
24
|
+
"types": "./dist/logger.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": {
|
|
27
|
+
"import": "./dist/logger.js",
|
|
28
|
+
"types": "./dist/logger.d.ts"
|
|
29
|
+
},
|
|
30
|
+
"./oxlint": {
|
|
31
|
+
"import": "./oxlint/index.mjs",
|
|
32
|
+
"default": "./oxlint/index.mjs"
|
|
33
|
+
},
|
|
34
|
+
"./bun": {
|
|
35
|
+
"import": "./dist/bun_logger.js",
|
|
36
|
+
"types": "./dist/bun_logger.d.ts"
|
|
37
|
+
},
|
|
38
|
+
"./deno": {
|
|
39
|
+
"import": "./dist/deno_logger.js",
|
|
40
|
+
"types": "./dist/deno_logger.d.ts"
|
|
41
|
+
},
|
|
42
|
+
"./hono": {
|
|
43
|
+
"import": "./dist/hono_middleware.js",
|
|
44
|
+
"types": "./dist/hono_middleware.d.ts"
|
|
45
|
+
},
|
|
46
|
+
"./node": {
|
|
47
|
+
"import": "./dist/logger.js",
|
|
48
|
+
"types": "./dist/logger.d.ts"
|
|
49
|
+
},
|
|
50
|
+
"./web": {
|
|
51
|
+
"import": "./dist/web_logger.js",
|
|
52
|
+
"types": "./dist/web_logger.d.ts"
|
|
53
|
+
},
|
|
54
|
+
"./package.json": "./package.json"
|
|
20
55
|
},
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public",
|
|
58
|
+
"provenance": true
|
|
24
59
|
},
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
|
|
60
|
+
"scripts": {
|
|
61
|
+
"build": "bun run clean && tsdown",
|
|
62
|
+
"clean": "rm -rf dist",
|
|
63
|
+
"lint": "oxlint -c ./.oxlintrc.jsonc --tsconfig ./tsconfig.json --type-aware src tsdown.config.ts",
|
|
64
|
+
"lint:fix": "oxlint -c ./.oxlintrc.jsonc --tsconfig ./tsconfig.json --type-aware --fix src tsdown.config.ts",
|
|
65
|
+
"format": "oxfmt -c ../config/oxc/.oxfmtrc.jsonc src oxlint smoke tsdown.config.ts",
|
|
66
|
+
"format:check": "oxfmt -c ../config/oxc/.oxfmtrc.jsonc --check src oxlint smoke tsdown.config.ts",
|
|
67
|
+
"smoke:oxlint": "bun ./smoke/oxlint/run-smoke.ts",
|
|
68
|
+
"test": "vitest run",
|
|
69
|
+
"typecheck": "tsgo --project ./tsconfig.json --noEmit"
|
|
28
70
|
},
|
|
29
|
-
"
|
|
30
|
-
|
|
31
|
-
"types": "./dist/web_logger.d.ts"
|
|
71
|
+
"dependencies": {
|
|
72
|
+
"@opentelemetry/api": "^1.9.0"
|
|
32
73
|
},
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
"lint": "oxlint -c ./.oxlintrc.jsonc --tsconfig ./tsconfig.json --type-aware src tsdown.config.ts",
|
|
42
|
-
"lint:fix": "oxlint -c ./.oxlintrc.jsonc --tsconfig ./tsconfig.json --type-aware --fix src tsdown.config.ts",
|
|
43
|
-
"format": "oxfmt -c ../config/oxc/.oxfmtrc.jsonc src tsdown.config.ts",
|
|
44
|
-
"format:check": "oxfmt -c ../config/oxc/.oxfmtrc.jsonc --check src tsdown.config.ts",
|
|
45
|
-
"test": "vitest run",
|
|
46
|
-
"typecheck": "tsgo --project ./tsconfig.json --noEmit"
|
|
47
|
-
},
|
|
48
|
-
"license": "MIT",
|
|
49
|
-
"description": "A simple wrapper around console to make logging enjoyable again",
|
|
50
|
-
"type": "module",
|
|
51
|
-
"sideEffects": false,
|
|
52
|
-
"repository": {
|
|
53
|
-
"type": "git",
|
|
54
|
-
"url": "git+https://github.com/MurkyTheMurloc/web_dev_tools.git",
|
|
55
|
-
"directory": "packages/simplelog"
|
|
56
|
-
},
|
|
57
|
-
"homepage": "https://github.com/MurkyTheMurloc/web_dev_tools/tree/main/packages/simplelog",
|
|
58
|
-
"bugs": {
|
|
59
|
-
"url": "https://github.com/MurkyTheMurloc/web_dev_tools/issues"
|
|
60
|
-
},
|
|
61
|
-
"publishConfig": {
|
|
62
|
-
"access": "public",
|
|
63
|
-
"provenance": true
|
|
64
|
-
},
|
|
65
|
-
"dependencies": {
|
|
66
|
-
"@opentelemetry/api": "^1.9.0"
|
|
67
|
-
},
|
|
68
|
-
"devDependencies": {
|
|
69
|
-
"@murky-web/config": "workspace:*",
|
|
70
|
-
"@types/node": "^25.5.0",
|
|
71
|
-
"hono": "^4.12.8"
|
|
72
|
-
},
|
|
73
|
-
"peerDependencies": {
|
|
74
|
-
"hono": "^4.12.8"
|
|
75
|
-
}
|
|
74
|
+
"devDependencies": {
|
|
75
|
+
"@murky-web/config": "workspace:*",
|
|
76
|
+
"@types/node": "^25.5.0",
|
|
77
|
+
"hono": "^4.12.8"
|
|
78
|
+
},
|
|
79
|
+
"peerDependencies": {
|
|
80
|
+
"hono": "^4.12.8"
|
|
81
|
+
}
|
|
76
82
|
}
|