Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(logs): save and restore columns with saved views (#120625)

Jon McCallumdecc21389b06
diff --git a/products/logs/backend/presentation/views/views_api.py b/products/logs/backend/presentation/views/views_api.py
index 31c7cecc8b5a..428f77003c9c 100644
--- a/products/logs/backend/presentation/views/views_api.py
+++ b/products/logs/backend/presentation/views/views_api.py
@@ -131,6 +131,7 @@ class LogsViewViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
"name": instance.name,
"pinned": instance.pinned,
"has_filters": bool(instance.filters),
+ "has_columns": instance.columns is not None,
},
team=self.team,
request=self.request,
diff --git a/products/logs/frontend/components/LogsViewer/config/columns.ts b/products/logs/frontend/components/LogsViewer/config/columns.ts
index 54fc720dd6b1..fed43133b540 100644
--- a/products/logs/frontend/components/LogsViewer/config/columns.ts
+++ b/products/logs/frontend/components/LogsViewer/config/columns.ts
@@ -3,6 +3,7 @@ import { uuid } from 'lib/utils/dom'
import { LogsQuery } from '~/queries/schema/schema-general'
import { PropertyFilterType } from '~/types'
+import { MAX_ATTRIBUTE_COLUMN_WIDTH } from 'products/logs/frontend/components/VirtualizedLogsList/layoutUtils'
import { AttributeColumnConfig, ParsedLogMessage } from 'products/logs/frontend/types'
export type LogsColumnType =
@@ -121,6 +122,25 @@ export function columnsToCustomColumns(columns: LogsColumnConfig[]): LogsQuery['
return expressions.length > 0 ? expressions : undefined
}
+/**
+ * Shape columns to what the saved-view serializer accepts. Local column state can hold a
+ * fractional width (from a touch drag, or an unbounded legacy width) and a whitespace-only
+ * name (from the inline editor). The serializer rejects both, and one bad column fails the
+ * whole save, so round and clamp widths and drop blank names.
+ */
+export function columnsForSavedView(columns: LogsColumnConfig[]): LogsColumnConfig[] {
+ return columns.map(({ name, width, ...column }) => {
+ const trimmedName = name?.trim()
+ return {
+ ...column,
+ ...(trimmedName ? { name: trimmedName } : {}),
+ ...(width !== undefined
+ ? { width: Math.min(Math.max(Math.round(width), 1), MAX_ATTRIBUTE_COLUMN_WIDTH) }
+ : {}),
+ }
+ })
+}
+
function escapeHogQLString(value: string): string {
return value.replace(/\\/g, '\\\\').replace(/'/g, "\\'")
}
diff --git a/products/logs/frontend/components/LogsViews/logsViewsListLogic.test.ts b/products/logs/frontend/components/LogsViews/logsViewsListLogic.test.ts
new file mode 100644
index 000000000000..78af187fcb00
--- /dev/null
+++ b/products/logs/frontend/components/LogsViews/logsViewsListLogic.test.ts
@@ -0,0 +1,110 @@
+import { expectLogic } from 'kea-test-utils'
+
+import { useMocks } from '~/mocks/jest'
+import { initKeaTests } from '~/test/init'
+
+import { DEFAULT_LOGS_COLUMNS, LogsColumnConfig } from '../LogsViewer/config/columns'
+import { logsViewerConfigLogic } from '../LogsViewer/config/logsViewerConfigLogic'
+import { logsViewsListLogic } from './logsViewsListLogic'
+import { LogsView } from './logsViewsLogic'
+
+const TEST_ID = 'test-views'
+
+const CUSTOM_COLUMNS: LogsColumnConfig[] = [
+ { id: 'timestamp', type: 'timestamp', width: 180 },
+ { id: 'level', type: 'level' },
+ { id: 'col-1', type: 'custom', expression: 'attributes.http.method', name: 'Method' },
+ { id: 'message', type: 'message' },
+]
+
+const makeView = (overrides: Partial<LogsView> = {}): LogsView =>
+ ({
+ id: 'view-uuid',
+ short_id: 'abc123',
+ name: 'My view',
+ filters: { searchTerm: 'boom' },
+ columns: null,
+ pinned: false,
+ created_at: '2026-10-20T00:00:00Z',
+ created_by: {} as LogsView['created_by'],
+ updated_at: null,
+ ...overrides,
+ }) as LogsView
+
+describe('logsViewsListLogic', () => {
+ let logic: ReturnType<typeof logsViewsListLogic.build>
+ let configLogic: ReturnType<typeof logsViewerConfigLogic.build>
+ let createdPayload: Record<string, any> | null
+
+ beforeEach(() => {
+ createdPayload = null
+ useMocks({
+ get: {
+ '/api/projects/:team_id/logs/views/': () => [200, { results: [] }],
+ },
+ post: {
+ '/api/projects/:team_id/logs/views/': async ({ request }) => {
+ createdPayload = (await request.json()) as Record<string, any>
+ return [200, makeView({ ...createdPayload })]
+ },
+ '/api/environments/:team_id/logs/query/': () => [200, { results: [], maxExportableLogs: 5000 }],
+ '/api/environments/:team_id/logs/sparkline/': () => [200, []],
+ },
+ })
+ initKeaTests()
+ configLogic = logsViewerConfigLogic({ id: TEST_ID })
+ configLogic.mount()
+ logic = logsViewsListLogic({ id: TEST_ID })
+ logic.mount()
+ })
+
+ afterEach(() => {
+ logic.unmount()
+ configLogic.unmount()
+ })
+
+ it.each([
+ ['as is when the API accepts every column', CUSTOM_COLUMNS, CUSTOM_COLUMNS],
+ [
+ 'with integer widths and no blank names',
+ [
+ { id: 'timestamp', type: 'timestamp', width: 180.6 },
+ { id: 'col-1', type: 'custom', expression: 'attributes.http.method', name: ' ' },
+ { id: 'col-2', type: 'custom', expression: 'attributes.region', name: ' Region ', width: 5000 },
+ { id: 'message', type: 'message' },
+ ],
+ [
+ { id: 'timestamp', type: 'timestamp', width: 181 },
+ { id: 'col-1', type: 'custom', expression: 'attributes.http.method' },
+ { id: 'col-2', type: 'custom', expression: 'attributes.region', name: 'Region', width: 2000 },
+ { id: 'message', type: 'message' },
+ ],
+ ],
+ ] as [string, LogsColumnConfig[], LogsColumnConfig[]][])(
+ 'saving a view sends the current columns %s',
+ async (_, currentColumns, expectedColumns) => {
+ configLogic.actions.setColumns(currentColumns)
+
+ await expectLogic(logic, () => {
+ logic.actions.setViewName('With columns')
+ logic.actions.saveView()
+ }).toDispatchActions(['createView', 'createViewSuccess'])
+
+ expect(createdPayload?.name).toEqual('With columns')
+ expect(createdPayload?.columns).toEqual(expectedColumns)
+ }
+ )
+
+ it.each([
+ ['restores the saved columns', CUSTOM_COLUMNS, CUSTOM_COLUMNS],
+ ['falls back to the default columns when the view has no column preference', null, DEFAULT_LOGS_COLUMNS],
+ ])('loading a view %s', async (_, savedColumns, expectedColumns) => {
+ configLogic.actions.setColumns([{ id: 'level', type: 'level' }])
+
+ await expectLogic(logic, () => {
+ logic.actions.loadView(makeView({ columns: savedColumns }))
+ }).toFinishAllListeners()
+
+ expect(configLogic.values.columns).toEqual(expectedColumns)
+ })
+})
diff --git a/products/logs/frontend/components/LogsViews/logsViewsListLogic.ts b/products/logs/frontend/components/LogsViews/logsViewsListLogic.ts
index 58559db2e90b..ef49c60e01ea 100644
--- a/products/logs/frontend/components/LogsViews/logsViewsListLogic.ts
+++ b/products/logs/frontend/components/LogsViews/logsViewsListLogic.ts
@@ -1,5 +1,7 @@
import { MakeLogicType, actions, connect, kea, key, listeners, path, props, reducers } from 'kea'
+import { LogsColumnConfig } from 'products/logs/frontend/components/LogsViewer/config/columns'
+import { logsViewerConfigLogic } from 'products/logs/frontend/components/LogsViewer/config/logsViewerConfigLogic'
import { logsViewerFiltersLogic } from 'products/logs/frontend/components/LogsViewer/Filters/logsViewerFiltersLogic'
import type { LogsViewApi, LogsViewApiFilters } from '../../generated/api.schemas'
@@ -8,6 +10,7 @@ import { LogsViewsLogicProps, logsViewsLogic } from './logsViewsLogic'
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface logsViewsListLogicValues {
+ columns: LogsColumnConfig[] // logsViewerConfigLogic
filters: LogsViewerFilters // logsViewerFiltersLogic
isModalOpen: boolean
isSaveModalOpen: boolean
@@ -16,7 +19,8 @@ export interface logsViewsListLogicValues {
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface logsViewsListLogicActions {
- createView: (args_0: { filters: LogsViewApiFilters; name: string }) => {
+ createView: (args_0: { columns: LogsColumnConfig[]; filters: LogsViewApiFilters; name: string }) => {
+ columns: LogsColumnConfig[]
filters: LogsViewApiFilters
name: string
} // logsViewsLogic
@@ -24,12 +28,14 @@ export interface logsViewsListLogicActions {
views: LogsViewApi[],
payload?:
| {
+ columns: LogsColumnConfig[]
filters: LogsViewApiFilters
name: string
}
| undefined
) => {
payload?: {
+ columns: LogsColumnConfig[]
filters: LogsViewApiFilters
name: string
}
@@ -77,7 +83,12 @@ export const logsViewsListLogic = kea<logsViewsListLogicType>([
path((key) => ['products', 'logs', 'frontend', 'components', 'LogsViews', 'logsViewsListLogic', key]),
connect((props: LogsViewsLogicProps) => ({
- values: [logsViewerFiltersLogic({ id: props.id }), ['filters']],
+ values: [
+ logsViewerFiltersLogic({ id: props.id }),
+ ['filters'],
+ logsViewerConfigLogic({ id: props.id }),
+ ['columns'],
+ ],
actions: [logsViewsLogic({ id: props.id }), ['createView', 'createViewSuccess', 'loadView', 'loadViews']],
})),
@@ -126,7 +137,7 @@ export const logsViewsListLogic = kea<logsViewsListLogicType>([
if (!name) {
return
}
- actions.createView({ name, filters: { ...values.filters } })
+ actions.createView({ name, filters: { ...values.filters }, columns: values.columns })
},
})),
])
diff --git a/products/logs/frontend/components/LogsViews/logsViewsLogic.ts b/products/logs/frontend/components/LogsViews/logsViewsLogic.ts
index 34f2898f649c..b4afd983705b 100644
--- a/products/logs/frontend/components/LogsViews/logsViewsLogic.ts
+++ b/products/logs/frontend/components/LogsViews/logsViewsLogic.ts
@@ -6,6 +6,12 @@ import { lemonToast } from '@posthog/lemon-ui'
import api from 'lib/api'
import { teamLogic } from 'scenes/teamLogic'
+import {
+ columnsForSavedView,
+ DEFAULT_LOGS_COLUMNS,
+ LogsColumnConfig,
+} from 'products/logs/frontend/components/LogsViewer/config/columns'
+import { logsViewerConfigLogic } from 'products/logs/frontend/components/LogsViewer/config/logsViewerConfigLogic'
import { logsViewerFiltersLogic } from 'products/logs/frontend/components/LogsViewer/Filters/logsViewerFiltersLogic'
import { LogsViewApi, LogsViewApiFilters } from 'products/logs/frontend/generated/api.schemas'
import { DEFAULT_ACTIVE_TAB, logsSceneLogic } from 'products/logs/frontend/logsSceneLogic'
@@ -33,6 +39,9 @@ export interface logsViewsLogicActions {
setActiveTab: (activeTab: LogsSceneActiveTab) => {
activeTab: LogsSceneActiveTab
} // logsSceneLogic
+ setColumns: (columns: LogsColumnConfig[]) => {
+ columns: LogsColumnConfig[]
+ } // logsViewerConfigLogic
setFilters: (
filters: Partial<LogsViewerFilters>,
pushToHistory?: boolean | undefined
@@ -40,9 +49,18 @@ export interface logsViewsLogicActions {
filters: Partial<LogsViewerFilters>
pushToHistory: boolean
} // logsViewerFiltersLogic
- createView: ({ name, filters }: { filters: LogsViewApiFilters; name: string }) => {
+ createView: ({
+ name,
+ filters,
+ columns,
+ }: {
+ columns: LogsColumnConfig[]
+ filters: LogsViewApiFilters
+ name: string
+ }) => {
name: string
filters: LogsViewApiFilters
+ columns: LogsColumnConfig[]
}
createViewFailure: (
error: string,
@@ -56,12 +74,14 @@ export interface logsViewsLogicActions {
payload?: {
name: string
filters: LogsViewApiFilters
+ columns: LogsColumnConfig[]
}
) => {
views: LogsViewApi[]
payload?: {
name: string
filters: LogsViewApiFilters
+ columns: LogsColumnConfig[]
}
}
deleteView: (shortId: string) => {
@@ -106,7 +126,14 @@ export const logsViewsLogic = kea<logsViewsLogicType>([
connect((props: LogsViewsLogicProps) => ({
values: [teamLogic, ['currentTeamId']],
- actions: [logsViewerFiltersLogic({ id: props.id }), ['setFilters'], logsSceneLogic(), ['setActiveTab']],
+ actions: [
+ logsViewerFiltersLogic({ id: props.id }),
+ ['setFilters'],
+ logsViewerConfigLogic({ id: props.id }),
+ ['setColumns'],
+ logsSceneLogic(),
+ ['setActiveTab'],
+ ],
})),
actions({
@@ -123,11 +150,20 @@ export const logsViewsLogic = kea<logsViewsLogicType>([
const response = await api.get(`${logsViewsUrl(values.currentTeamId)}/`)
return response.results
},
- createView: async ({ name, filters }: { name: string; filters: LogsViewApiFilters }) => {
+ createView: async ({
+ name,
+ filters,
+ columns,
+ }: {
+ name: string
+ filters: LogsViewApiFilters
+ columns: LogsColumnConfig[]
+ }) => {
// nosemgrep: prefer-codegen-api -- Legacy raw API call with a URL built at runtime and an unchecked response type. Use a generated function if one covers this endpoint.
const created: LogsView = await api.create(`${logsViewsUrl(values.currentTeamId)}/`, {
name,
filters,
+ columns: columnsForSavedView(columns),
})
lemonToast.success('View saved')
return [created, ...values.views]
@@ -154,6 +190,8 @@ export const logsViewsLogic = kea<logsViewsLogicType>([
}
},
loadView: ({ view }) => {
+ // A null `columns` means the view has no column preference, so it opens with the default set
+ actions.setColumns(view.columns ?? DEFAULT_LOGS_COLUMNS)
actions.setFilters(view.filters || {})
actions.setActiveTab(DEFAULT_ACTIVE_TAB)
},