mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-11 00:08:30 +01:00
feat: add option for LOG_LEVEL (#803)
This commit is contained in:
parent
c729f514cf
commit
b4b524a11b
@ -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([
|
||||
|
@ -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 },
|
||||
);
|
||||
|
@ -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`);
|
||||
}
|
||||
|
@ -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}`);
|
||||
|
@ -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<IUserPermission[]> {
|
||||
|
@ -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<IDBOption>;
|
||||
session?: Partial<ISessionOption>;
|
||||
getLogger?: LogProvider;
|
||||
logLevel?: LogLevel;
|
||||
server?: Partial<IServerOption>;
|
||||
versionCheck?: Partial<IVersionOption>;
|
||||
authentication?: Partial<IAuthOption>;
|
||||
|
@ -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,
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user