@encore.dev/sdk 0.0.0 → 2.0.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +362 -0
- package/README.md +122 -2
- package/app_meta.test.ts +134 -0
- package/app_meta.ts +186 -0
- package/config/config.test.ts +381 -0
- package/config/config.ts +178 -0
- package/config/mod.ts +14 -0
- package/config/secrets.ts +147 -0
- package/config/value.ts +243 -0
- package/cron/mod.ts +23 -0
- package/dist/app_meta.d.ts +93 -0
- package/dist/app_meta.js +65 -0
- package/dist/app_meta.js.map +1 -0
- package/dist/config/config.d.ts +105 -0
- package/dist/config/config.js +87 -0
- package/dist/config/config.js.map +1 -0
- package/dist/config/mod.d.ts +4 -0
- package/dist/config/mod.js +3 -0
- package/dist/config/mod.js.map +1 -0
- package/dist/config/secrets.d.ts +92 -0
- package/dist/config/secrets.js +80 -0
- package/dist/config/secrets.js.map +1 -0
- package/dist/config/value.d.ts +51 -0
- package/dist/config/value.js +178 -0
- package/dist/config/value.js.map +1 -0
- package/dist/cron/mod.d.ts +22 -0
- package/dist/cron/mod.js +9 -0
- package/dist/cron/mod.js.map +1 -0
- package/dist/internal/otel/backend.d.ts +50 -0
- package/dist/internal/otel/backend.js +2 -0
- package/dist/internal/otel/backend.js.map +1 -0
- package/dist/internal/otel/context.d.ts +10 -0
- package/dist/internal/otel/context.js +34 -0
- package/dist/internal/otel/context.js.map +1 -0
- package/dist/internal/otel/context_manager.d.ts +22 -0
- package/dist/internal/otel/context_manager.js +120 -0
- package/dist/internal/otel/context_manager.js.map +1 -0
- package/dist/internal/otel/encode.d.ts +112 -0
- package/dist/internal/otel/encode.js +290 -0
- package/dist/internal/otel/encode.js.map +1 -0
- package/dist/internal/otel/index.d.ts +9 -0
- package/dist/internal/otel/index.js +9 -0
- package/dist/internal/otel/index.js.map +1 -0
- package/dist/internal/otel/meter.d.ts +73 -0
- package/dist/internal/otel/meter.js +265 -0
- package/dist/internal/otel/meter.js.map +1 -0
- package/dist/internal/otel/propagator.d.ts +14 -0
- package/dist/internal/otel/propagator.js +61 -0
- package/dist/internal/otel/propagator.js.map +1 -0
- package/dist/internal/otel/provider.d.ts +9 -0
- package/dist/internal/otel/provider.js +19 -0
- package/dist/internal/otel/provider.js.map +1 -0
- package/dist/internal/otel/request_context.d.ts +11 -0
- package/dist/internal/otel/request_context.js +20 -0
- package/dist/internal/otel/request_context.js.map +1 -0
- package/dist/internal/otel/span.d.ts +61 -0
- package/dist/internal/otel/span.js +206 -0
- package/dist/internal/otel/span.js.map +1 -0
- package/dist/internal/otel/tracer.d.ts +15 -0
- package/dist/internal/otel/tracer.js +65 -0
- package/dist/internal/otel/tracer.js.map +1 -0
- package/dist/internal/reqtrack/mod.d.ts +6 -0
- package/dist/internal/reqtrack/mod.js +28 -0
- package/dist/internal/reqtrack/mod.js.map +1 -0
- package/dist/internal/runtime/meter.d.ts +11 -0
- package/dist/internal/runtime/meter.js +22 -0
- package/dist/internal/runtime/meter.js.map +1 -0
- package/dist/internal/runtime/mod.d.ts +12 -0
- package/dist/internal/runtime/mod.js +28 -0
- package/dist/internal/runtime/mod.js.map +1 -0
- package/dist/internal/runtime/napi/napi.cjs +122 -0
- package/dist/internal/runtime/napi/napi.d.cts +601 -0
- package/dist/internal/runtime/shutdown.d.ts +13 -0
- package/dist/internal/runtime/shutdown.js +56 -0
- package/dist/internal/runtime/shutdown.js.map +1 -0
- package/dist/internal/utils/constraints.d.ts +9 -0
- package/dist/internal/utils/constraints.js +2 -0
- package/dist/internal/utils/constraints.js.map +1 -0
- package/dist/log/index.d.ts +39 -0
- package/dist/log/index.js +44 -0
- package/dist/log/index.js.map +1 -0
- package/dist/log/logger.d.ts +123 -0
- package/dist/log/logger.js +203 -0
- package/dist/log/logger.js.map +1 -0
- package/dist/log/runtime.d.ts +19 -0
- package/dist/log/runtime.js +22 -0
- package/dist/log/runtime.js.map +1 -0
- package/dist/log/stderr.d.ts +9 -0
- package/dist/log/stderr.js +25 -0
- package/dist/log/stderr.js.map +1 -0
- package/dist/metrics/mod.d.ts +315 -0
- package/dist/metrics/mod.js +488 -0
- package/dist/metrics/mod.js.map +1 -0
- package/dist/mod.d.ts +6 -0
- package/dist/mod.js +4 -0
- package/dist/mod.js.map +1 -0
- package/dist/otel.d.ts +9 -0
- package/dist/otel.js +13 -0
- package/dist/otel.js.map +1 -0
- package/dist/pubsub/encoding.d.ts +3 -0
- package/dist/pubsub/encoding.js +10 -0
- package/dist/pubsub/encoding.js.map +1 -0
- package/dist/pubsub/mod.d.ts +6 -0
- package/dist/pubsub/mod.js +3 -0
- package/dist/pubsub/mod.js.map +1 -0
- package/dist/pubsub/refs.d.ts +12 -0
- package/dist/pubsub/refs.js +11 -0
- package/dist/pubsub/refs.js.map +1 -0
- package/dist/pubsub/subscription.d.ts +128 -0
- package/dist/pubsub/subscription.js +39 -0
- package/dist/pubsub/subscription.js.map +1 -0
- package/dist/pubsub/topic.d.ts +148 -0
- package/dist/pubsub/topic.js +164 -0
- package/dist/pubsub/topic.js.map +1 -0
- package/dist/req_meta.d.ts +47 -0
- package/dist/req_meta.js +26 -0
- package/dist/req_meta.js.map +1 -0
- package/dist/services/index.d.ts +7 -0
- package/dist/services/index.js +17 -0
- package/dist/services/index.js.map +1 -0
- package/dist/shutdown/mod.d.ts +23 -0
- package/dist/shutdown/mod.js +24 -0
- package/dist/shutdown/mod.js.map +1 -0
- package/dist/storage/objects/bucket.d.ts +216 -0
- package/dist/storage/objects/bucket.js +123 -0
- package/dist/storage/objects/bucket.js.map +1 -0
- package/dist/storage/objects/error.d.ts +14 -0
- package/dist/storage/objects/error.js +81 -0
- package/dist/storage/objects/error.js.map +1 -0
- package/dist/storage/objects/mod.d.ts +4 -0
- package/dist/storage/objects/mod.js +3 -0
- package/dist/storage/objects/mod.js.map +1 -0
- package/dist/storage/objects/refs.d.ts +31 -0
- package/dist/storage/objects/refs.js +20 -0
- package/dist/storage/objects/refs.js.map +1 -0
- package/dist/storage/postgres/database.d.ts +295 -0
- package/dist/storage/postgres/database.js +378 -0
- package/dist/storage/postgres/database.js.map +1 -0
- package/dist/storage/postgres/mod.d.ts +2 -0
- package/dist/storage/postgres/mod.js +2 -0
- package/dist/storage/postgres/mod.js.map +1 -0
- package/dist/storage/redis/commands.d.ts +573 -0
- package/dist/storage/redis/commands.js +637 -0
- package/dist/storage/redis/commands.js.map +1 -0
- package/dist/storage/redis/database.d.ts +302 -0
- package/dist/storage/redis/database.js +336 -0
- package/dist/storage/redis/database.js.map +1 -0
- package/dist/storage/redis/mod.d.ts +88 -0
- package/dist/storage/redis/mod.js +84 -0
- package/dist/storage/redis/mod.js.map +1 -0
- package/dist/storage/redis/native.d.ts +8 -0
- package/dist/storage/redis/native.js +11 -0
- package/dist/storage/redis/native.js.map +1 -0
- package/dist/storage/redis/options.d.ts +89 -0
- package/dist/storage/redis/options.js +53 -0
- package/dist/storage/redis/options.js.map +1 -0
- package/dist/storage/redis/pubsub.d.ts +53 -0
- package/dist/storage/redis/pubsub.js +88 -0
- package/dist/storage/redis/pubsub.js.map +1 -0
- package/dist/storage/redis/reply.d.ts +256 -0
- package/dist/storage/redis/reply.js +572 -0
- package/dist/storage/redis/reply.js.map +1 -0
- package/dist/storage/redis/scan.d.ts +11 -0
- package/dist/storage/redis/scan.js +76 -0
- package/dist/storage/redis/scan.js.map +1 -0
- package/dist/storage/redis/script.d.ts +91 -0
- package/dist/storage/redis/script.js +143 -0
- package/dist/storage/redis/script.js.map +1 -0
- package/dist/storage/redis/target.d.ts +47 -0
- package/dist/storage/redis/target.js +82 -0
- package/dist/storage/redis/target.js.map +1 -0
- package/dist/storage/redis/wire.d.ts +105 -0
- package/dist/storage/redis/wire.js +333 -0
- package/dist/storage/redis/wire.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/types/mod.d.ts +42 -0
- package/dist/types/mod.js +65 -0
- package/dist/types/mod.js.map +1 -0
- package/internal/otel/backend.ts +60 -0
- package/internal/otel/context.test.ts +70 -0
- package/internal/otel/context.ts +48 -0
- package/internal/otel/context_manager.test.ts +154 -0
- package/internal/otel/context_manager.ts +141 -0
- package/internal/otel/encode.test.ts +266 -0
- package/internal/otel/encode.ts +408 -0
- package/internal/otel/index.ts +20 -0
- package/internal/otel/meter.test.ts +231 -0
- package/internal/otel/meter.ts +332 -0
- package/internal/otel/propagator.test.ts +105 -0
- package/internal/otel/propagator.ts +68 -0
- package/internal/otel/provider.ts +23 -0
- package/internal/otel/request_context.ts +32 -0
- package/internal/otel/span.ts +252 -0
- package/internal/otel/tracer.test.ts +352 -0
- package/internal/otel/tracer.ts +104 -0
- package/internal/reqtrack/mod.ts +39 -0
- package/internal/runtime/meter.ts +26 -0
- package/internal/runtime/mod.ts +41 -0
- package/internal/runtime/napi/napi.cjs +122 -0
- package/internal/runtime/napi/napi.d.cts +601 -0
- package/internal/runtime/shutdown.test.ts +248 -0
- package/internal/runtime/shutdown.ts +64 -0
- package/internal/utils/constraints.ts +12 -0
- package/log/index.ts +107 -0
- package/log/logger.test.ts +194 -0
- package/log/logger.ts +308 -0
- package/log/runtime.test.ts +65 -0
- package/log/runtime.ts +40 -0
- package/log/stderr.test.ts +23 -0
- package/log/stderr.ts +24 -0
- package/metrics/export.test.ts +337 -0
- package/metrics/mod.test.ts +180 -0
- package/metrics/mod.ts +619 -0
- package/mod.ts +22 -0
- package/otel.test.ts +84 -0
- package/otel.ts +17 -0
- package/package.json +121 -5
- package/pubsub/encoding.ts +8 -0
- package/pubsub/mod.ts +14 -0
- package/pubsub/pubsub.test.ts +186 -0
- package/pubsub/refs.ts +22 -0
- package/pubsub/subscription.ts +180 -0
- package/pubsub/topic.test.ts +121 -0
- package/pubsub/topic.ts +212 -0
- package/req_meta.ts +75 -0
- package/services/index.test.ts +30 -0
- package/services/index.ts +16 -0
- package/shutdown/mod.ts +24 -0
- package/storage/objects/bucket.ts +338 -0
- package/storage/objects/error.ts +94 -0
- package/storage/objects/mod.ts +35 -0
- package/storage/objects/refs.ts +69 -0
- package/storage/postgres/database.integration.test.ts +175 -0
- package/storage/postgres/database.test.ts +241 -0
- package/storage/postgres/database.ts +497 -0
- package/storage/postgres/mod.ts +15 -0
- package/storage/redis/commands.ts +1221 -0
- package/storage/redis/database.ts +466 -0
- package/storage/redis/mod.ts +135 -0
- package/storage/redis/native.ts +11 -0
- package/storage/redis/options.ts +119 -0
- package/storage/redis/pubsub.ts +92 -0
- package/storage/redis/redis.test.ts +706 -0
- package/storage/redis/reply.ts +705 -0
- package/storage/redis/runtime.test.ts +233 -0
- package/storage/redis/scan.ts +125 -0
- package/storage/redis/script.ts +162 -0
- package/storage/redis/target.ts +131 -0
- package/storage/redis/wire.ts +369 -0
- package/types/mod.ts +92 -0
package/log/logger.ts
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import {isSpanContextValid, trace, type SpanContext} from '@opentelemetry/api';
|
|
2
|
+
import {activeContext} from '../internal/otel/context';
|
|
3
|
+
import {encodeLog} from '../internal/runtime/napi/napi.cjs';
|
|
4
|
+
import {writeStderr} from './stderr';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A field value we support logging
|
|
8
|
+
*/
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
10
|
+
// @ts-expect-error
|
|
11
|
+
export type FieldValue =
|
|
12
|
+
string | number | boolean | null | undefined | FieldsObject | FieldValue[];
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* A map of fields that can be logged
|
|
16
|
+
*/
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
18
|
+
// @ts-expect-error
|
|
19
|
+
export type FieldsObject = Record<string, FieldValue>;
|
|
20
|
+
|
|
21
|
+
export enum LogLevel {
|
|
22
|
+
Trace = 1,
|
|
23
|
+
Debug,
|
|
24
|
+
Info,
|
|
25
|
+
Warn,
|
|
26
|
+
Error,
|
|
27
|
+
Fatal,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** A level above every other: nothing is logged. */
|
|
31
|
+
export const OFF = LogLevel.Fatal + 1;
|
|
32
|
+
|
|
33
|
+
/** Each level's name, as `ENCORE_LOG` gives it. */
|
|
34
|
+
const LEVELS: Record<LogLevel, string> = {
|
|
35
|
+
[LogLevel.Trace]: 'trace',
|
|
36
|
+
[LogLevel.Debug]: 'debug',
|
|
37
|
+
[LogLevel.Info]: 'info',
|
|
38
|
+
[LogLevel.Warn]: 'warn',
|
|
39
|
+
[LogLevel.Error]: 'error',
|
|
40
|
+
[LogLevel.Fatal]: 'fatal',
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/** The level a name (any case) or `off` stands for; trace otherwise. */
|
|
44
|
+
export function parseLevel(name: string | undefined): LogLevel {
|
|
45
|
+
const v = name?.toLowerCase();
|
|
46
|
+
if (v === 'off') return OFF;
|
|
47
|
+
const found = Object.entries(LEVELS).find(([, l]) => l === v);
|
|
48
|
+
return found ? (Number(found[0]) as LogLevel) : LogLevel.Trace;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** @internal */
|
|
52
|
+
export interface LoggerOptions {
|
|
53
|
+
/** The lowest level logged. */
|
|
54
|
+
level: LogLevel;
|
|
55
|
+
/** The `logger` of every record. */
|
|
56
|
+
name?: string;
|
|
57
|
+
/** Attributes of every record. */
|
|
58
|
+
fields?: FieldsObject;
|
|
59
|
+
/** Where lines go; stderr by default. */
|
|
60
|
+
write?: (line: Buffer) => void;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** A record's parts besides its level and the logger's name. */
|
|
64
|
+
export interface RecordParts {
|
|
65
|
+
/** When the record was made; absent: now. */
|
|
66
|
+
timeUnixMillis?: number;
|
|
67
|
+
message: string;
|
|
68
|
+
/** The span's 25-byte context: trace id, span id, flags. */
|
|
69
|
+
spanCtx?: Buffer;
|
|
70
|
+
exceptionType?: string;
|
|
71
|
+
exceptionMessage?: string;
|
|
72
|
+
/** The stack trace in JavaScript's format. */
|
|
73
|
+
exceptionStacktrace?: string;
|
|
74
|
+
/** The calling function's name. */
|
|
75
|
+
codeFunction?: string;
|
|
76
|
+
/** The caller's source file, line, and column. */
|
|
77
|
+
codeFile?: string;
|
|
78
|
+
codeLine?: number;
|
|
79
|
+
codeColumn?: number;
|
|
80
|
+
/** The runtime module a line of the runtime's own comes from; such a
|
|
81
|
+
* line logs under the runtime's logger, in place of the logger's name. */
|
|
82
|
+
runtimeTarget?: string;
|
|
83
|
+
/** A JSON object of the user's attributes. */
|
|
84
|
+
attrsJson?: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* A structured logger. Each call prints a log record on stderr, one line in
|
|
89
|
+
* the Encore log format, with the logger's name, the logger's and the call's
|
|
90
|
+
* fields as attributes, and the active span's ids. The SDK core encodes the
|
|
91
|
+
* line; it needs the native library, not the runtime. An attribute named
|
|
92
|
+
* like a reserved key is that field when valid, `raw.<key>` otherwise; one
|
|
93
|
+
* named like a key of the record's exception is `raw.<key>`.
|
|
94
|
+
* Lines are written synchronously; none is lost on `process.exit`.
|
|
95
|
+
*/
|
|
96
|
+
export class Logger {
|
|
97
|
+
private readonly level: LogLevel;
|
|
98
|
+
private readonly name: string | undefined;
|
|
99
|
+
private readonly fields: FieldsObject;
|
|
100
|
+
private readonly write: (line: Buffer) => void;
|
|
101
|
+
|
|
102
|
+
/** @internal */
|
|
103
|
+
constructor(options: LoggerOptions) {
|
|
104
|
+
this.level = options.level;
|
|
105
|
+
this.name = options.name;
|
|
106
|
+
this.fields = options.fields ?? {};
|
|
107
|
+
this.write = options.write ?? writeStderr;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Returns a new logger with the specified level.
|
|
112
|
+
*/
|
|
113
|
+
withLevel(level: LogLevel): Logger {
|
|
114
|
+
return new Logger({...this.options(), level});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Returns a new logger named `name`, the `logger` of its records, with
|
|
119
|
+
* this logger's level and fields.
|
|
120
|
+
*/
|
|
121
|
+
withName(name: string): Logger {
|
|
122
|
+
return new Logger({...this.options(), name});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Returns a new logger with the given fields added to the context.
|
|
127
|
+
*/
|
|
128
|
+
with(fields: FieldsObject): Logger {
|
|
129
|
+
return new Logger({
|
|
130
|
+
...this.options(),
|
|
131
|
+
fields: {...this.fields, ...fields},
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Trace logs a message at the trace level.
|
|
137
|
+
*/
|
|
138
|
+
trace(msg: string, fields?: FieldsObject) {
|
|
139
|
+
this.log(LogLevel.Trace, msg, fields);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Debug logs a message at the debug level.
|
|
144
|
+
*/
|
|
145
|
+
debug(msg: string, fields?: FieldsObject) {
|
|
146
|
+
this.log(LogLevel.Debug, msg, fields);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Info logs a message at the info level.
|
|
151
|
+
*/
|
|
152
|
+
info(msg: string, fields?: FieldsObject) {
|
|
153
|
+
this.log(LogLevel.Info, msg, fields);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Warn logs a message at the warn level.
|
|
158
|
+
*/
|
|
159
|
+
warn(err: Error | unknown, fields?: FieldsObject): void;
|
|
160
|
+
warn(err: Error | unknown, msg: string, fields?: FieldsObject): void;
|
|
161
|
+
warn(msg: string, fields?: FieldsObject): void;
|
|
162
|
+
warn(errOrMsg: unknown, msgOrFields?: unknown, fields?: unknown) {
|
|
163
|
+
this.log(LogLevel.Warn, errOrMsg, msgOrFields, fields);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Error logs a message at the error level.
|
|
168
|
+
*/
|
|
169
|
+
error(err: Error | unknown, fields?: FieldsObject): void;
|
|
170
|
+
error(err: Error | unknown, msg: string, fields?: FieldsObject): void;
|
|
171
|
+
error(msg: string, fields?: FieldsObject): void;
|
|
172
|
+
error(errOrMsg: unknown, msgOrFields?: unknown, fields?: unknown) {
|
|
173
|
+
this.log(LogLevel.Error, errOrMsg, msgOrFields, fields);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Fatal logs a message at the fatal level.
|
|
178
|
+
*/
|
|
179
|
+
fatal(err: Error | unknown, fields?: FieldsObject): void;
|
|
180
|
+
fatal(err: Error | unknown, msg: string, fields?: FieldsObject): void;
|
|
181
|
+
fatal(msg: string, fields?: FieldsObject): void;
|
|
182
|
+
fatal(errOrMsg: unknown, msgOrFields?: unknown, fields?: unknown) {
|
|
183
|
+
this.log(LogLevel.Fatal, errOrMsg, msgOrFields, fields);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
private options(): LoggerOptions {
|
|
187
|
+
return {
|
|
188
|
+
level: this.level,
|
|
189
|
+
name: this.name,
|
|
190
|
+
fields: this.fields,
|
|
191
|
+
write: this.write,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
private log(
|
|
196
|
+
level: LogLevel,
|
|
197
|
+
errOrMsg: unknown,
|
|
198
|
+
msgOrFields?: unknown,
|
|
199
|
+
possibleFields?: unknown,
|
|
200
|
+
) {
|
|
201
|
+
if (level < this.level) return;
|
|
202
|
+
|
|
203
|
+
let err: Error | undefined;
|
|
204
|
+
let msg: string;
|
|
205
|
+
let fields: FieldsObject | undefined;
|
|
206
|
+
if (typeof errOrMsg === 'string') {
|
|
207
|
+
// log(msg, fields?)
|
|
208
|
+
msg = errOrMsg;
|
|
209
|
+
fields = msgOrFields as FieldsObject | undefined;
|
|
210
|
+
} else {
|
|
211
|
+
// log(err, msg, fields?) or log(err, fields?)
|
|
212
|
+
if (errOrMsg) {
|
|
213
|
+
err =
|
|
214
|
+
errOrMsg instanceof Error ? errOrMsg : new Error(String(errOrMsg));
|
|
215
|
+
}
|
|
216
|
+
if (typeof msgOrFields === 'string') {
|
|
217
|
+
msg = msgOrFields;
|
|
218
|
+
fields = possibleFields as FieldsObject | undefined;
|
|
219
|
+
} else {
|
|
220
|
+
msg = '';
|
|
221
|
+
fields = msgOrFields as FieldsObject | undefined;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const span = trace.getSpan(activeContext())?.spanContext();
|
|
226
|
+
this.emit(level, {
|
|
227
|
+
message: msg,
|
|
228
|
+
spanCtx: span && isSpanContextValid(span) ? spanBytes(span) : undefined,
|
|
229
|
+
exceptionType: err?.name,
|
|
230
|
+
exceptionMessage: err?.message,
|
|
231
|
+
exceptionStacktrace: err?.stack,
|
|
232
|
+
attrsJson: attrsJson({...this.fields, ...fields}),
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Prints a record of `parts` under the logger's name, at or above the
|
|
238
|
+
* logger's level.
|
|
239
|
+
* @internal
|
|
240
|
+
*/
|
|
241
|
+
emit(level: LogLevel, parts: RecordParts): void {
|
|
242
|
+
if (level < this.level) return;
|
|
243
|
+
try {
|
|
244
|
+
this.write(encode((level - 1) * 4 + 1, this.name || undefined, parts));
|
|
245
|
+
} catch {
|
|
246
|
+
// Logging never throws.
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** A record as a line in the Encore log format; `severity` is the
|
|
252
|
+
* OpenTelemetry severity number. */
|
|
253
|
+
function encode(
|
|
254
|
+
severity: number,
|
|
255
|
+
logger: string | undefined,
|
|
256
|
+
p: RecordParts,
|
|
257
|
+
): Buffer {
|
|
258
|
+
return encodeLog(
|
|
259
|
+
severity,
|
|
260
|
+
p.timeUnixMillis,
|
|
261
|
+
p.message,
|
|
262
|
+
logger,
|
|
263
|
+
p.spanCtx,
|
|
264
|
+
p.exceptionType,
|
|
265
|
+
p.exceptionMessage,
|
|
266
|
+
p.exceptionStacktrace,
|
|
267
|
+
p.codeFunction,
|
|
268
|
+
p.codeFile,
|
|
269
|
+
p.codeLine,
|
|
270
|
+
p.codeColumn,
|
|
271
|
+
p.runtimeTarget,
|
|
272
|
+
p.attrsJson,
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/** `span` in its 25-byte wire form: trace id, span id, flags. */
|
|
277
|
+
function spanBytes(span: SpanContext): Buffer {
|
|
278
|
+
const b = Buffer.allocUnsafe(25);
|
|
279
|
+
b.write(span.traceId, 0, 16, 'hex');
|
|
280
|
+
b.write(span.spanId, 16, 8, 'hex');
|
|
281
|
+
b[24] = span.traceFlags & 0xff;
|
|
282
|
+
return b;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/** `attrs` as a JSON object: bigints as strings, errors as their string,
|
|
286
|
+
* `undefined` left out; a value JSON cannot hold (a cycle) as its string. */
|
|
287
|
+
function attrsJson(attrs: Record<string, unknown>): string {
|
|
288
|
+
try {
|
|
289
|
+
return JSON.stringify(attrs, replacer);
|
|
290
|
+
} catch {
|
|
291
|
+
const out: Record<string, unknown> = {};
|
|
292
|
+
for (const [k, v] of Object.entries(attrs)) {
|
|
293
|
+
try {
|
|
294
|
+
JSON.stringify(v, replacer);
|
|
295
|
+
out[k] = v;
|
|
296
|
+
} catch {
|
|
297
|
+
out[k] = String(v);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return JSON.stringify(out, replacer);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function replacer(_key: string, v: unknown): unknown {
|
|
305
|
+
if (typeof v === 'bigint') return v.toString();
|
|
306
|
+
if (v instanceof Error) return String(v);
|
|
307
|
+
return v;
|
|
308
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {describe, expect, test} from 'bun:test';
|
|
2
|
+
import {Logger, LogLevel} from './logger';
|
|
3
|
+
import {runtimeLinePrinter} from './runtime';
|
|
4
|
+
|
|
5
|
+
function setup() {
|
|
6
|
+
const lines: string[] = [];
|
|
7
|
+
const logger = new Logger({
|
|
8
|
+
level: LogLevel.Trace,
|
|
9
|
+
name: 'app',
|
|
10
|
+
write: line => lines.push(line.toString()),
|
|
11
|
+
});
|
|
12
|
+
return {
|
|
13
|
+
print: runtimeLinePrinter(logger),
|
|
14
|
+
records: () => lines.map(l => JSON.parse(l) as Record<string, unknown>),
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
describe('runtime lines', () => {
|
|
19
|
+
test('level, message, target, fields, and span', () => {
|
|
20
|
+
const {print, records} = setup();
|
|
21
|
+
const ctx = Buffer.from(
|
|
22
|
+
'0102030405060708090a0b0c0d0e0f10' + '0a0b0c0d0e0f1011' + '01',
|
|
23
|
+
'hex',
|
|
24
|
+
);
|
|
25
|
+
print({
|
|
26
|
+
level: 2,
|
|
27
|
+
target: 'encore_sdk_core::pubsub',
|
|
28
|
+
message: 'redelivering',
|
|
29
|
+
fields: '{"attempt":3,"exception.message":"boom","message":7}',
|
|
30
|
+
spanCtx: ctx,
|
|
31
|
+
});
|
|
32
|
+
const {time, ...rest} = records()[0]!;
|
|
33
|
+
expect(typeof time).toBe('string');
|
|
34
|
+
expect(rest).toEqual({
|
|
35
|
+
level: 'warn',
|
|
36
|
+
message: 'redelivering',
|
|
37
|
+
logger: 'encore.runtime',
|
|
38
|
+
trace_id: '0102030405060708090a0b0c0d0e0f10',
|
|
39
|
+
span_id: '0a0b0c0d0e0f1011',
|
|
40
|
+
sampled: true,
|
|
41
|
+
'encore.runtime.target': 'encore_sdk_core::pubsub',
|
|
42
|
+
attempt: 3,
|
|
43
|
+
'exception.message': 'boom',
|
|
44
|
+
'raw.message': 7,
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test('levels map; no span without a context; bad fields kept raw', () => {
|
|
49
|
+
const {print, records} = setup();
|
|
50
|
+
for (const level of [1, 2, 3, 4, 5]) {
|
|
51
|
+
print({level, target: 't', message: 'm', fields: '{}'});
|
|
52
|
+
}
|
|
53
|
+
print({level: 3, target: 't', message: 'm', fields: 'nope'});
|
|
54
|
+
expect(records().map(r => r.level)).toEqual([
|
|
55
|
+
'error',
|
|
56
|
+
'warn',
|
|
57
|
+
'info',
|
|
58
|
+
'debug',
|
|
59
|
+
'trace',
|
|
60
|
+
'info',
|
|
61
|
+
]);
|
|
62
|
+
expect(records()[0]!.trace_id).toBeUndefined();
|
|
63
|
+
expect(records()[5]!['raw.attributes']).toBe('nope');
|
|
64
|
+
});
|
|
65
|
+
});
|
package/log/runtime.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import log from './index';
|
|
2
|
+
import {LogLevel, type Logger} from './logger';
|
|
3
|
+
|
|
4
|
+
/** One runtime log line (the napi `RuntimeLogLine`). */
|
|
5
|
+
export interface RuntimeLine {
|
|
6
|
+
/** 1 ERROR, 2 WARN, 3 INFO, 4 DEBUG, 5 TRACE. */
|
|
7
|
+
level: number;
|
|
8
|
+
/** The module path, or `file:line`, the line comes from. */
|
|
9
|
+
target: string;
|
|
10
|
+
message: string;
|
|
11
|
+
/** A JSON object. */
|
|
12
|
+
fields: string;
|
|
13
|
+
/** 25 bytes: trace id, span id, flags. */
|
|
14
|
+
spanCtx?: Buffer;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const LEVELS: Record<number, LogLevel> = {
|
|
18
|
+
1: LogLevel.Error,
|
|
19
|
+
2: LogLevel.Warn,
|
|
20
|
+
3: LogLevel.Info,
|
|
21
|
+
4: LogLevel.Debug,
|
|
22
|
+
5: LogLevel.Trace,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** Prints a runtime line through `logger`'s level and writer, under the
|
|
26
|
+
* runtime's logger: its target and fields as attributes, its span's ids. */
|
|
27
|
+
export function runtimeLinePrinter(
|
|
28
|
+
logger: Logger,
|
|
29
|
+
): (line: RuntimeLine) => void {
|
|
30
|
+
return line =>
|
|
31
|
+
logger.emit(LEVELS[line.level] ?? LogLevel.Info, {
|
|
32
|
+
message: line.message,
|
|
33
|
+
spanCtx: line.spanCtx,
|
|
34
|
+
runtimeTarget: line.target,
|
|
35
|
+
attrsJson: line.fields,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Prints runtime lines through the default logger. */
|
|
40
|
+
export const printRuntimeLine = runtimeLinePrinter(log);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {expect, test} from 'bun:test';
|
|
2
|
+
|
|
3
|
+
test('lines written before process.exit all reach a piped stderr', async () => {
|
|
4
|
+
const n = 2000;
|
|
5
|
+
const script = `
|
|
6
|
+
import log from ${JSON.stringify(`${import.meta.dir}/index.ts`)};
|
|
7
|
+
const pad = "x".repeat(1000);
|
|
8
|
+
for (let i = 0; i < ${n}; i++) log.info("line", { i, pad });
|
|
9
|
+
process.exit(0);
|
|
10
|
+
`;
|
|
11
|
+
const proc = Bun.spawn([process.execPath, '-e', script], {
|
|
12
|
+
stdout: 'ignore',
|
|
13
|
+
stderr: 'pipe',
|
|
14
|
+
env: {...process.env, ENCORE_LOG: 'info'},
|
|
15
|
+
});
|
|
16
|
+
// Read late: the pipe fills, and writes wait for it.
|
|
17
|
+
await new Promise(r => setTimeout(r, 200));
|
|
18
|
+
const out = await new Response(proc.stderr).text();
|
|
19
|
+
expect(await proc.exited).toBe(0);
|
|
20
|
+
const lines = out.trimEnd().split('\n');
|
|
21
|
+
expect(lines).toHaveLength(n);
|
|
22
|
+
expect(JSON.parse(lines[n - 1]!).i).toBe(n - 1);
|
|
23
|
+
});
|
package/log/stderr.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import {writeSync} from 'node:fs';
|
|
2
|
+
|
|
3
|
+
const STDERR = 2;
|
|
4
|
+
const sleep = new Int32Array(new SharedArrayBuffer(4));
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Writes `buf` to stderr synchronously, so no line is lost on
|
|
8
|
+
* `process.exit` or a crash. `process.stderr.write` is asynchronous for pipes
|
|
9
|
+
* on POSIX under Node, and the process manager reads stderr through one.
|
|
10
|
+
* When the pipe is full (EAGAIN on a non-blocking descriptor) the write is
|
|
11
|
+
* retried, blocking the caller like a blocking write would; other errors
|
|
12
|
+
* drop the line.
|
|
13
|
+
*/
|
|
14
|
+
export function writeStderr(buf: Uint8Array): void {
|
|
15
|
+
let off = 0;
|
|
16
|
+
while (off < buf.length) {
|
|
17
|
+
try {
|
|
18
|
+
off += writeSync(STDERR, buf, off);
|
|
19
|
+
} catch (err) {
|
|
20
|
+
if ((err as NodeJS.ErrnoException).code !== 'EAGAIN') return;
|
|
21
|
+
Atomics.wait(sleep, 0, 0, 1);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|