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,
|
IListeningPipe,
|
||||||
IListeningHost,
|
IListeningHost,
|
||||||
} from './types/option';
|
} from './types/option';
|
||||||
import { defaultLogProvider, validateLogProvider } from './logger';
|
import { getDefaultLogProvider, LogLevel, validateLogProvider } from './logger';
|
||||||
|
|
||||||
const safeToUpper = (s: string) => (s ? s.toUpperCase() : s);
|
const safeToUpper = (s: string) => (s ? s.toUpperCase() : s);
|
||||||
|
|
||||||
@ -143,7 +143,9 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig {
|
|||||||
options.session,
|
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);
|
validateLogProvider(getLogger);
|
||||||
|
|
||||||
const server: IServerOption = mergeAll([
|
const server: IServerOption = mergeAll([
|
||||||
|
@ -3,10 +3,6 @@
|
|||||||
const test = require('ava');
|
const test = require('ava');
|
||||||
const logger = require('./logger');
|
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 => {
|
test('should require custom logger to implement info', t => {
|
||||||
const loggerImpl = {
|
const loggerImpl = {
|
||||||
debug: () => {},
|
debug: () => {},
|
||||||
@ -16,7 +12,7 @@ test('should require custom logger to implement info', t => {
|
|||||||
const provider = () => loggerImpl;
|
const provider = () => loggerImpl;
|
||||||
const error = t.throws(
|
const error = t.throws(
|
||||||
() => {
|
() => {
|
||||||
logger.setLoggerProvider(provider)();
|
logger.validateLogProvider(provider)();
|
||||||
},
|
},
|
||||||
{ instanceOf: TypeError },
|
{ instanceOf: TypeError },
|
||||||
);
|
);
|
||||||
@ -32,7 +28,7 @@ test('should require custom logger to implement error', t => {
|
|||||||
const provider = () => loggerImpl;
|
const provider = () => loggerImpl;
|
||||||
const error = t.throws(
|
const error = t.throws(
|
||||||
() => {
|
() => {
|
||||||
logger.setLoggerProvider(provider)();
|
logger.validateLogProvider(provider)();
|
||||||
},
|
},
|
||||||
{ instanceOf: TypeError },
|
{ instanceOf: TypeError },
|
||||||
);
|
);
|
||||||
|
@ -1,7 +1,15 @@
|
|||||||
import { configure, getLogger, levels } from 'log4js';
|
import { configure, getLogger } from 'log4js';
|
||||||
|
|
||||||
export type LogProvider = (category?: string) => Logger;
|
export type LogProvider = (category?: string) => Logger;
|
||||||
|
|
||||||
|
export enum LogLevel {
|
||||||
|
debug = 'debug',
|
||||||
|
info = 'info',
|
||||||
|
warn = 'warn',
|
||||||
|
error = 'error',
|
||||||
|
fatal = 'fatal',
|
||||||
|
}
|
||||||
|
|
||||||
export interface Logger {
|
export interface Logger {
|
||||||
debug(message: any, ...args: any[]): void;
|
debug(message: any, ...args: any[]): void;
|
||||||
info(message: any, ...args: any[]): void;
|
info(message: any, ...args: any[]): void;
|
||||||
@ -10,22 +18,15 @@ export interface Logger {
|
|||||||
fatal(message: any, ...args: any[]): void;
|
fatal(message: any, ...args: any[]): void;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDefaultLogProvider(): LogProvider {
|
export function getDefaultLogProvider(
|
||||||
let level: string;
|
logLevel: LogLevel = LogLevel.error,
|
||||||
if (process.env.NODE_ENV === 'production') {
|
): LogProvider {
|
||||||
level = levels.ERROR.levelStr;
|
|
||||||
} else if (process.env.NODE_ENV === 'test') {
|
|
||||||
level = levels.FATAL.levelStr;
|
|
||||||
} else {
|
|
||||||
level = levels.DEBUG.levelStr;
|
|
||||||
}
|
|
||||||
|
|
||||||
configure({
|
configure({
|
||||||
appenders: {
|
appenders: {
|
||||||
console: { type: 'console' },
|
console: { type: 'console' },
|
||||||
},
|
},
|
||||||
categories: {
|
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.warn === 'function', 'Logger must implement warn');
|
||||||
validate(typeof logger.error === 'function', 'Logger must implement error');
|
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) {
|
module.exports = function(config) {
|
||||||
const logger = config.getLogger('HTTP');
|
const logger = config.getLogger('HTTP');
|
||||||
|
const enable = config.server.enableRequestLogger;
|
||||||
return (req, res, next) => {
|
return (req, res, next) => {
|
||||||
if (config.enableRequestLogger) {
|
if (enable) {
|
||||||
res.on('finish', () => {
|
res.on('finish', () => {
|
||||||
const { pathname } = url.parse(req.originalUrl);
|
const { pathname } = url.parse(req.originalUrl);
|
||||||
logger.info(`${res.statusCode} ${req.method} ${pathname}`);
|
logger.info(`${res.statusCode} ${req.method} ${pathname}`);
|
||||||
|
@ -135,12 +135,16 @@ export class AccessService {
|
|||||||
p.project === projectId ||
|
p.project === projectId ||
|
||||||
p.project === ALL_PROJECTS,
|
p.project === ALL_PROJECTS,
|
||||||
)
|
)
|
||||||
.some(p => p.permission === permission || p.permission === ADMIN);
|
.some(
|
||||||
|
p => p.permission === permission || p.permission === ADMIN,
|
||||||
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error(`Error checking permission=${permission}, userId=${user.id} projectId=${projectId}`, e);
|
this.logger.error(
|
||||||
|
`Error checking permission=${permission}, userId=${user.id} projectId=${projectId}`,
|
||||||
|
e,
|
||||||
|
);
|
||||||
return Promise.resolve(false);
|
return Promise.resolve(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getPermissionsForUser(user: User): Promise<IUserPermission[]> {
|
async getPermissionsForUser(user: User): Promise<IUserPermission[]> {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { LogProvider } from '../logger';
|
import { LogLevel, LogProvider } from '../logger';
|
||||||
|
|
||||||
export interface ISSLOption {
|
export interface ISSLOption {
|
||||||
rejectUnauthorized: boolean;
|
rejectUnauthorized: boolean;
|
||||||
@ -73,6 +73,7 @@ export interface IUnleashOptions {
|
|||||||
db?: Partial<IDBOption>;
|
db?: Partial<IDBOption>;
|
||||||
session?: Partial<ISessionOption>;
|
session?: Partial<ISessionOption>;
|
||||||
getLogger?: LogProvider;
|
getLogger?: LogProvider;
|
||||||
|
logLevel?: LogLevel;
|
||||||
server?: Partial<IServerOption>;
|
server?: Partial<IServerOption>;
|
||||||
versionCheck?: Partial<IVersionOption>;
|
versionCheck?: Partial<IVersionOption>;
|
||||||
authentication?: Partial<IAuthOption>;
|
authentication?: Partial<IAuthOption>;
|
||||||
|
@ -2,8 +2,10 @@
|
|||||||
|
|
||||||
import unleash from './lib/server-impl';
|
import unleash from './lib/server-impl';
|
||||||
import { createConfig } from './lib/create-config';
|
import { createConfig } from './lib/create-config';
|
||||||
|
import { LogLevel } from './lib/logger';
|
||||||
|
|
||||||
unleash.start(createConfig({
|
unleash.start(
|
||||||
|
createConfig({
|
||||||
db: {
|
db: {
|
||||||
user: 'unleash_user',
|
user: 'unleash_user',
|
||||||
password: 'passord',
|
password: 'passord',
|
||||||
@ -15,8 +17,10 @@ unleash.start(createConfig({
|
|||||||
server: {
|
server: {
|
||||||
enableRequestLogger: true,
|
enableRequestLogger: true,
|
||||||
},
|
},
|
||||||
|
logLevel: LogLevel.debug,
|
||||||
enableOAS: true,
|
enableOAS: true,
|
||||||
versionCheck: {
|
versionCheck: {
|
||||||
enable: false,
|
enable: false,
|
||||||
},
|
},
|
||||||
}));
|
}),
|
||||||
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user