mirror of
https://github.com/appium/appium.git
synced 2026-02-22 03:08:47 -06:00
feat(logger): Use LRUCache to manage log history (#20325)
This commit is contained in:
17
package-lock.json
generated
17
package-lock.json
generated
@@ -22150,6 +22150,7 @@
|
||||
"dependencies": {
|
||||
"console-control-strings": "1.1.0",
|
||||
"lodash": "4.17.21",
|
||||
"lru-cache": "10.3.0",
|
||||
"set-blocking": "2.0.0"
|
||||
},
|
||||
"engines": {
|
||||
@@ -22157,6 +22158,14 @@
|
||||
"npm": ">=8"
|
||||
}
|
||||
},
|
||||
"packages/logger/node_modules/lru-cache": {
|
||||
"version": "10.3.0",
|
||||
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.3.0.tgz",
|
||||
"integrity": "sha512-CQl19J/g+Hbjbv4Y3mFNNXFEL/5t/KCg8POCuUqd4rMKjGG+j1ybER83hxV58zL+dFI1PTkt3GNFSHRt+d8qEQ==",
|
||||
"engines": {
|
||||
"node": "14 || >=16.14"
|
||||
}
|
||||
},
|
||||
"packages/opencv": {
|
||||
"name": "@appium/opencv",
|
||||
"version": "3.0.4",
|
||||
@@ -23142,7 +23151,15 @@
|
||||
"requires": {
|
||||
"console-control-strings": "1.1.0",
|
||||
"lodash": "4.17.21",
|
||||
"lru-cache": "10.3.0",
|
||||
"set-blocking": "2.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"lru-cache": {
|
||||
"version": "10.3.0",
|
||||
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.3.0.tgz",
|
||||
"integrity": "sha512-CQl19J/g+Hbjbv4Y3mFNNXFEL/5t/KCg8POCuUqd4rMKjGG+j1ybER83hxV58zL+dFI1PTkt3GNFSHRt+d8qEQ=="
|
||||
}
|
||||
}
|
||||
},
|
||||
"@appium/opencv": {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import _ from 'lodash';
|
||||
import {EventEmitter} from 'node:events';
|
||||
// @ts-ignore This module does not provide type definitons
|
||||
// @ts-ignore This module does not provide type definitions
|
||||
import setBlocking from 'set-blocking';
|
||||
// @ts-ignore This module does not provide type definitons
|
||||
// @ts-ignore This module does not provide type definitions
|
||||
import consoleControl from 'console-control-strings';
|
||||
import * as util from 'node:util';
|
||||
import type {
|
||||
@@ -17,6 +17,7 @@ import type {Writable} from 'node:stream';
|
||||
import {AsyncLocalStorage} from 'node:async_hooks';
|
||||
import { unleakString } from './utils';
|
||||
import { SecureValuesPreprocessor } from './secure-values-preprocessor';
|
||||
import { LRUCache } from 'lru-cache';
|
||||
|
||||
const DEFAULT_LOG_LEVELS = [
|
||||
['silly', -Infinity, {inverse: true}, 'sill'],
|
||||
@@ -30,13 +31,12 @@ const DEFAULT_LOG_LEVELS = [
|
||||
['error', 5000, {fg: 'red', bg: 'black'}, 'ERR!'],
|
||||
['silent', Infinity],
|
||||
] as const;
|
||||
const DEFAULT_HISTORY_SIZE = 10000;
|
||||
|
||||
setBlocking(true);
|
||||
|
||||
export class Log extends EventEmitter implements Logger {
|
||||
level: LogLevel | string;
|
||||
record: MessageObject[];
|
||||
maxRecordSize: number;
|
||||
prefixStyle: StyleObject;
|
||||
headingStyle: StyleObject;
|
||||
heading: string;
|
||||
@@ -52,13 +52,16 @@ export class Log extends EventEmitter implements Logger {
|
||||
_paused: boolean;
|
||||
_secureValuesPreprocessor: SecureValuesPreprocessor;
|
||||
|
||||
private _history: LRUCache<number, MessageObject>;
|
||||
private _maxRecordSize: number;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.level = 'info';
|
||||
this._buffer = [];
|
||||
this.record = [];
|
||||
this.maxRecordSize = 10000;
|
||||
this._maxRecordSize = DEFAULT_HISTORY_SIZE;
|
||||
this._history = new LRUCache({max: this.maxRecordSize});
|
||||
this.stream = process.stderr;
|
||||
this.heading = '';
|
||||
this.prefixStyle = {fg: 'magenta'};
|
||||
@@ -77,6 +80,27 @@ export class Log extends EventEmitter implements Logger {
|
||||
this.on('error', () => {});
|
||||
}
|
||||
|
||||
get record(): MessageObject[] {
|
||||
return [...this._history.rvalues()] as MessageObject[];
|
||||
}
|
||||
|
||||
get maxRecordSize(): number {
|
||||
return this._maxRecordSize;
|
||||
}
|
||||
|
||||
set maxRecordSize(value: number) {
|
||||
if (value === this._maxRecordSize) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._maxRecordSize = value;
|
||||
const newHistory = new LRUCache<number, MessageObject>({max: value});
|
||||
for (const [key, value] of this._history.rentries() as Generator<[number, MessageObject]>) {
|
||||
newHistory.set(key, value);
|
||||
}
|
||||
this._history = newHistory;
|
||||
}
|
||||
|
||||
private useColor(): boolean {
|
||||
// by default, decide based on tty-ness.
|
||||
return (
|
||||
@@ -239,12 +263,7 @@ export class Log extends EventEmitter implements Logger {
|
||||
this.emit(m.prefix, m);
|
||||
}
|
||||
|
||||
this.record.push(m);
|
||||
const mrs = this.maxRecordSize;
|
||||
if (this.record.length > mrs) {
|
||||
this.record.shift();
|
||||
}
|
||||
|
||||
this._history.set(m.id, m);
|
||||
this.emitLog(m);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
"dependencies": {
|
||||
"console-control-strings": "1.1.0",
|
||||
"lodash": "4.17.21",
|
||||
"lru-cache": "10.3.0",
|
||||
"set-blocking": "2.0.0"
|
||||
},
|
||||
"license": "ISC",
|
||||
|
||||
@@ -363,7 +363,24 @@ describe('basic', async function () {
|
||||
log.log('verbose', 'test', 'log 2');
|
||||
log.log('verbose', 'test', 'log 3');
|
||||
log.log('verbose', 'test', 'log 4');
|
||||
log.record.length.should.equal(3);
|
||||
log.record.map(({message}) => message).should.eql([
|
||||
'log 2',
|
||||
'log 3',
|
||||
'log 4',
|
||||
]);
|
||||
log.maxRecordSize = 2;
|
||||
log.log('verbose', 'test', 'log 5');
|
||||
log.record.map(({message}) => message).should.eql([
|
||||
'log 4',
|
||||
'log 5',
|
||||
]);
|
||||
log.maxRecordSize = 3;
|
||||
log.log('verbose', 'test', 'log 6');
|
||||
log.record.map(({message}) => message).should.eql([
|
||||
'log 4',
|
||||
'log 5',
|
||||
'log 6',
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user