import {MessageTemplate, templateString} from './message-template';
import {Interceptor} from './interceptor';
import {Context} from './context';
import {LogLevel} from './LogLevel';
import {MessageFormat} from './message-format';
export type LogConfigOptions = {
rootLevel?: LogLevel;
messageTemplate?: MessageTemplate;
messageFormat?: MessageFormat;
interceptors?: Interceptor[];
contextSupplier?: Context;
};
export class LogConfig {
constructor(
public readonly rootLevel: LogLevel = LogLevel.INFO,
public readonly messageTemplate: MessageTemplate = templateString(),
public readonly interceptors: Interceptor[] = [],
public readonly contextSupplier: Context = (): any => ({}),
public readonly messageFormat?: MessageFormat
) {
}
static create(props: LogConfigOptions = {}): LogConfig {
return new LogConfig(props.rootLevel, props.messageTemplate, props.interceptors, props.contextSupplier, props.messageFormat);
}
extend(props: LogConfigOptions, merge = false): LogConfig {
props.rootLevel = props.rootLevel || this.rootLevel;
props.messageTemplate = props.messageTemplate || this.messageTemplate;
props.messageFormat = props.messageFormat || this.messageFormat;
let _contextSupplier: Context;
let _interceptors: Interceptor[];
if (merge) {
if (props.contextSupplier !== undefined) {
_contextSupplier = (): any => {
return {
...this.contextSupplier(),
...props.contextSupplier ? props.contextSupplier() : {},
}
}
} else {
_contextSupplier = this.contextSupplier;
}
if (props.interceptors) {
_interceptors = [
...this.interceptors,
...props.interceptors
];
} else {
_interceptors = [...this.interceptors];
}
} else {
_interceptors = props.interceptors || [...this.interceptors];
_contextSupplier = props.contextSupplier || this.contextSupplier;
}
return new LogConfig(props.rootLevel, props.messageTemplate, _interceptors, _contextSupplier, props.messageFormat);
}
}
|