Skip to content

Commit

Permalink
Add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasodonnell committed Aug 28, 2023
1 parent 0375232 commit df2f794
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "odo-stream",
"version": "1.3.0",
"version": "1.3.1",
"scripts": {
"dev": "next dev",
"build": "next build",
Expand Down
21 changes: 18 additions & 3 deletions src/app/api/live/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,29 @@ import { NextRequest, NextResponse } from 'next/server'

import { RS_URL, STREAM_TOKEN_COOKIE_NAME } from '../../../constants'
import { verify } from '../../../lib/jwt'
import * as logger from '../../../lib/logger'

export const dynamic = 'force-dynamic'
export const fetchCache = 'force-no-store'
export const revalidate = 0

export async function GET(request: NextRequest): Promise<Response> {
try {
const token = request.cookies.get(STREAM_TOKEN_COOKIE_NAME)
const claims = token ? await verify(token.value) : undefined
const ip =
request.headers.get('x-real-ip') ||
request.headers.get('x-forwarded-for') ||
request.ip
const token = request.cookies.get(STREAM_TOKEN_COOKIE_NAME)
const claims = token ? await verify(token.value) : undefined

const meta = {
geo: request.geo?.city as string,
ip: ip as string,
viewerId: claims?.viewerId as string,
}

logger.info('Live request', meta)

try {
if (!claims) {
throw HttpError.Unauthorized('Invalid token')
}
Expand All @@ -31,6 +44,8 @@ export async function GET(request: NextRequest): Promise<Response> {
cause: e,
})

logger.error(`Live error: ${error.message}`, meta)

return new NextResponse(error.message)
}
}
2 changes: 1 addition & 1 deletion src/components/player.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const hlsConfig = {
initialLiveManifestSize: 2, // preload 2 chunks before autostart
liveDurationInfinity: true, // instructs browser that video is live
liveMaxLatencyDuration: 10, // if higher than this, adujst to liveSyncDuration
liveSyncDuration: 3, // how close to live to target? shorter than 3sec causes frequent buffering issues
liveSyncDuration: 5, // how close to live to target? shorter than 3sec causes frequent buffering issues
lowLatencyMode: true, // enable low latency mode
maxBufferLength: 10, // limit forward buffer
maxLiveSyncPlaybackRate: 2, // if running behind, speed up video
Expand Down
26 changes: 26 additions & 0 deletions src/lib/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export enum LOG_LEVEL {
INFO = 'INFO',
WARN = 'WARN',
ERROR = 'ERROR',
}

const loggerFn = {
[LOG_LEVEL.ERROR]: console.error,
[LOG_LEVEL.INFO]: console.log,
[LOG_LEVEL.WARN]: console.warn,
}

export function log(level: LOG_LEVEL) {
return (message: string, meta: Record<string, string> = {}) => {
const metaString = Object.entries(meta)
.filter(([, value]) => Boolean(value))
.map(([key, value]) => `[${key}=${value}]`)
.join('')

loggerFn[level](`[${level}]${metaString} ${message}`)
}
}

export const info = log(LOG_LEVEL.INFO)
export const warn = log(LOG_LEVEL.WARN)
export const error = log(LOG_LEVEL.ERROR)

0 comments on commit df2f794

Please sign in to comment.