From c0dbbb564db907c24becc6142298afb9b70ed003 Mon Sep 17 00:00:00 2001 From: Matias Chomicki Date: Mon, 30 Jun 2025 14:03:01 +0100 Subject: [PATCH] LogLine: remove new lines in unwrapped mode --- .../logs/components/panel/LogList.tsx | 4 +-- .../logs/components/panel/processing.ts | 36 ++++++++++++++----- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/public/app/features/logs/components/panel/LogList.tsx b/public/app/features/logs/components/panel/LogList.tsx index 58a82266bcc..960f19b2b25 100644 --- a/public/app/features/logs/components/panel/LogList.tsx +++ b/public/app/features/logs/components/panel/LogList.tsx @@ -283,13 +283,13 @@ const LogListComponent = ({ setProcessedLogs( preProcessLogs( logs, - { getFieldLinks, escape: forceEscape ?? false, order: sortOrder, timeZone, virtualization }, + { getFieldLinks, escape: forceEscape ?? false, order: sortOrder, timeZone, virtualization, wrapLogMessage }, grammar ) ); virtualization.resetLogLineSizes(); listRef.current?.resetAfterIndex(0); - }, [forceEscape, getFieldLinks, grammar, loading, logs, sortOrder, timeZone, virtualization]); + }, [forceEscape, getFieldLinks, grammar, loading, logs, sortOrder, timeZone, virtualization, wrapLogMessage]); useEffect(() => { listRef.current?.resetAfterIndex(0); diff --git a/public/app/features/logs/components/panel/processing.ts b/public/app/features/logs/components/panel/processing.ts index f891f89b097..5a8ed40278c 100644 --- a/public/app/features/logs/components/panel/processing.ts +++ b/public/app/features/logs/components/panel/processing.ts @@ -47,8 +47,12 @@ export class LogListModel implements LogRowModel { private _fields: FieldDef[] | undefined = undefined; private _getFieldLinks: GetFieldLinksFn | undefined = undefined; private _virtualization?: LogLineVirtualization; + private _wrapLogMessage: boolean; - constructor(log: LogRowModel, { escape, getFieldLinks, grammar, timeZone, virtualization }: PreProcessLogOptions) { + constructor( + log: LogRowModel, + { escape, getFieldLinks, grammar, timeZone, virtualization, wrapLogMessage }: PreProcessLogOptions + ) { // LogRowModel this.datasourceType = log.datasourceType; this.dataFrame = log.dataFrame; @@ -82,6 +86,7 @@ export class LogListModel implements LogRowModel { defaultWithMS: true, }); this._virtualization = virtualization; + this._wrapLogMessage = wrapLogMessage; let raw = log.raw; if (escape && log.hasUnescapedContent) { @@ -95,6 +100,9 @@ export class LogListModel implements LogRowModel { this._body = this.collapsed ? this.raw.substring(0, this._virtualization?.getTruncationLength(null) ?? TRUNCATION_DEFAULT_LENGTH) : this.raw; + if (!this._wrapLogMessage) { + this._body = this._body.replace(/(\r\n|\n|\r)/g, ''); + } } return this._body; } @@ -127,14 +135,20 @@ export class LogListModel implements LogRowModel { if (fieldName === LOG_LINE_BODY_FIELD_NAME) { return this.body; } + let fieldValue = ''; if (this.labels[fieldName] != null) { - return this.labels[fieldName]; - } - const field = this.fields.find((field) => { - return field.keys[0] === fieldName; - }); + fieldValue = this.labels[fieldName]; + } else { + const field = this.fields.find((field) => { + return field.keys[0] === fieldName; + }); - return field ? field.values.toString() : ''; + fieldValue = field ? field.values.toString() : ''; + } + if (!this._wrapLogMessage) { + return fieldValue.replace(/(\r\n|\n|\r)/g, ''); + } + return fieldValue; } updateCollapsedState(displayedFields: string[], container: HTMLDivElement | null) { @@ -172,15 +186,18 @@ export interface PreProcessOptions { order: LogsSortOrder; timeZone: string; virtualization?: LogLineVirtualization; + wrapLogMessage: boolean; } export const preProcessLogs = ( logs: LogRowModel[], - { escape, getFieldLinks, order, timeZone, virtualization }: PreProcessOptions, + { escape, getFieldLinks, order, timeZone, virtualization, wrapLogMessage }: PreProcessOptions, grammar?: Grammar ): LogListModel[] => { const orderedLogs = sortLogRows(logs, order); - return orderedLogs.map((log) => preProcessLog(log, { escape, getFieldLinks, grammar, timeZone, virtualization })); + return orderedLogs.map((log) => + preProcessLog(log, { escape, getFieldLinks, grammar, timeZone, virtualization, wrapLogMessage }) + ); }; interface PreProcessLogOptions { @@ -189,6 +206,7 @@ interface PreProcessLogOptions { grammar?: Grammar; timeZone: string; virtualization?: LogLineVirtualization; + wrapLogMessage: boolean; } const preProcessLog = (log: LogRowModel, options: PreProcessLogOptions): LogListModel => { return new LogListModel(log, options);