: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(logs): sync the logs/patterns/group lens with the url on the logs scene (#71053)
Jon McCallum2e5c9638a163
diff --git a/products/logs/frontend/logsSceneLogic.test.ts b/products/logs/frontend/logsSceneLogic.test.tsindex 7e9346429dfc..9b8db97b8dfe 100644--- a/products/logs/frontend/logsSceneLogic.test.ts+++ b/products/logs/frontend/logsSceneLogic.test.ts@@ -77,6 +77,32 @@ describe('logsSceneLogic', () => {expect(logic.values.filters.severityLevels).toEqual(['error', 'warn', 'info'])})+ it.each([+ ['a valid lens', 'patterns', 'patterns'],+ ['an unrecognised lens falls back to the default', 'nonsense', 'logs'],+ ])('applies viewMode from the URL: %s', async (_, urlValue, expected) => {+ await expectLogic(logic, () => {+ router.actions.push('/logs', { viewMode: urlValue })+ }).toFinishAllListeners()++ expect(logic.values.viewMode).toEqual(expected)+ })++ it('syncs lens switches back to the URL, dropping the param for the default lens', async () => {+ // The round-trip contract for shareable lens links: switching to Patterns writes+ // ?viewMode=patterns, and returning to Logs (the default) removes the param+ // instead of pinning viewMode=logs into every copied URL.+ await expectLogic(logic, () => {+ logic.actions.setViewMode('patterns')+ }).toFinishAllListeners()+ expect(router.values.searchParams.viewMode).toEqual('patterns')++ await expectLogic(logic, () => {+ logic.actions.setViewMode('logs')+ }).toFinishAllListeners()+ expect(router.values.searchParams.viewMode).toBeUndefined()+ })+it.each([['completely invalid value', '["invalid-level"]', []],['typo in valid level', '["debug123"]', []],diff --git a/products/logs/frontend/logsSceneLogic.tsx b/products/logs/frontend/logsSceneLogic.tsxindex 9ed6c3d86a49..05752483bfc9 100644--- a/products/logs/frontend/logsSceneLogic.tsx+++ b/products/logs/frontend/logsSceneLogic.tsx@@ -13,7 +13,9 @@ import { Params } from 'scenes/sceneTypes'import {DEFAULT_ORDER_BY,+ DEFAULT_VIEW_MODE,logsViewerConfigLogic,+ LogsViewerViewMode,} from 'products/logs/frontend/components/LogsViewer/config/logsViewerConfigLogic'import { LogsViewerFilters } from 'products/logs/frontend/components/LogsViewer/config/types'import {@@ -40,6 +42,8 @@ export const getLogsSqlEditorTabId = (id: string): string => `logs-sql-editor-${// A static id would persist across projects in the same browser, leaking one project's pinned log payloads into another.export const LOGS_SCENE_VIEWER_ID = `logs-scene-${window.POSTHOG_APP_CONTEXT?.current_team?.id ?? 'unknown'}`+const VALID_VIEW_MODES: LogsViewerViewMode[] = ['logs', 'patterns', 'group']+export type LogsSceneActiveTab = 'viewer' | 'services' | 'alerts' | 'sql' | 'configuration'const VALID_ACTIVE_TABS: LogsSceneActiveTab[] = ['viewer', 'services', 'alerts', 'sql', 'configuration']export const DEFAULT_ACTIVE_TAB: LogsSceneActiveTab = 'viewer'@@ -63,7 +67,7 @@ export const logsSceneLogic = kea<logsSceneLogicType>([logsFilterHistoryLogic({ id: LOGS_SCENE_VIEWER_ID }),['pushToFilterHistory'],logsViewerConfigLogic({ id: LOGS_SCENE_VIEWER_ID }),- ['setOrderBy'],+ ['setOrderBy', 'setViewMode'],logsViewerDataLogic({ id: LOGS_SCENE_VIEWER_ID }),['setInitialLogsLimit', 'fetchLogsSuccess', 'handleQueryChange'],logsViewerLogic({ id: LOGS_SCENE_VIEWER_ID }),@@ -77,7 +81,7 @@ export const logsSceneLogic = kea<logsSceneLogicType>([logsViewerFiltersLogic({ id: LOGS_SCENE_VIEWER_ID }),['filters', 'utcDateRange'],logsViewerConfigLogic({ id: LOGS_SCENE_VIEWER_ID }),- ['orderBy'],+ ['orderBy', 'viewMode'],logsViewerDataLogic({ id: LOGS_SCENE_VIEWER_ID }),['initialLogsLimit'],logsViewerLogic({ id: LOGS_SCENE_VIEWER_ID }),@@ -161,6 +165,15 @@ export const logsSceneLogic = kea<logsSceneLogicType>([if (params.orderBy && !equal(params.orderBy, values.orderBy)) {actions.setOrderBy(params.orderBy)}+ // The Logs⇄Patterns⇄Group lens. Absent or unrecognised param means the default —+ // a shared link with a flag-gated lens stays safe because the viewer double-gates+ // rendering on the flag, so an unreachable mode just shows the Logs table.+ const viewMode: LogsViewerViewMode = VALID_VIEW_MODES.includes(params.viewMode as LogsViewerViewMode)+ ? (params.viewMode as LogsViewerViewMode)+ : DEFAULT_VIEW_MODE+ if (viewMode !== values.viewMode) {+ actions.setViewMode(viewMode)+ }if (params.initialLogsLimit != null && +params.initialLogsLimit !== values.initialLogsLimit) {actions.setInitialLogsLimit(+params.initialLogsLimit)}@@ -256,6 +269,15 @@ export const logsSceneLogic = kea<logsSceneLogicType>([)}+ const syncViewMode = (): ReturnType<typeof syncSearchParams> => {+ return withUrlSyncGuard(() =>+ syncSearchParams(router, (params: Params) => {+ updateSearchParams(params, 'viewMode', values.viewMode, DEFAULT_VIEW_MODE)+ return params+ })+ )+ }+return {// initialLogsLimit is a one-shot override from "copy link to log" URLs.// It ensures the first fetch loads enough logs to include the linked log,@@ -265,6 +287,7 @@ export const logsSceneLogic = kea<logsSceneLogicType>([clearLinkToLogId: () => clearLinkToLogId(),syncUrl: () => syncUrl(),setActiveTab: () => syncActiveTab(),+ setViewMode: () => syncViewMode(),}}),