Permalink
Cannot retrieve contributors at this time
104 lines (90 sloc)
2.51 KB
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
directus/api/src/logger.ts /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
| import { Request, RequestHandler } from 'express'; | |
| import pino, { LoggerOptions } from 'pino'; | |
| import pinoHTTP, { stdSerializers } from 'pino-http'; | |
| import { getConfigFromEnv } from './utils/get-config-from-env'; | |
| import { URL } from 'url'; | |
| import env from './env'; | |
| import { toArray } from '@directus/shared/utils'; | |
| import { merge } from 'lodash'; | |
| const pinoOptions: LoggerOptions = { | |
| level: env.LOG_LEVEL || 'info', | |
| redact: { | |
| paths: ['req.headers.authorization', `req.cookies.${env.REFRESH_TOKEN_COOKIE_NAME}`], | |
| censor: '--redact--', | |
| }, | |
| }; | |
| const httpLoggerOptions: LoggerOptions = { | |
| level: env.LOG_LEVEL || 'info', | |
| redact: { | |
| paths: ['req.headers.authorization', `req.cookies.${env.REFRESH_TOKEN_COOKIE_NAME}`], | |
| censor: '--redact--', | |
| }, | |
| }; | |
| if (env.LOG_STYLE !== 'raw') { | |
| pinoOptions.transport = { | |
| target: 'pino-pretty', | |
| options: { | |
| ignore: 'hostname,pid', | |
| sync: true, | |
| }, | |
| }; | |
| httpLoggerOptions.transport = { | |
| target: 'pino-http-print', | |
| options: { | |
| all: true, | |
| translateTime: 'SYS:HH:MM:ss', | |
| relativeUrl: true, | |
| prettyOptions: { | |
| ignore: 'hostname,pid', | |
| sync: true, | |
| }, | |
| }, | |
| }; | |
| } | |
| const loggerEnvConfig = getConfigFromEnv('LOGGER_', 'LOGGER_HTTP'); | |
| // Expose custom log levels into formatter function | |
| if (loggerEnvConfig.levels) { | |
| const customLogLevels: { [key: string]: string } = {}; | |
| for (const el of toArray(loggerEnvConfig.levels)) { | |
| const key_val = el.split(':'); | |
| customLogLevels[key_val[0].trim()] = key_val[1].trim(); | |
| } | |
| pinoOptions.formatters = { | |
| level(label: string, number: any) { | |
| return { | |
| severity: customLogLevels[label] || 'info', | |
| level: number, | |
| }; | |
| }, | |
| }; | |
| httpLoggerOptions.formatters = { | |
| level(label: string, number: any) { | |
| return { | |
| severity: customLogLevels[label] || 'info', | |
| level: number, | |
| }; | |
| }, | |
| }; | |
| delete loggerEnvConfig.levels; | |
| } | |
| const logger = pino(merge(pinoOptions, loggerEnvConfig)); | |
| const httpLoggerEnvConfig = getConfigFromEnv('LOGGER_HTTP', ['LOGGER_HTTP_LOGGER']); | |
| export const expressLogger = pinoHTTP({ | |
| logger: pino(merge(httpLoggerOptions, loggerEnvConfig)), | |
| ...httpLoggerEnvConfig, | |
| serializers: { | |
| req(request: Request) { | |
| const output = stdSerializers.req(request); | |
| output.url = redactQuery(output.url); | |
| return output; | |
| }, | |
| }, | |
| }) as RequestHandler; | |
| export default logger; | |
| function redactQuery(originalPath: string) { | |
| const url = new URL(originalPath, 'http://example.com/'); | |
| if (url.searchParams.has('access_token')) { | |
| url.searchParams.set('access_token', '--redacted--'); | |
| } | |
| return url.pathname + url.search; | |
| } |