From b4b524a11b8dfd01d10d49de471f30f9504cd000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivar=20Conradi=20=C3=98sthus?= Date: Fri, 23 Apr 2021 15:31:12 +0200 Subject: [PATCH] feat: add option for LOG_LEVEL (#803) --- src/lib/create-config.ts | 6 +++-- src/lib/logger.test.js | 8 ++---- src/lib/logger.ts | 38 ++++++++++------------------ src/lib/middleware/request-logger.js | 3 ++- src/lib/services/access-service.ts | 12 ++++++--- src/lib/types/option.ts | 3 ++- src/server-dev.ts | 38 +++++++++++++++------------- 7 files changed, 52 insertions(+), 56 deletions(-) diff --git a/src/lib/create-config.ts b/src/lib/create-config.ts index d7d87cce1d..9fe88107b4 100644 --- a/src/lib/create-config.ts +++ b/src/lib/create-config.ts @@ -14,7 +14,7 @@ import { IListeningPipe, IListeningHost, } from './types/option'; -import { defaultLogProvider, validateLogProvider } from './logger'; +import { getDefaultLogProvider, LogLevel, validateLogProvider } from './logger'; const safeToUpper = (s: string) => (s ? s.toUpperCase() : s); @@ -143,7 +143,9 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig { options.session, ]); - const getLogger = options.getLogger || defaultLogProvider; + const logLevel = + options.logLevel || LogLevel[process.env.LOG_LEVEL] || LogLevel.error; + const getLogger = options.getLogger || getDefaultLogProvider(logLevel); validateLogProvider(getLogger); const server: IServerOption = mergeAll([ diff --git a/src/lib/logger.test.js b/src/lib/logger.test.js index fe5212a9a5..89d6369b1d 100644 --- a/src/lib/logger.test.js +++ b/src/lib/logger.test.js @@ -3,10 +3,6 @@ const test = require('ava'); const logger = require('./logger'); -test('should expose a setLoggerProvider function', t => { - t.true(logger.setLoggerProvider instanceof Function); -}); - test('should require custom logger to implement info', t => { const loggerImpl = { debug: () => {}, @@ -16,7 +12,7 @@ test('should require custom logger to implement info', t => { const provider = () => loggerImpl; const error = t.throws( () => { - logger.setLoggerProvider(provider)(); + logger.validateLogProvider(provider)(); }, { instanceOf: TypeError }, ); @@ -32,7 +28,7 @@ test('should require custom logger to implement error', t => { const provider = () => loggerImpl; const error = t.throws( () => { - logger.setLoggerProvider(provider)(); + logger.validateLogProvider(provider)(); }, { instanceOf: TypeError }, ); diff --git a/src/lib/logger.ts b/src/lib/logger.ts index a31f65bbdb..81edb566a8 100644 --- a/src/lib/logger.ts +++ b/src/lib/logger.ts @@ -1,7 +1,15 @@ -import { configure, getLogger, levels } from 'log4js'; +import { configure, getLogger } from 'log4js'; export type LogProvider = (category?: string) => Logger; +export enum LogLevel { + debug = 'debug', + info = 'info', + warn = 'warn', + error = 'error', + fatal = 'fatal', +} + export interface Logger { debug(message: any, ...args: any[]): void; info(message: any, ...args: any[]): void; @@ -10,22 +18,15 @@ export interface Logger { fatal(message: any, ...args: any[]): void; } -function getDefaultLogProvider(): LogProvider { - let level: string; - if (process.env.NODE_ENV === 'production') { - level = levels.ERROR.levelStr; - } else if (process.env.NODE_ENV === 'test') { - level = levels.FATAL.levelStr; - } else { - level = levels.DEBUG.levelStr; - } - +export function getDefaultLogProvider( + logLevel: LogLevel = LogLevel.error, +): LogProvider { configure({ appenders: { console: { type: 'console' }, }, categories: { - default: { appenders: ['console'], level }, + default: { appenders: ['console'], level: logLevel }, }, }); @@ -47,16 +48,3 @@ export function validateLogProvider(provider: LogProvider): void { validate(typeof logger.warn === 'function', 'Logger must implement warn'); validate(typeof logger.error === 'function', 'Logger must implement error'); } - -// Deprecated (TODO: remove this in v4) -let loggerProvider = getDefaultLogProvider(); -export const defaultLogProvider = loggerProvider; - -export function setLoggerProvider(provider: LogProvider): void { - validateLogProvider(provider); - - loggerProvider = provider; - const logger = provider('unleash:logger'); - logger.info(`Your way of configuring a logProvider is deprecated. - See https://docs.getunleash.io/docs/deploy/configuring_unleash for details`); -} diff --git a/src/lib/middleware/request-logger.js b/src/lib/middleware/request-logger.js index b478bc355b..33cc806822 100644 --- a/src/lib/middleware/request-logger.js +++ b/src/lib/middleware/request-logger.js @@ -4,8 +4,9 @@ const url = require('url'); module.exports = function(config) { const logger = config.getLogger('HTTP'); + const enable = config.server.enableRequestLogger; return (req, res, next) => { - if (config.enableRequestLogger) { + if (enable) { res.on('finish', () => { const { pathname } = url.parse(req.originalUrl); logger.info(`${res.statusCode} ${req.method} ${pathname}`); diff --git a/src/lib/services/access-service.ts b/src/lib/services/access-service.ts index a97086b4b7..a950955d00 100644 --- a/src/lib/services/access-service.ts +++ b/src/lib/services/access-service.ts @@ -135,12 +135,16 @@ export class AccessService { p.project === projectId || p.project === ALL_PROJECTS, ) - .some(p => p.permission === permission || p.permission === ADMIN); - } catch(e) { - this.logger.error(`Error checking permission=${permission}, userId=${user.id} projectId=${projectId}`, e); + .some( + p => p.permission === permission || p.permission === ADMIN, + ); + } catch (e) { + this.logger.error( + `Error checking permission=${permission}, userId=${user.id} projectId=${projectId}`, + e, + ); return Promise.resolve(false); } - } async getPermissionsForUser(user: User): Promise { diff --git a/src/lib/types/option.ts b/src/lib/types/option.ts index 43bc44ec02..8cbd21ee19 100644 --- a/src/lib/types/option.ts +++ b/src/lib/types/option.ts @@ -1,4 +1,4 @@ -import { LogProvider } from '../logger'; +import { LogLevel, LogProvider } from '../logger'; export interface ISSLOption { rejectUnauthorized: boolean; @@ -73,6 +73,7 @@ export interface IUnleashOptions { db?: Partial; session?: Partial; getLogger?: LogProvider; + logLevel?: LogLevel; server?: Partial; versionCheck?: Partial; authentication?: Partial; diff --git a/src/server-dev.ts b/src/server-dev.ts index 41d7777973..e5b5734871 100644 --- a/src/server-dev.ts +++ b/src/server-dev.ts @@ -2,21 +2,25 @@ import unleash from './lib/server-impl'; import { createConfig } from './lib/create-config'; +import { LogLevel } from './lib/logger'; -unleash.start(createConfig({ - db: { - user: 'unleash_user', - password: 'passord', - host: 'localhost', - port: 5432, - database: 'unleash', - ssl: false, - }, - server: { - enableRequestLogger: true, - }, - enableOAS: true, - versionCheck: { - enable: false, - }, -})); +unleash.start( + createConfig({ + db: { + user: 'unleash_user', + password: 'passord', + host: 'localhost', + port: 5432, + database: 'unleash', + ssl: false, + }, + server: { + enableRequestLogger: true, + }, + logLevel: LogLevel.debug, + enableOAS: true, + versionCheck: { + enable: false, + }, + }), +);