fix(base-driver): Use WeakRef to reference the driver instance in the log prefix generator (#16636)

This commit is contained in:
Mykola Mokhnach
2022-03-23 18:48:33 +01:00
committed by GitHub
parent 21d337f976
commit bbfc7ef51d
2 changed files with 16 additions and 5 deletions
+1 -1
View File
@@ -66,7 +66,7 @@ class AppiumDriver extends BaseDriver {
*/
get log () {
if (!this._log) {
const instanceName = `${this.constructor.name}@${node.getObjectId(this).substring(0, 8)}`;
const instanceName = `${this.constructor.name}@${node.getObjectId(this).substring(0, 4)}`;
this._log = logger.getLogger(instanceName);
}
return this._log;
+15 -4
View File
@@ -108,10 +108,21 @@ class BaseDriver extends Protocol {
get log () {
if (!this._log) {
const instanceName = `${this.constructor.name}@${node.getObjectId(this).substring(0, 8)}`;
this._log = logger.getLogger(() =>
this.sessionId ? `${instanceName} (${this.sessionId.substring(0, 8)})` : instanceName
);
const instanceName = `${this.constructor.name}@${node.getObjectId(this).substring(0, 4)}`;
// We don't want the self reference to be captured inside the below closure
// to avoid possible memory leaks,
// but NodeJS started supporting WeakRef only since v. 14.6
const self = global.WeakRef ? new global.WeakRef(this) : this;
this._log = logger.getLogger(() => {
let sessionId = self?.sessionId;
if (!sessionId && _.isFunction(self.deref)) {
const ref = self.deref();
if (ref?.sessionId) {
sessionId = ref.sessionId;
}
}
return sessionId ? `${instanceName} (${sessionId.substring(0, 8)})` : instanceName;
});
}
return this._log;