Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Improve unified logs formatting for auth logs (#46365)

Joshen Lim94834752b2f2
diff --git a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.utils.ts b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.utils.ts
index 78c96b0ce83..e03af92e3ab 100644
--- a/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.utils.ts
+++ b/apps/studio/components/interfaces/UnifiedLogs/UnifiedLogs.utils.ts
@@ -124,16 +124,28 @@ export function formatServiceTypeForDisplay(serviceType: string): string {
*/
export function parseAuthLogEventMessage(value: string | undefined): string | undefined {
if (!value) return value
+
try {
const parsed = JSON.parse(value)
+
if (parsed && typeof parsed === 'object') {
+ const err = parsed.error || parsed.error_code
+ if (typeof err === 'string' && err.trim()) {
+ return !/^\d{3}:/.test(err) ? err.replaceAll('_', ' ') : err
+ }
+
const msg = parsed.msg
- const err = parsed.error
- if (typeof msg === 'string' && msg.trim()) return msg
- if (typeof err === 'string' && err.trim()) return err
+ if (typeof msg === 'string' && msg.trim()) {
+ const authEvent =
+ 'action' in parsed || 'auth_event_action' in parsed
+ ? (parsed.action || parsed.auth_event.action).replaceAll('_', ' ')
+ : undefined
+ return `${authEvent ? `${authEvent}: ` : ''}${msg}`
+ }
}
+
return value
- } catch {
+ } catch (error) {
return value
}
}
diff --git a/apps/studio/components/interfaces/UnifiedLogs/components/Columns.tsx b/apps/studio/components/interfaces/UnifiedLogs/components/Columns.tsx
index 544261a58e4..9bb50e54274 100644
--- a/apps/studio/components/interfaces/UnifiedLogs/components/Columns.tsx
+++ b/apps/studio/components/interfaces/UnifiedLogs/components/Columns.tsx
@@ -1,5 +1,5 @@
import { ColumnDef } from '@tanstack/react-table'
-import { Checkbox, Tooltip, TooltipContent, TooltipTrigger } from 'ui'
+import { Checkbox, cn, Tooltip, TooltipContent, TooltipTrigger } from 'ui'
import { STATUS_CODE_LABELS } from '../UnifiedLogs.constants'
import { ColumnFilterSchema, ColumnSchema } from '../UnifiedLogs.schema'
@@ -203,10 +203,6 @@ export function generateDynamicColumns({ data }: { data: ColumnSchema[] }): {
{
accessorKey: 'pathname',
header: 'Pathname',
- cell: ({ row }) => {
- const value = row.getValue<ColumnFilterSchema['pathname']>('pathname') ?? ''
- return value
- },
enableSorting: false,
enableResizing: false,
size: 250,
@@ -216,6 +212,10 @@ export function generateDynamicColumns({ data }: { data: ColumnSchema[] }): {
cellClassName: 'font-mono tracking-tight w-[250px]',
headerClassName: 'w-[250px]',
},
+ cell: ({ row }) => {
+ const value = row.getValue<ColumnFilterSchema['pathname']>('pathname') ?? ''
+ return value
+ },
},
// Event message column - controlled by columnVisibility
{
@@ -241,7 +241,13 @@ export function generateDynamicColumns({ data }: { data: ColumnSchema[] }): {
</TooltipContent>
</Tooltip>
)}
- {displayMessage && <span className="text-muted-foreground">{displayMessage}</span>}
+ {displayMessage && (
+ <span
+ className={cn('text-muted-foreground', logType === 'auth' && 'capitalize-sentence')}
+ >
+ {displayMessage}
+ </span>
+ )}
</div>
)
},
diff --git a/apps/studio/data/logs/unified-logs-infinite-query.ts b/apps/studio/data/logs/unified-logs-infinite-query.ts
index 23698524f25..edf5d868175 100644
--- a/apps/studio/data/logs/unified-logs-infinite-query.ts
+++ b/apps/studio/data/logs/unified-logs-infinite-query.ts
@@ -5,6 +5,7 @@ import { executeAnalyticsSql } from './execute-analytics-sql'
import { logsKeys } from './keys'
import { logsAllEndpointUrl, pickLogsQueryBuilder } from './logs-endpoint'
import { analyticsLiteral, safeSql } from './safe-analytics-sql'
+import { extractLogMetadata } from './unified-logs.utils'
import { getUnifiedLogsQuery } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries'
import { getUnifiedLogsQuery as getUnifiedLogsQueryBq } from '@/components/interfaces/UnifiedLogs/UnifiedLogs.queries.bq'
import {
@@ -134,15 +135,17 @@ export async function getUnifiedLogs(
? new Date(/Z$|[+-]\d{2}:?\d{2}$/.test(ts) ? ts : `${ts}Z`)
: new Date(Number(ts) / 1000)
+ const { status, method, pathname } = extractLogMetadata(row)
+
return {
id: row.id,
date,
+ method,
+ pathname,
+ status,
timestamp: row.timestamp,
level: row.level as LogLevel,
- status: row.status || 200,
- method: row.method,
host: row.host,
- pathname: (row.url || '').replace(/^https?:\/\/[^\/]+/, '') || row.pathname || '',
event_message: row.event_message || row.body || '',
headers:
typeof row.headers === 'string' ? JSON.parse(row.headers || '{}') : row.headers || {},
diff --git a/apps/studio/data/logs/unified-logs.utils.test.ts b/apps/studio/data/logs/unified-logs.utils.test.ts
new file mode 100644
index 00000000000..1246c6e4c17
--- /dev/null
+++ b/apps/studio/data/logs/unified-logs.utils.test.ts
@@ -0,0 +1,173 @@
+import { describe, expect, it } from 'vitest'
+
+import { extractLogMetadata } from './unified-logs.utils'
+
+describe('extractLogMetadata', () => {
+ describe('non-auth logs', () => {
+ it('returns the row status, method, and url-derived pathname', () => {
+ const row = {
+ log_type: 'api',
+ status: 404,
+ method: 'GET',
+ url: 'https://example.supabase.co/rest/v1/users?select=id',
+ pathname: '/ignored',
+ event_message: 'irrelevant',
+ }
+
+ expect(extractLogMetadata(row)).toEqual({
+ status: 404,
+ method: 'GET',
+ pathname: '/rest/v1/users?select=id',
+ })
+ })
+
+ it('falls back to row.pathname when url is missing', () => {
+ const row = {
+ log_type: 'api',
+ status: 500,
+ method: 'POST',
+ url: '',
+ pathname: '/fallback',
+ event_message: '',
+ }
+
+ expect(extractLogMetadata(row).pathname).toBe('/fallback')
+ })
+
+ it('returns empty string for pathname when both url and pathname are missing', () => {
+ const row = {
+ log_type: 'api',
+ status: 200,
+ method: 'GET',
+ event_message: '',
+ }
+
+ expect(extractLogMetadata(row).pathname).toBe('')
+ })
+
+ it('defaults status to 200 when missing', () => {
+ const row = {
+ log_type: 'api',
+ method: 'GET',
+ url: 'https://example.supabase.co/health',
+ event_message: '',
+ }
+
+ expect(extractLogMetadata(row).status).toBe(200)
+ })
+ })
+
+ describe('auth logs', () => {
+ it('extracts status, method, and pathname from event_message JSON', () => {
+ const row = {
+ log_type: 'auth',
+ status: 999,
+ method: 'IGNORED',
+ url: 'https://ignored',
+ event_message: JSON.stringify({
+ status: 400,
+ method: 'POST',
+ path: '/token',
+ msg: 'request completed',
+ }),
+ }
+
+ expect(extractLogMetadata(row)).toEqual({
+ status: 400,
+ method: 'POST',
+ pathname: '/token',
+ })
+ })
+
+ it('falls back to leading 3-digit status in msg when event_message.status is missing', () => {
+ const row = {
+ log_type: 'auth',
+ event_message: JSON.stringify({
+ method: 'POST',
+ path: '/token',
+ msg: '400: Invalid login credentials',
+ }),
+ }
+
+ expect(extractLogMetadata(row).status).toBe(400)
+ })
+
+ it('falls back to leading 3-digit status in error when neither status nor msg has one', () => {
+ const row = {
+ log_type: 'auth',
+ event_message: JSON.stringify({
+ method: 'POST',
+ path: '/token',
+ msg: 'request completed',
+ error: '403: forbidden',
+ }),
+ }
+
+ expect(extractLogMetadata(row).status).toBe(403)
+ })
+
+ it('returns undefined status when no status can be found anywhere', () => {
+ const row = {
+ log_type: 'auth',
+ event_message: JSON.stringify({
+ method: 'POST',
+ path: '/token',
+ msg: 'request completed',
+ }),
+ }
+
+ expect(extractLogMetadata(row).status).toBeUndefined()
+ })
+
+ it('does not extract status from a non-leading 3-digit number in msg', () => {
+ const row = {
+ log_type: 'auth',
+ event_message: JSON.stringify({
+ msg: 'attempt 500 failed',
+ }),
+ }
+
+ expect(extractLogMetadata(row).status).toBeUndefined()
+ })
+
+ it('does not match 4+ digit numbers at the start of msg', () => {
+ const row = {
+ log_type: 'auth',
+ event_message: JSON.stringify({
+ msg: '4000 something',
+ }),
+ }
+
+ expect(extractLogMetadata(row).status).toBeUndefined()
+ })
+
+ it('returns undefined metadata when event_message is not valid JSON', () => {
+ const row = {
+ log_type: 'auth',
+ status: 200,
+ method: 'GET',
+ url: 'https://example.supabase.co/token',
+ event_message: 'not json',
+ }
+
+ expect(extractLogMetadata(row)).toEqual({
+ status: undefined,
+ method: undefined,
+ pathname: undefined,
+ })
+ })
+
+ it('prefers explicit event_message.status over msg/error extraction', () => {
+ const row = {
+ log_type: 'auth',
+ event_message: JSON.stringify({
+ status: 200,
+ msg: '500: something went wrong',
+ error: '503: service unavailable',
+ }),
+ }
+
+ expect(extractLogMetadata(row).status).toBe(200)
+ })
+ })
+})
diff --git a/apps/studio/data/logs/unified-logs.utils.ts b/apps/studio/data/logs/unified-logs.utils.ts
new file mode 100644
index 00000000000..4a4a290e7e9
--- /dev/null
+++ b/apps/studio/data/logs/unified-logs.utils.ts
@@ -0,0 +1,27 @@
+import { tryParseJson } from '@/lib/helpers'
+
+const extractLeadingStatus = (s?: string) => {
+ const m = typeof s === 'string' ? s.match(/^(\d{3})\b/) : null
+ return m ? Number(m[1]) : undefined
+}
+
+// [Joshen] Row has an unknown type in this case so `any` is accurate
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+export const extractLogMetadata = (row: any) => {
+ // [Joshen] For auth logs, these metadata are nested within event_message,
+ // so opting to bring them out at the query level
+ const eventMessage = tryParseJson(row.event_message)
+ const status =
+ row.log_type === 'auth'
+ ? (eventMessage?.status ??
+ extractLeadingStatus(eventMessage?.msg) ??
+ extractLeadingStatus(eventMessage?.error))
+ : (row.status ?? 200)
+ const method = row.log_type === 'auth' ? eventMessage?.method : row.method
+ const pathname =
+ row.log_type === 'auth'
+ ? eventMessage?.path
+ : (row.url || '').replace(/^https?:\/\/[^\/]+/, '') || row.pathname || ''
+
+ return { status, method, pathname }
+}