-
Notifications
You must be signed in to change notification settings - Fork 1
#174: Add User Action Logging #204
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
Open
JamesTLopez
wants to merge
27
commits into
main
Choose a base branch
from
feat/174-lyric-logging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
d47038f
feat(middleware): actionLogger middleware
JamesTLopez a1b2f81
fix: pnpm-workspace push
JamesTLopez f22c5c0
feat(routes): add action logger middleware
JamesTLopez 3f14bef
chore: code cleanup
JamesTLopez 6bcfce8
fix: eslint fix
JamesTLopez 383cb4b
chore: simplify comment
JamesTLopez 026b3ed
chore: remove useless utils
JamesTLopez 4bdbece
fix: categoryRouter using hardcoded boolean
JamesTLopez 3d9ffbc
Merge branch 'main' into feat/174-lyric-logging
JamesTLopez e8f57ab
feat: re-add params/body extraction
JamesTLopez 7e45905
feat: add loggerEnabled config
JamesTLopez 6f9e7a7
feat: update loggerEnabled in worker
JamesTLopez 40c2a68
feat: move logger up
JamesTLopez 4c1fa26
fix: add user to logger and remove duration errormessage
JamesTLopez b2d4d0d
fix: pass user to formatActionLog
JamesTLopez 0fd47bc
feat: attach requestContext to errorHandler
JamesTLopez b09bf7a
fix: remove logging from controllers
JamesTLopez af55190
fix: rename disableLogger
JamesTLopez 5dc7893
fix: remove debug comment
JamesTLopez d2e21cf
feat: update comment
JamesTLopez d3aa3ca
Merge branch 'main' into feat/174-lyric-logging
JamesTLopez 37b81d8
Merge branch 'main' into feat/174-lyric-logging
JamesTLopez ca86a3d
fix: change send to json
JamesTLopez 3067cb9
fix: rename logger config
JamesTLopez bca1612
fix: remove should log route
JamesTLopez cd6c961
fix: update naming
JamesTLopez 9890e16
fix: flip enable condition
JamesTLopez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,48 @@ | ||
| import type { NextFunction, Response } from 'express'; | ||
|
|
||
| import type { Logger } from '../config/logger.js'; | ||
| import { ActionResult, extractActionMetadata, formatActionLog } from '../utils/actionLoggerUtils.js'; | ||
| import type { RequestWithUser } from './auth.js'; | ||
|
|
||
| export type ActionLoggerConfig = { | ||
| enabled: boolean; | ||
| }; | ||
|
|
||
| /** | ||
| * Action Logger Middleware | ||
| * This middleware should be placed after authMiddleware in the router chain. | ||
| */ | ||
| export const actionLoggerMiddleware = (config: ActionLoggerConfig, logger: Logger) => { | ||
| return (req: RequestWithUser, res: Response, next: NextFunction) => { | ||
| // Skip logging if disabled | ||
| if (!config.enabled) { | ||
| return next(); | ||
| } | ||
| // Extract request metadata | ||
| const metadata = extractActionMetadata(req); | ||
| const startTime = Date.now(); | ||
|
|
||
| const logAction = (statusCode: number) => { | ||
| const duration = Date.now() - startTime; | ||
| const statusResult = statusCode >= 200 && statusCode < 400 ? ActionResult.ALLOWED : ActionResult.DENIED; | ||
|
|
||
| const logMessage = formatActionLog(metadata, statusResult, statusCode, duration); | ||
|
|
||
| // Use appropriate log level based on statusCode returned | ||
| if (statusResult === ActionResult.DENIED) { | ||
| logger.warn(logMessage); | ||
| return; | ||
| } | ||
| logger.info(logMessage); | ||
| }; | ||
|
|
||
| /** | ||
| * Log the action after response is sent | ||
| */ | ||
| res.on('finish', () => { | ||
| logAction(res.statusCode); | ||
| }); | ||
|
|
||
| next(); | ||
| }; | ||
| }; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,69 @@ | ||
| import type { RequestWithUser } from '../middleware/auth.js'; | ||
|
|
||
| export const ActionType = { | ||
| READ: 'READ', | ||
| WRITE: 'WRITE', | ||
| } as const; | ||
|
|
||
| export type ActionTypeValues = (typeof ActionType)[keyof typeof ActionType]; | ||
|
|
||
| export const ActionResult = { | ||
| ALLOWED: 'ALLOWED', | ||
| DENIED: 'DENIED', | ||
| } as const; | ||
|
|
||
| export type ActionResultValues = (typeof ActionResult)[keyof typeof ActionResult]; | ||
|
|
||
| export interface ActionLogMetadata { | ||
| action: ActionTypeValues; | ||
| method: string; | ||
| path: string; | ||
| categoryId?: number; | ||
| organization?: string; | ||
| userId?: string; | ||
| entityName?: string; | ||
| systemId?: string; | ||
| submissionId?: number; | ||
|
JamesTLopez marked this conversation as resolved.
|
||
| } | ||
|
|
||
| /** | ||
| * Determines if an HTTP method represents a read or write operation | ||
| */ | ||
| export const getActionType = (method: string): ActionTypeValues => { | ||
| return method === 'GET' || method === 'HEAD' ? ActionType.READ : ActionType.WRITE; | ||
| }; | ||
|
|
||
| /** | ||
| * Extracts all relevant metadata from a request for action logging | ||
| */ | ||
| export const extractActionMetadata = (req: RequestWithUser): ActionLogMetadata => { | ||
| return { | ||
| action: getActionType(req.method), | ||
| method: req.method, | ||
| path: req.originalUrl || req.path, | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Formats action log metadata into a readable string for logging | ||
| */ | ||
| export const formatActionLog = ( | ||
| metadata: ActionLogMetadata, | ||
| statusResult: ActionResultValues, | ||
| statusCode: number, | ||
| duration: number, | ||
| errorMessage?: string, | ||
| ): string => { | ||
| const actionLogResult = [`ACTION_LOG - PATH=${metadata.path}`, `type=|${metadata.action}-${metadata.method}|`]; | ||
|
|
||
| actionLogResult.push(`userId: ${metadata.userId || 'null'}`); | ||
| actionLogResult.push(`result: ${statusResult}`); | ||
| actionLogResult.push(`status: ${statusCode}`); | ||
| actionLogResult.push(`duration: ${duration}ms`); | ||
|
|
||
| if (errorMessage) { | ||
| actionLogResult.push(`error: ${errorMessage}`); | ||
| } | ||
|
|
||
| return actionLogResult.join(' | '); | ||
| }; | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
having
actionLoggerMiddlewareafter theauthMiddlewarewon't be called on an unauthorized request.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the case of
clinicalthere's already an implementation of a request logger, which I think will duplicate some information..