Skip to content

Commit

Permalink
feat(logging): add request logging to board page (#1666)
Browse files Browse the repository at this point in the history
* feat(logging): add pino logger

* feat(logging): add middleware and logging to board page
  • Loading branch information
SelmaBergstrand authored Oct 10, 2024
1 parent d21cba7 commit a166f01
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 8 deletions.
26 changes: 26 additions & 0 deletions tavla/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { NextResponse, userAgent } from 'next/server'
import type { NextRequest } from 'next/server'
import { logger } from 'utils/logger'

const log = logger.child({ module: 'middleware' })
export function middleware(request: NextRequest) {
const response = NextResponse.next()
log.info({
request: {
browser: userAgent(request).browser,
device: userAgent(request).device,
url: request.url,
path: request.nextUrl.pathname,
method: request.method,
},
response: {
status: response.status,
body: response.body,
},
})
return response
}

export const config = {
matcher: ['/:id(\\w{20})'],
}
13 changes: 8 additions & 5 deletions tavla/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ const { PHASE_DEVELOPMENT_SERVER } = require('next/constants')
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'standalone',
transpilePackages: ['swr', "tailwindcss"],
transpilePackages: ['swr', 'tailwindcss'],
i18n: {
locales: ["nb"],
defaultLocale: "nb"
locales: ['nb'],
defaultLocale: 'nb',
},
images: {
remotePatterns: [
Expand All @@ -20,17 +20,20 @@ const nextConfig = {
},
],
},
experimental: {
serverComponentsExternalPackages: ['pino', 'pino-pretty'],
},
}

module.exports = async (phase, { defaultConfig }) => {
if (phase === PHASE_DEVELOPMENT_SERVER) {
nextConfig.images.remotePatterns.push({
protocol: 'http',
hostname: 'localhost'
hostname: 'localhost',
})
nextConfig.images.remotePatterns.push({
protocol: 'http',
hostname: '127.0.0.1'
hostname: '127.0.0.1',
})
}

Expand Down
2 changes: 2 additions & 0 deletions tavla/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
"lodash": "4.17.21",
"nanoid": "4.0.2",
"next": "14.2.4",
"pino": "9.4.0",
"pino-pretty": "11.2.2",
"posthog-js": "1.155.2",
"react": "18.2.0",
"react-dom": "18.2.0",
Expand Down
6 changes: 6 additions & 0 deletions tavla/pages/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import { useRefresh } from 'hooks/useRefresh'
import { getBackendUrl } from 'utils/index'
import Head from 'next/head'
import { useEffect } from 'react'
import { logger } from 'utils/logger'

const log = logger.child({ module: 'board' })
export async function getServerSideProps({
params,
}: {
Expand Down Expand Up @@ -44,6 +46,10 @@ function BoardPage({
backend_url: string
}) {
const updatedBoard = useRefresh(board, backend_url)
log.info({
boardID: board.id,
organization: organization?.name,
})

const title = updatedBoard.meta?.title
? updatedBoard.meta.title + ' | Entur tavla'
Expand Down
38 changes: 38 additions & 0 deletions tavla/src/Shared/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import pino, { Logger } from 'pino'

const PinoLevelToSeverityLookup: { [key: string]: string } = {
trace: 'DEBUG',
debug: 'DEBUG',
info: 'INFO',
warn: 'WARNING',
error: 'ERROR',
fatal: 'CRITICAL',
}

const defaultPinoConf = {
messageKey: 'message',
formatters: {
level(label: string, number: number) {
return {
severity:
PinoLevelToSeverityLookup[label] ||
PinoLevelToSeverityLookup['info'],
level: number,
}
},
},
}

export const logger: Logger =
process.env.COMMON_ENV === 'prd'
? pino({ level: 'warn', ...defaultPinoConf })
: pino({
transport: {
target: 'pino-pretty',
options: {
colorize: true,
},
},
level: 'debug',
...defaultPinoConf,
})
Loading

0 comments on commit a166f01

Please sign in to comment.