Skip to content

Commit a4b6bc2

Browse files
authored
feat(logs): user-selectable logs limit (#1999)
* feat(logs): user-selectable logs limit When viewing debug logs, the current buffer limit of a 100 is full pretty quick. I'm adding an option for the user to choose a bigger limit (up to a 1000). * perf(logs): extracted var to optimize a loop https://github.com/nurikk/zigbee2mqtt-frontend/pull/1999/commits/ 9bca1ad#r1574572268
1 parent cc4e080 commit a4b6bc2

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

src/components/logs-page/index.tsx

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Component } from 'react';
22
import { connect } from 'unistore/react';
33
import actions, { UtilsApi } from '../../actions/actions';
4-
import { GlobalState, LogMessage } from '../../store';
4+
import store, { GlobalState, LogMessage } from '../../store';
55
import cx from 'classnames';
66
import escapeRegExp from 'lodash/escapeRegExp';
77
import { BridgeApi } from '../../actions/BridgeApi';
@@ -71,6 +71,7 @@ export function LogRow(props: LogRowProps): JSX.Element {
7171
}
7272

7373
const logLevels = [ALL, 'debug', 'info', 'warning', 'error'];
74+
const logLimits = [100, 200, 500, 1000];
7475

7576
type PropsFromStore = Pick<GlobalState, 'bridgeInfo' | 'logs'>;
7677
class LogsPage extends Component<PropsFromStore & BridgeApi & UtilsApi & WithTranslation<'logs'>, LogsPageState> {
@@ -83,6 +84,7 @@ class LogsPage extends Component<PropsFromStore & BridgeApi & UtilsApi & WithTra
8384
t,
8485
} = this.props;
8586
const { search } = this.state;
87+
const { logsLimit } = store.getState();
8688
return (
8789
<div className="card">
8890
<div className="card-body">
@@ -125,6 +127,28 @@ class LogsPage extends Component<PropsFromStore & BridgeApi & UtilsApi & WithTra
125127
onChange={updateBridgeConfig}
126128
/>
127129
</div>
130+
<div className="col-12 col-sm-4 col-xxl-4">
131+
<label htmlFor="logs-limit" className="form-label">
132+
{t('logs_limit')}
133+
</label>
134+
<select
135+
id="logs-limit"
136+
className="form-select"
137+
onChange={(e) => {
138+
const limit = parseInt(e.target.value);
139+
store.setState({
140+
logsLimit: limit,
141+
logs: [...store.getState().logs.slice(-limit)],
142+
});
143+
}}
144+
>
145+
{logLimits.map((limit) => (
146+
<option key={limit} value={limit} selected={limit == logsLimit}>
147+
{limit}
148+
</option>
149+
))}
150+
</select>
151+
</div>
128152
<div className="col-12">
129153
<label htmlFor="reset">&nbsp;</label>
130154
<input

src/i18n/locales/en.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2535,7 +2535,8 @@
25352535
"logs": {
25362536
"empty_logs_message": "Nothing to display",
25372537
"filter_by_text": "Filter by text",
2538-
"show_only": "Show only"
2538+
"show_only": "Show only",
2539+
"logs_limit": "Logs limit"
25392540
},
25402541
"map": {
25412542
"help_coordinator_link_description": "Solid lines are the link to the Coordinator",

src/initialState.json

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
}
2424
},
2525
"logs": [],
26+
"logsLimit": 100,
2627
"extensions": [],
2728
"theme": "light",
2829
"missingTranslations": {},

src/store.ts

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export interface GlobalState extends WithDevices, WithDeviceStates, WithGroups,
6767
bridgeConfig: BridgeConfig;
6868
bridgeState: BridgeState;
6969
logs: LogMessage[];
70+
logsLimit: number;
7071
extensions: Extension[];
7172
theme: Theme;
7273
missingTranslations: Map<string, unknown>;

src/ws-client.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import keyBy from 'lodash/keyBy';
1515
import { GraphI } from './components/map/types';
1616
import local from 'store2';
1717

18-
const MAX_LOGS_RECORDS_IN_BUFFER = 100;
1918
const TOKEN_LOCAL_STORAGE_ITEM_NAME = "z2m-token-v2";
2019
const AUTH_FLAG_LOCAL_STORAGE_ITEM_NAME = "z2m-auth-v2";
2120
const UNAUTHORIZED_ERROR_CODE = 4401;
@@ -193,8 +192,8 @@ class Api {
193192

194193
case "bridge/logging":
195194
{
196-
const { logs } = store.getState();
197-
const newLogs = [...logs.slice(-MAX_LOGS_RECORDS_IN_BUFFER)];
195+
const { logs, logsLimit } = store.getState();
196+
const newLogs = [...logs.slice(-logsLimit)];
198197
newLogs.push({ ...(data.payload as unknown as LogMessage), timestamp: new Date() } as LogMessage);
199198
store.setState({ logs: newLogs });
200199
const log = data.payload as unknown as LogMessage;

0 commit comments

Comments
 (0)