From ee7e678f24fd85846798bc3cc1af3103e728ebb7 Mon Sep 17 00:00:00 2001 From: keesvanbemmel Date: Wed, 12 Jan 2022 16:41:28 +0100 Subject: [PATCH] Adds possible extra pino logger options through env vars (#10911) * Added missing data field to sessions yaml * rebased sessions.yaml * Added custom pino logger options from env vars * Added LOGGER_ explanation to docs * Added similar functionality for pino http, so level of request logs can be set --- api/src/logger.ts | 29 +++++++++++++++++++++++++++- docs/configuration/config-options.md | 14 ++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/api/src/logger.ts b/api/src/logger.ts index 40b96aef44..0e3332233c 100644 --- a/api/src/logger.ts +++ b/api/src/logger.ts @@ -1,6 +1,7 @@ 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'; @@ -17,11 +18,37 @@ if (env.LOG_STYLE !== 'raw') { pinoOptions.prettifier = require('pino-colada'); } -const logger = pino(pinoOptions); +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 loggerEnvConfig.levels.split(',')) { + 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, + }; + }, + }; + + delete loggerEnvConfig.levels; +} + +const logger = pino(Object.assign(pinoOptions, loggerEnvConfig)); + +const httpLoggerEnvConfig = getConfigFromEnv('LOGGER_HTTP', ['LOGGER_HTTP_LOGGER']); export const expressLogger = pinoHTTP( { logger, + ...httpLoggerEnvConfig, }, { serializers: { diff --git a/docs/configuration/config-options.md b/docs/configuration/config-options.md index cceb410172..e15e8b4820 100644 --- a/docs/configuration/config-options.md +++ b/docs/configuration/config-options.md @@ -194,6 +194,20 @@ prefixing the value with `{type}:`. The following types are available: [1] The PUBLIC_URL value is used for things like OAuth redirects, forgot-password emails, and logos that needs to be publicly available on the internet. +::: tip Additional Logger Variables + +All `LOGGER_*` environment variables are passed to the `options` configuration of a +[`Pino` instance](https://github.com/pinojs/pino/blob/master/docs/api.md#options). All `LOGGER_HTTP*` environment +variables are passed to the `options` configuration of a +[`Pino-http` instance](https://github.com/pinojs/pino-http#api). Based on your project's needs, you can extend the +`LOGGER_*` environment variables with any config you need to pass to the logger instance. If a LOGGER_LEVELS key is +added, these values will be passed to the logger formatter, as described +[here](https://github.com/pinojs/pino/blob/master/docs/help.md#mapping-pino-log-levels-to-google-cloud-logging-stackdriver-serverity-levels) +for example. The format for adding LEVELS values is: +`LOGGER_LEVELS="trace:DEBUG,debug:DEBUG,info:INFO,warn:WARNING,error:ERROR,fatal:CRITICAL"` + +::: + ## Database | Variable | Description | Default Value |