Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/print-client-ingest-events.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'evlog': patch
---

Client events received by the Nuxt and Nitro ingest endpoint are now printed to stdout, the same way server events are. Before, they were only sent to drains, so an app without a drain accepted browser logs with a 204 and they never showed up anywhere. Events still reach your drains, and `silent: true` still suppresses the console output.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createError, defineEventHandler, getHeader, getHeaders, getRequestHost, readRawBody, setResponseStatus } from 'h3'
import { useNitroApp } from 'nitropack/runtime'
import type { IngestPayload, WideEvent } from '../../../../types'
import { getEnvironment, getGlobalPluginRunner, shouldSample } from '../../../../logger'
import { getEnvironment, getGlobalPluginRunner, outputWideEvent, shouldSample } from '../../../../logger'
import { filterSafeHeaders } from '../../../../utils'

type IngestEvent = Parameters<typeof defineEventHandler>[0] extends (e: infer E) => unknown ? E : never
Expand Down Expand Up @@ -189,6 +189,10 @@ export default defineEventHandler(async (event) => {
await runner.runEnrich(enrichCtx)
}

// Write to stdout once enrichers have run, so client events appear in the
// console even when no drain is configured, same as server events.
outputWideEvent(wideEvent)

const drainCtx = {
event: wideEvent,
request,
Expand Down
74 changes: 74 additions & 0 deletions packages/evlog/test/nuxt/ingest-output.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { createApp, toWebHandler } from 'h3'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { initLogger } from '../../src/logger'

const callHook = vi.fn().mockResolvedValue(undefined)

vi.mock('nitropack/runtime', () => ({
useNitroApp: () => ({ hooks: { callHook } }),
}))

const { default: ingest } = await import('../../src/runtime/server/routes/_evlog/ingest.post')

const app = createApp()
app.use('/api/_evlog/ingest', ingest)
const handler = toWebHandler(app)

function send(level: string, message: string) {
return handler(new Request('http://localhost/api/_evlog/ingest', {
method: 'POST',
headers: { 'content-type': 'application/json', 'origin': 'http://localhost' },
body: JSON.stringify({ timestamp: new Date().toISOString(), level, message }),
}))
}

describe('client ingest stdout output', () => {
let info: ReturnType<typeof vi.spyOn>
let error: ReturnType<typeof vi.spyOn>

beforeEach(() => {
callHook.mockClear()
info = vi.spyOn(console, 'info').mockImplementation(() => {})
error = vi.spyOn(console, 'error').mockImplementation(() => {})
})

afterEach(() => {
vi.restoreAllMocks()
})

it('prints a client event to stdout when no drain is configured', async () => {
initLogger({ pretty: false, stringify: false })

const res = await send('info', 'client hello')

expect(res.status).toBe(204)
expect(info).toHaveBeenCalledTimes(1)
expect(info.mock.calls[0][0]).toMatchObject({ level: 'info', message: 'client hello', source: 'client' })
})

it('prints a client error to stdout through the error console method', async () => {
initLogger({ pretty: false, stringify: false })

await send('error', 'client boom')

expect(error).toHaveBeenCalledTimes(1)
expect(error.mock.calls[0][0]).toMatchObject({ level: 'error', message: 'client boom', source: 'client' })
})

it('prints a client event and still runs the drain hook', async () => {
initLogger({ pretty: false, stringify: false })

await send('info', 'both')

expect(info).toHaveBeenCalledTimes(1)
expect(callHook.mock.calls.filter(([name]) => name === 'evlog:drain')).toHaveLength(1)
})

it('does not print when the logger is silent', async () => {
initLogger({ pretty: false, stringify: false, silent: true })

await send('info', 'quiet')

expect(info).not.toHaveBeenCalled()
})
})
Loading