Skip to content

Commit f1abaad

Browse files
committed
git commit -m "make internal properties and methods truly private
1 parent f2aa86d commit f1abaad

File tree

1 file changed

+55
-42
lines changed

1 file changed

+55
-42
lines changed

lib/logger.js

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
ObjectKeys,
99
ObjectPrototypeToString,
1010
SafeSet,
11+
Symbol,
1112
} = primordials;
1213

1314
const {
@@ -53,6 +54,18 @@ const LEVELS = {
5354

5455
const LEVEL_NAMES = ObjectKeys(LEVELS);
5556

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

@@ -68,8 +81,8 @@ class LogConsumer {
6881

6982
validateOneOf(level, 'options.level', LEVEL_NAMES);
7083

71-
this._level = level;
72-
this._levelValue = LEVELS[level];
84+
this[kLevel] = level;
85+
this[kLevelValue] = LEVELS[level];
7386
}
7487

7588
/**
@@ -78,7 +91,7 @@ class LogConsumer {
7891
* @returns {boolean}
7992
*/
8093
enabled(level) {
81-
return LEVELS[level] >= this._levelValue;
94+
return LEVELS[level] >= this[kLevelValue];
8295
}
8396

8497
/**
@@ -87,7 +100,7 @@ class LogConsumer {
87100
attach() {
88101
for (const level of LEVEL_NAMES) {
89102
if (this.enabled(level)) {
90-
channels[level].subscribe(this._handleLog.bind(this, level));
103+
channels[level].subscribe(this[kHandleLog].bind(this, level));
91104
}
92105
}
93106
}
@@ -98,7 +111,7 @@ class LogConsumer {
98111
* @param {object} record
99112
* @private
100113
*/
101-
_handleLog(level, record) {
114+
[kHandleLog](level, record) {
102115
this.handle(record);
103116
}
104117

@@ -124,12 +137,12 @@ class JSONConsumer extends LogConsumer {
124137

125138
validateObject(fields, 'options.fields');
126139

127-
this._stream = stream ? this._createStream(stream) :
140+
this[kStream] = stream ? this[kCreateStream](stream) :
128141
new Utf8Stream({ fd: 1 });
129-
this._fields = fields;
142+
this[kFields] = fields;
130143
}
131144

132-
_createStream(stream) {
145+
[kCreateStream](stream) {
133146
// Fast path: already a Utf8Stream
134147
if (stream instanceof Utf8Stream) {
135148
return stream;
@@ -163,35 +176,35 @@ class JSONConsumer extends LogConsumer {
163176
level: record.level,
164177
time: record.time,
165178
msg: record.msg,
166-
...this._fields,
179+
...this[kFields],
167180
...record.bindings,
168181
...record.fields,
169182
};
170183

171184
const json = JSONStringify(logObj) + '\n';
172-
this._stream.write(json);
185+
this[kStream].write(json);
173186
}
174187

175188
/**
176189
* Flush pending writes
177190
* @param {Function} callback
178191
*/
179192
flush(callback) {
180-
this._stream.flush(callback);
193+
this[kStream].flush(callback);
181194
}
182195

183196
/**
184197
* Flush pending writes synchronously
185198
*/
186199
flushSync() {
187-
this._stream.flushSync();
200+
this[kStream].flushSync();
188201
}
189202

190203
/**
191204
* Close the consumer
192205
*/
193206
end() {
194-
this._stream.end();
207+
this[kStream].end();
195208
}
196209
}
197210

@@ -214,19 +227,19 @@ class Logger {
214227

215228
validateObject(bindings, 'options.bindings');
216229

217-
this._level = level;
218-
this._levelValue = LEVELS[level];
219-
this._bindings = bindings;
230+
this[kLevel] = level;
231+
this[kLevelValue] = LEVELS[level];
232+
this[kBindings] = bindings;
220233

221-
this._setLogMethods();
234+
this[kSetLogMethods]();
222235
}
223236

224237
/**
225238
* Replace disabled log methods with noop for performance
226239
* @private
227240
*/
228-
_setLogMethods() {
229-
const levelValue = this._levelValue;
241+
[kSetLogMethods]() {
242+
const levelValue = this[kLevelValue];
230243

231244
if (levelValue > LEVELS.trace) this.trace = noop;
232245
if (levelValue > LEVELS.debug) this.debug = noop;
@@ -242,7 +255,7 @@ class Logger {
242255
* @returns {boolean}
243256
*/
244257
enabled(level) {
245-
return LEVELS[level] >= this._levelValue;
258+
return LEVELS[level] >= this[kLevelValue];
246259
}
247260

248261
/**
@@ -257,12 +270,12 @@ class Logger {
257270

258271
const mergedBindings = ObjectAssign(
259272
{ __proto__: null },
260-
this._bindings,
273+
this[kBindings],
261274
bindings,
262275
);
263276

264277
return new Logger({
265-
level: options.level || this._level,
278+
level: options.level || this[kLevel],
266279
bindings: mergedBindings,
267280
});
268281
}
@@ -273,7 +286,7 @@ class Logger {
273286
* @returns {boolean}
274287
* @private
275288
*/
276-
_isError(value) {
289+
[kIsError](value) {
277290
// TODO(@mertcanaltin): Use ErrorIsError from primordials when available
278291
// For now, use manual check until ErrorIsError is added to primordials
279292

@@ -290,16 +303,16 @@ class Logger {
290303
* @param {string|object} msgOrObj - Message string or object with msg property
291304
* @param {object} [fields] - Optional fields to merge
292305
*/
293-
_log(level, levelValue, msgOrObj, fields) {
294-
if (levelValue < this._levelValue) {
306+
[kLog](level, levelValue, msgOrObj, fields) {
307+
if (levelValue < this[kLevelValue]) {
295308
return;
296309
}
297310

298311
if (typeof msgOrObj === 'string') {
299312
if (fields !== undefined) {
300313
validateObject(fields, 'fields');
301314
}
302-
} else if (!this._isError(msgOrObj)) {
315+
} else if (!this[kIsError](msgOrObj)) {
303316
validateObject(msgOrObj, 'obj');
304317
validateString(msgOrObj.msg, 'obj.msg');
305318
}
@@ -312,10 +325,10 @@ class Logger {
312325
let msg;
313326
let logFields;
314327

315-
if (this._isError(msgOrObj)) {
328+
if (this[kIsError](msgOrObj)) {
316329
msg = msgOrObj.message;
317330
logFields = {
318-
err: this._serializeError(msgOrObj),
331+
err: this[kSerializeError](msgOrObj),
319332
...fields,
320333
};
321334
} else if (typeof msgOrObj === 'string') {
@@ -326,20 +339,20 @@ class Logger {
326339
msg = extractedMsg;
327340
logFields = restFields;
328341

329-
if (logFields.err && this._isError(logFields.err)) {
330-
logFields.err = this._serializeError(logFields.err);
342+
if (logFields.err && this[kIsError](logFields.err)) {
343+
logFields.err = this[kSerializeError](logFields.err);
331344
}
332345

333-
if (logFields.error && this._isError(logFields.error)) {
334-
logFields.error = this._serializeError(logFields.error);
346+
if (logFields.error && this[kIsError](logFields.error)) {
347+
logFields.error = this[kSerializeError](logFields.error);
335348
}
336349
}
337350

338351
const record = {
339352
level,
340353
msg,
341354
time: DateNow(),
342-
bindings: this._bindings,
355+
bindings: this[kBindings],
343356
fields: logFields,
344357
};
345358

@@ -353,7 +366,7 @@ class Logger {
353366
* @returns {object} Serialized error object
354367
* @private
355368
*/
356-
_serializeError(err, seen = new SafeSet()) {
369+
[kSerializeError](err, seen = new SafeSet()) {
357370
if (seen.has(err)) {
358371
return '[Circular]';
359372
}
@@ -370,8 +383,8 @@ class Logger {
370383
}
371384

372385
if (err.cause !== undefined) {
373-
serialized.cause = this._isError(err.cause) ?
374-
this._serializeError(err.cause, seen) :
386+
serialized.cause = this[kIsError](err.cause) ?
387+
this[kSerializeError](err.cause, seen) :
375388
err.cause;
376389
}
377390

@@ -385,27 +398,27 @@ class Logger {
385398
}
386399

387400
trace(msgOrObj, fields) {
388-
this._log('trace', LEVELS.trace, msgOrObj, fields);
401+
this[kLog]('trace', LEVELS.trace, msgOrObj, fields);
389402
}
390403

391404
debug(msgOrObj, fields) {
392-
this._log('debug', LEVELS.debug, msgOrObj, fields);
405+
this[kLog]('debug', LEVELS.debug, msgOrObj, fields);
393406
}
394407

395408
info(msgOrObj, fields) {
396-
this._log('info', LEVELS.info, msgOrObj, fields);
409+
this[kLog]('info', LEVELS.info, msgOrObj, fields);
397410
}
398411

399412
warn(msgOrObj, fields) {
400-
this._log('warn', LEVELS.warn, msgOrObj, fields);
413+
this[kLog]('warn', LEVELS.warn, msgOrObj, fields);
401414
}
402415

403416
error(msgOrObj, fields) {
404-
this._log('error', LEVELS.error, msgOrObj, fields);
417+
this[kLog]('error', LEVELS.error, msgOrObj, fields);
405418
}
406419

407420
fatal(msgOrObj, fields) {
408-
this._log('fatal', LEVELS.fatal, msgOrObj, fields);
421+
this[kLog]('fatal', LEVELS.fatal, msgOrObj, fields);
409422
}
410423
}
411424

0 commit comments

Comments
 (0)