mirror of
https://github.com/appium/appium.git
synced 2026-10-29 12:41:58 +08:00
fix(base-driver): Use WeakRef to reference the driver instance in the log prefix generator (#16636)
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user