generated from MapColonies/ts-server-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: added many logs * feat: added operationId to http log
- Loading branch information
1 parent
37053d7
commit 3d2fc0e
Showing
16 changed files
with
325 additions
and
80 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,60 @@ | ||
import { AsyncLocalStorage } from 'node:async_hooks'; | ||
import type { IncomingMessage, ServerResponse } from 'node:http'; | ||
import { get } from 'lodash'; | ||
import jsLogger, { Logger, LoggerOptions } from '@map-colonies/js-logger'; | ||
import { getOtelMixin } from '@map-colonies/telemetry'; | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { DependencyContainer } from 'tsyringe'; | ||
import { SERVICES } from './constants'; | ||
import { IConfig } from './interfaces'; | ||
|
||
const logContext = new AsyncLocalStorage<object>(); | ||
|
||
export function addOperationIdToLog(req: IncomingMessage, res: ServerResponse, loggableObject: Record<string, unknown>): unknown { | ||
const operationId = get(req, 'openapi.schema.operationId') as string | undefined; | ||
if (operationId !== undefined) { | ||
loggableObject['operationId'] = operationId; | ||
} | ||
return loggableObject; | ||
} | ||
|
||
export function enrichLogContext(values: object): void { | ||
const store = logContext.getStore(); | ||
if (store) { | ||
Object.assign(store, values); | ||
} | ||
} | ||
|
||
export function loggerFactory(container: DependencyContainer): Logger { | ||
const config = container.resolve<IConfig>(SERVICES.CONFIG); | ||
const loggerConfig = config.get<LoggerOptions>('telemetry.logger'); | ||
|
||
const logger = jsLogger({ | ||
...loggerConfig, | ||
mixin: (mergeObj, level) => { | ||
const otelMixin = getOtelMixin(); | ||
const store = logContext.getStore(); | ||
return { ...otelMixin(mergeObj, level), ...store }; | ||
}, | ||
}); | ||
|
||
return logger; | ||
} | ||
|
||
export function logContextInjectionMiddleware(req: Request, res: Response, next: NextFunction): void { | ||
logContext.run({}, () => { | ||
next(); | ||
}); | ||
} | ||
|
||
export function logEnrichmentParamMiddlewareFactory( | ||
logEntry: string | ||
): (req: Request, res: Response, next: NextFunction, paramValue: unknown) => void { | ||
return function (req: Request, res: Response, next: NextFunction, paramValue: unknown): void { | ||
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions | ||
if (paramValue) { | ||
enrichLogContext({ [logEntry]: paramValue }); | ||
} | ||
next(); | ||
}; | ||
} |
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
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
Oops, something went wrong.