arcway 0.1.5 → 0.1.6
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/package.json +1 -1
- package/server/context.js +1 -1
- package/server/logger/index.js +10 -0
- package/server/router/api-router.js +2 -0
package/package.json
CHANGED
package/server/context.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
function trackDb(db) {
|
|
2
|
-
if (!db) return { db, stats: { queries: 0, durationMs: 0 } };
|
|
2
|
+
if (!db || typeof db !== 'function' && typeof db !== 'object') return { db, stats: { queries: 0, durationMs: 0 } };
|
|
3
3
|
const stats = { queries: 0, durationMs: 0 };
|
|
4
4
|
const handler = {
|
|
5
5
|
get(target, prop, receiver) {
|
package/server/logger/index.js
CHANGED
|
@@ -112,6 +112,15 @@ class Logger {
|
|
|
112
112
|
return this.query({ level: 'error', limit, since });
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
+
addContext(fields) {
|
|
116
|
+
if (!this._canonicalContext) this._canonicalContext = {};
|
|
117
|
+
Object.assign(this._canonicalContext, fields);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
getCanonicalContext() {
|
|
121
|
+
return this._canonicalContext ?? null;
|
|
122
|
+
}
|
|
123
|
+
|
|
115
124
|
extend(fields) {
|
|
116
125
|
const child = Object.create(Logger.prototype);
|
|
117
126
|
const hasFields = Object.keys(fields).length > 0;
|
|
@@ -123,6 +132,7 @@ class Logger {
|
|
|
123
132
|
child._root = this._root ?? this;
|
|
124
133
|
child._base = null;
|
|
125
134
|
child._ring = null;
|
|
135
|
+
child._canonicalContext = null;
|
|
126
136
|
return child;
|
|
127
137
|
}
|
|
128
138
|
}
|
|
@@ -149,6 +149,7 @@ class ApiRouter {
|
|
|
149
149
|
_emitCanonicalLog(ctx, { method, path, route, status, startTime, middlewareNames, error, session }) {
|
|
150
150
|
const durationMs = Date.now() - startTime;
|
|
151
151
|
const dbStats = ctx._dbStats ?? { queries: 0, durationMs: 0 };
|
|
152
|
+
const extra = ctx.log.getCanonicalContext?.() ?? {};
|
|
152
153
|
const line = {
|
|
153
154
|
method,
|
|
154
155
|
path,
|
|
@@ -158,6 +159,7 @@ class ApiRouter {
|
|
|
158
159
|
dbQueries: dbStats.queries,
|
|
159
160
|
dbDurationMs: Math.round(dbStats.durationMs),
|
|
160
161
|
middleware: middlewareNames,
|
|
162
|
+
...extra,
|
|
161
163
|
};
|
|
162
164
|
if (session?.userId ?? session?.id) {
|
|
163
165
|
line.userId = session.userId ?? session.id;
|