-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
176 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { createContext, useCallback, useContext, useEffect } from 'react' | ||
import { useMe } from './me' | ||
|
||
const LoggerContext = createContext() | ||
|
||
export function LoggerProvider ({ children }) { | ||
const me = useMe() | ||
|
||
const logLevel = useCallback(level => { | ||
return async (event) => { | ||
if (!me?.clientSideLogging) return | ||
const env = { | ||
userAgent: window.navigator.userAgent | ||
} | ||
const body = { | ||
level, | ||
env, | ||
event: typeof event === 'string' ? { message: event } : event | ||
} | ||
await fetch('/api/log', { | ||
method: 'post', | ||
headers: { | ||
'Content-type': 'application/json' | ||
}, | ||
body: JSON.stringify(body) | ||
}).catch(console.error) | ||
} | ||
}, [me?.clientSideLogging]) | ||
|
||
const logger = { | ||
log: logLevel('log'), | ||
warn: logLevel('warn'), | ||
error: logLevel('error') | ||
} | ||
|
||
useEffect(() => { | ||
const channel = new BroadcastChannel('log') | ||
channel.onmessage = event => { | ||
const level = event.data?.level || 'log' | ||
delete event.data?.level | ||
logger[level](event.data) | ||
} | ||
return () => channel.close() | ||
}, [logger]) | ||
|
||
return ( | ||
<LoggerContext.Provider value={logger}> | ||
{children} | ||
</LoggerContext.Provider> | ||
) | ||
} | ||
|
||
export function useLogger () { | ||
return useContext(LoggerContext) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { getServerSession } from 'next-auth' | ||
import models from '../../../api/models' | ||
import { getAuthOptions } from '../auth/[...nextauth]' | ||
|
||
export default async (req, res) => { | ||
const session = await getServerSession(req, res, getAuthOptions(req)) | ||
|
||
if (!session.user) return res.status(401).json({ status: 'unauthorized' }) | ||
|
||
// TODO: | ||
// Should we scramble user IDs by hashing? | ||
// If no key is involved, anyone with knowledge of user IDs could hash IDs and compare hashes. | ||
// If a key is involved during hashing, only we can compare hashes of user IDs. | ||
await models.log.create({ data: { data: { userId: session.user.id, ...req.body } } }) | ||
|
||
return res.status(200).json({ status: 'ok' }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
prisma/migrations/20230831054014_client_side_logging/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
-- AlterTable | ||
ALTER TABLE "users" ADD COLUMN "clientSideLogging" BOOLEAN NOT NULL DEFAULT false; | ||
|
||
-- CreateTable | ||
CREATE TABLE "Log" ( | ||
"id" SERIAL NOT NULL, | ||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"data" JSONB NOT NULL, | ||
|
||
CONSTRAINT "Log_pkey" PRIMARY KEY ("id") | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters