Skip to content

Commit d94fd2b

Browse files
committed
used private fields
1 parent f76726c commit d94fd2b

File tree

1 file changed

+52
-55
lines changed

1 file changed

+52
-55
lines changed

lib/logger.js

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const {
77
ObjectAssign,
88
ObjectKeys,
99
SafeSet,
10-
Symbol,
1110
} = primordials;
1211

1312
const {
@@ -53,18 +52,6 @@ const LEVELS = {
5352

5453
const LEVEL_NAMES = ObjectKeys(LEVELS);
5554

56-
const kLevel = Symbol('level');
57-
const kLevelValue = Symbol('levelValue');
58-
const kBindings = Symbol('bindings');
59-
const kFields = Symbol('fields');
60-
const kStream = Symbol('stream');
61-
const kHandleLog = Symbol('handleLog');
62-
const kCreateStream = Symbol('createStream');
63-
const kSetLogMethods = Symbol('setLogMethods');
64-
const kIsError = Symbol('isError');
65-
const kLog = Symbol('log');
66-
const kSerializeError = Symbol('serializeError');
67-
6855
// Noop function for disabled log levels
6956
function noop() {}
7057

@@ -73,15 +60,18 @@ function noop() {}
7360
* Consumers subscribe to diagnostics_channel events
7461
*/
7562
class LogConsumer {
63+
#level;
64+
#levelValue;
65+
7666
constructor(options = kEmptyObject) {
7767
validateObject(options, 'options');
7868
const { level = 'info' } = options;
7969
validateString(level, 'options.level');
8070

8171
validateOneOf(level, 'options.level', LEVEL_NAMES);
8272

83-
this[kLevel] = level;
84-
this[kLevelValue] = LEVELS[level];
73+
this.#level = level;
74+
this.#levelValue = LEVELS[level];
8575
}
8676

8777
/**
@@ -90,7 +80,7 @@ class LogConsumer {
9080
* @returns {boolean}
9181
*/
9282
enabled(level) {
93-
return LEVELS[level] >= this[kLevelValue];
83+
return LEVELS[level] >= this.#levelValue;
9484
}
9585

9686
/**
@@ -99,7 +89,7 @@ class LogConsumer {
9989
attach() {
10090
for (const level of LEVEL_NAMES) {
10191
if (this.enabled(level)) {
102-
channels[level].subscribe(this[kHandleLog].bind(this, level));
92+
channels[level].subscribe(this.#handleLog.bind(this, level));
10393
}
10494
}
10595
}
@@ -110,7 +100,7 @@ class LogConsumer {
110100
* @param {object} record
111101
* @private
112102
*/
113-
[kHandleLog](level, record) {
103+
#handleLog(level, record) {
114104
this.handle(record);
115105
}
116106

@@ -127,6 +117,9 @@ class LogConsumer {
127117
* JSON consumer - outputs structured JSON logs
128118
*/
129119
class JSONConsumer extends LogConsumer {
120+
#stream;
121+
#fields;
122+
130123
constructor(options = kEmptyObject) {
131124
super(options);
132125
const {
@@ -136,12 +129,12 @@ class JSONConsumer extends LogConsumer {
136129

137130
validateObject(fields, 'options.fields');
138131

139-
this[kStream] = stream ? this[kCreateStream](stream) :
132+
this.#stream = stream ? this.#createStream(stream) :
140133
new Utf8Stream({ fd: 1 });
141-
this[kFields] = fields;
134+
this.#fields = fields;
142135
}
143136

144-
[kCreateStream](stream) {
137+
#createStream(stream) {
145138
// Fast path: already a Utf8Stream
146139
if (stream instanceof Utf8Stream) {
147140
return stream;
@@ -175,42 +168,46 @@ class JSONConsumer extends LogConsumer {
175168
level: record.level,
176169
time: record.time,
177170
msg: record.msg,
178-
...this[kFields],
171+
...this.#fields,
179172
...record.bindings,
180173
...record.fields,
181174
};
182175

183176
const json = JSONStringify(logObj) + '\n';
184-
this[kStream].write(json);
177+
this.#stream.write(json);
185178
}
186179

187180
/**
188181
* Flush pending writes
189182
* @param {Function} callback
190183
*/
191184
flush(callback) {
192-
this[kStream].flush(callback);
185+
this.#stream.flush(callback);
193186
}
194187

195188
/**
196189
* Flush pending writes synchronously
197190
*/
198191
flushSync() {
199-
this[kStream].flushSync();
192+
this.#stream.flushSync();
200193
}
201194

202195
/**
203196
* Close the consumer
204197
*/
205198
end() {
206-
this[kStream].end();
199+
this.#stream.end();
207200
}
208201
}
209202

210203
/**
211204
* Logger class
212205
*/
213206
class Logger {
207+
#level;
208+
#levelValue;
209+
#bindings;
210+
214211
constructor(options = kEmptyObject) {
215212
validateObject(options, 'options');
216213
const { level = 'info', bindings = kEmptyObject } = options;
@@ -226,19 +223,19 @@ class Logger {
226223

227224
validateObject(bindings, 'options.bindings');
228225

229-
this[kLevel] = level;
230-
this[kLevelValue] = LEVELS[level];
231-
this[kBindings] = bindings;
226+
this.#level = level;
227+
this.#levelValue = LEVELS[level];
228+
this.#bindings = bindings;
232229

233-
this[kSetLogMethods]();
230+
this.#setLogMethods();
234231
}
235232

236233
/**
237234
* Replace disabled log methods with noop for performance
238235
* @private
239236
*/
240-
[kSetLogMethods]() {
241-
const levelValue = this[kLevelValue];
237+
#setLogMethods() {
238+
const levelValue = this.#levelValue;
242239

243240
if (levelValue > LEVELS.trace) this.trace = noop;
244241
if (levelValue > LEVELS.debug) this.debug = noop;
@@ -254,7 +251,7 @@ class Logger {
254251
* @returns {boolean}
255252
*/
256253
enabled(level) {
257-
return LEVELS[level] >= this[kLevelValue];
254+
return LEVELS[level] >= this.#levelValue;
258255
}
259256

260257
/**
@@ -269,12 +266,12 @@ class Logger {
269266

270267
const mergedBindings = ObjectAssign(
271268
{ __proto__: null },
272-
this[kBindings],
269+
this.#bindings,
273270
bindings,
274271
);
275272

276273
return new Logger({
277-
level: options.level || this[kLevel],
274+
level: options.level || this.#level,
278275
bindings: mergedBindings,
279276
});
280277
}
@@ -285,7 +282,7 @@ class Logger {
285282
* @returns {boolean}
286283
* @private
287284
*/
288-
[kIsError](value) {
285+
#isError(value) {
289286
return Error.isError(value);
290287
}
291288

@@ -296,16 +293,16 @@ class Logger {
296293
* @param {string|object} msgOrObj - Message string or object with msg property
297294
* @param {object} [fields] - Optional fields to merge
298295
*/
299-
[kLog](level, levelValue, msgOrObj, fields) {
300-
if (levelValue < this[kLevelValue]) {
296+
#log(level, levelValue, msgOrObj, fields) {
297+
if (levelValue < this.#levelValue) {
301298
return;
302299
}
303300

304301
if (typeof msgOrObj === 'string') {
305302
if (fields !== undefined) {
306303
validateObject(fields, 'fields');
307304
}
308-
} else if (!this[kIsError](msgOrObj)) {
305+
} else if (!this.#isError(msgOrObj)) {
309306
validateObject(msgOrObj, 'obj');
310307
validateString(msgOrObj.msg, 'obj.msg');
311308
}
@@ -318,10 +315,10 @@ class Logger {
318315
let msg;
319316
let logFields;
320317

321-
if (this[kIsError](msgOrObj)) {
318+
if (this.#isError(msgOrObj)) {
322319
msg = msgOrObj.message;
323320
logFields = {
324-
err: this[kSerializeError](msgOrObj),
321+
err: this.#serializeError(msgOrObj),
325322
...fields,
326323
};
327324
} else if (typeof msgOrObj === 'string') {
@@ -332,20 +329,20 @@ class Logger {
332329
msg = extractedMsg;
333330
logFields = restFields;
334331

335-
if (logFields.err && this[kIsError](logFields.err)) {
336-
logFields.err = this[kSerializeError](logFields.err);
332+
if (logFields.err && this.#isError(logFields.err)) {
333+
logFields.err = this.#serializeError(logFields.err);
337334
}
338335

339-
if (logFields.error && this[kIsError](logFields.error)) {
340-
logFields.error = this[kSerializeError](logFields.error);
336+
if (logFields.error && this.#isError(logFields.error)) {
337+
logFields.error = this.#serializeError(logFields.error);
341338
}
342339
}
343340

344341
const record = {
345342
level,
346343
msg,
347344
time: DateNow(),
348-
bindings: this[kBindings],
345+
bindings: this.#bindings,
349346
fields: logFields,
350347
};
351348

@@ -359,7 +356,7 @@ class Logger {
359356
* @returns {object} Serialized error object
360357
* @private
361358
*/
362-
[kSerializeError](err, seen = new SafeSet()) {
359+
#serializeError(err, seen = new SafeSet()) {
363360
if (seen.has(err)) {
364361
return '[Circular]';
365362
}
@@ -376,8 +373,8 @@ class Logger {
376373
}
377374

378375
if (err.cause !== undefined) {
379-
serialized.cause = this[kIsError](err.cause) ?
380-
this[kSerializeError](err.cause, seen) :
376+
serialized.cause = this.#isError(err.cause) ?
377+
this.#serializeError(err.cause, seen) :
381378
err.cause;
382379
}
383380

@@ -391,27 +388,27 @@ class Logger {
391388
}
392389

393390
trace(msgOrObj, fields) {
394-
this[kLog]('trace', LEVELS.trace, msgOrObj, fields);
391+
this.#log('trace', LEVELS.trace, msgOrObj, fields);
395392
}
396393

397394
debug(msgOrObj, fields) {
398-
this[kLog]('debug', LEVELS.debug, msgOrObj, fields);
395+
this.#log('debug', LEVELS.debug, msgOrObj, fields);
399396
}
400397

401398
info(msgOrObj, fields) {
402-
this[kLog]('info', LEVELS.info, msgOrObj, fields);
399+
this.#log('info', LEVELS.info, msgOrObj, fields);
403400
}
404401

405402
warn(msgOrObj, fields) {
406-
this[kLog]('warn', LEVELS.warn, msgOrObj, fields);
403+
this.#log('warn', LEVELS.warn, msgOrObj, fields);
407404
}
408405

409406
error(msgOrObj, fields) {
410-
this[kLog]('error', LEVELS.error, msgOrObj, fields);
407+
this.#log('error', LEVELS.error, msgOrObj, fields);
411408
}
412409

413410
fatal(msgOrObj, fields) {
414-
this[kLog]('fatal', LEVELS.fatal, msgOrObj, fields);
411+
this.#log('fatal', LEVELS.fatal, msgOrObj, fields);
415412
}
416413
}
417414

0 commit comments

Comments
 (0)