diff --git a/packages/appium/lib/appium.js b/packages/appium/lib/appium.js index 8c6e775ea..82f0da473 100644 --- a/packages/appium/lib/appium.js +++ b/packages/appium/lib/appium.js @@ -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; diff --git a/packages/base-driver/lib/basedriver/driver.js b/packages/base-driver/lib/basedriver/driver.js index 9d9e30119..8b6f60a50 100644 --- a/packages/base-driver/lib/basedriver/driver.js +++ b/packages/base-driver/lib/basedriver/driver.js @@ -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;