Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(logger): include query params #3702

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions src/middleware/logger/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ describe('Logger by Middleware', () => {
expect(log).toMatch(/m?s$/)
})

it('Log status 200 with small body and query param', async () => {
const res = await app.request('http://localhost/short?foo=bar')
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(log.startsWith('--> GET /short?foo=bar \x1b[32m200\x1b[0m')).toBe(true)
expect(log).toMatch(/m?s$/)
})

it('Log status 200 with big body', async () => {
const res = await app.request('http://localhost/long')
expect(res).not.toBeNull()
Expand Down Expand Up @@ -105,15 +113,15 @@ describe('Logger by Middleware', () => {
const res = await app.request('http://localhost/server-error?status=100')
expect(res).not.toBeNull()
expect(res.status).toBe(100)
expect(log.startsWith('--> GET /server-error 100')).toBe(true)
expect(log.startsWith('--> GET /server-error?status=100 100')).toBe(true)
expect(log).toMatch(/m?s$/)
})

it('Log status 700', async () => {
const res = await app.request('http://localhost/server-error?status=700')
expect(res).not.toBeNull()
expect(res.status).toBe(700)
expect(log.startsWith('--> GET /server-error 700')).toBe(true)
expect(log.startsWith('--> GET /server-error?status=700 700')).toBe(true)
expect(log).toMatch(/m?s$/)
})
})
Expand Down
5 changes: 2 additions & 3 deletions src/middleware/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import type { MiddlewareHandler } from '../../types'
import { getColorEnabled } from '../../utils/color'
import { getPath } from '../../utils/url'

enum LogPrefix {
Outgoing = '-->',
Expand Down Expand Up @@ -81,9 +80,9 @@ function log(
*/
export const logger = (fn: PrintFunc = console.log): MiddlewareHandler => {
return async function logger(c, next) {
const { method } = c.req
const { method, url } = c.req

const path = getPath(c.req.raw)
const path = url.slice(url.indexOf('/', 8))

log(fn, LogPrefix.Incoming, method, path)

Expand Down
Loading