Skip to content

Commit

Permalink
feat(logger): include query params (#3702)
Browse files Browse the repository at this point in the history
* feat(logger): include query param

* optimize slice path
  • Loading branch information
ryuapp authored Feb 6, 2025
1 parent 31d6e6c commit b4f1d45
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
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

0 comments on commit b4f1d45

Please sign in to comment.