Files
ontime/apps/client/src/common/stores/logger.ts
T
Claude 46e4ac94f1 feat(log): add filters to the event log and cap the log store
Finding a single error in a stream of RX/TX traffic was not practical
with only the origin filters.

- add a text filter and an issues only filter
- show how many entries are being displayed
- add an empty state which distinguishes no activity from no matches
- collapse the six origin flags into a single set
- cap the log store at 500 entries, it was growing unbounded for the
  duration of a session
- the event log button was labelled Extract, which did not convey that
  it opens the log in a new window

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NrhcEgY8mqaxZi8veMPrrx
2026-08-01 13:20:46 +00:00

88 lines
1.8 KiB
TypeScript

import { Log, LogLevel, LogOrigin, MessageTag } from 'ontime-types';
import { generateId, millisToString } from 'ontime-utils';
import { useCallback } from 'react';
import { useStore } from 'zustand';
import { createStore } from 'zustand/vanilla';
import { sendSocket } from '../utils/socket';
import { nowInMillis } from '../utils/time';
type LogStore = {
logs: Log[];
};
const logger = createStore<LogStore>(() => ({
logs: [],
}));
/** the log is kept in memory, we cap it to avoid it growing through a long show */
const maxLogEntries = 500;
export const useLogData = () => useStore(logger);
export const addLog = (log: Log) =>
logger.setState((state) => ({
logs: [log, ...state.logs].slice(0, maxLogEntries),
}));
export const clearLogs = () => logger.setState({ logs: [] });
export function useEmitLog() {
/**
* Utility function sends message over socket
* @param text
* @param level
* @private
*/
const _emit = useCallback((text: string, level: LogLevel) => {
const log = {
id: generateId(),
origin: LogOrigin.Client,
time: millisToString(nowInMillis()),
level,
text,
};
sendSocket(MessageTag.Log, log);
}, []);
/**
* Sends a message with level INFO
* @param text
*/
const emitInfo = useCallback(
(text: string) => {
_emit(text, LogLevel.Info);
},
[_emit],
);
/**
* Sends a message with level WARN
* @param text
*/
const emitWarning = useCallback(
(text: string) => {
_emit(text, LogLevel.Warn);
},
[_emit],
);
/**
* Sends a message with level ERROR
* @param text
*/
const emitError = useCallback(
(text: string) => {
_emit(text, LogLevel.Error);
},
[_emit],
);
return {
emitInfo,
emitWarning,
emitError,
};
}