Code coverage report for core/src/log/log-config.ts

Statements: 41.67% (10 / 24)      Branches: 20.83% (5 / 24)      Functions: 60% (3 / 5)      Lines: 39.13% (9 / 23)      Ignored: none     

All files » core/src/log/ » log-config.ts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 661     1                       1   23 23 23 23 23         23                                                                              
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);
  }
}