All files / src logger-factory.ts

100% Statements 17/17
77.27% Branches 17/22
100% Functions 5/5
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92                              9x       9x                         9x   2x 2x 2x 2x     2x       3x       3x   3x 1x 1x       3x                                   9x   55x 55x                            
/* 
  Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  
  Licensed under the Apache License, Version 2.0 (the "License").
  You may not use this file except in compliance with the License.
  You may obtain a copy of the License at
  
      http://www.apache.org/licenses/LICENSE-2.0
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
import * as winston from "winston";
import * as Transport from "winston-transport";
import { Logger } from "./logger-type";
 
const DEFAULT_LOG_LEVEL = "debug";
export type LogLevel =
  | "error"
  | "warn"
  | "info"
  | "verbose"
  | "debug"
  | "silly";
 
export interface LoggerFactory {
  getLogger(name: string, logLevel?: LogLevel): Logger;
}
 
export class LambdaLoggerFactory<TEvent, TContext> implements LoggerFactory {
  constructor(
    private event: TEvent,
    private context: TContext,
    private runLocally?: boolean,
    private additionalData?: {
      [key: string]: (event: TEvent, context: TContext) => string;
    },
    private logLevel?: LogLevel
  ) {}
 
  static customTransports(): Transport[] {
    return [new winston.transports.Console()];
  }
 
  getLogger(name: string, logLevel?: LogLevel): Logger {
    const metadata: { [key: string]: string } = {};
 
    if (this.additionalData) {
      Object.entries(this.additionalData).forEach(([key, op]) => {
        metadata[key] = op ? op(this.event, this.context) : "";
      });
    }
 
    return winston.createLogger({
      defaultMeta: {
        ...metadata,
      },
      transports: LambdaLoggerFactory.customTransports(),
      format: winston.format.combine(
        winston.format.label({ label: name }),
        winston.format.timestamp(),
        winston.format.splat(),
        this.runLocally
          ? winston.format.prettyPrint({ colorize: true })
          : winston.format.json()
      ),
      level: logLevel ?? this.logLevel ?? DEFAULT_LOG_LEVEL,
    });
  }
}
 
export class StaticLoggerFactory implements LoggerFactory {
  getLogger(name: string, logLevel?: LogLevel): Logger {
    const isTestEnvironment = process.env.NODE_ENV === "test";
    return winston.createLogger({
      transports: isTestEnvironment
        ? []
        : LambdaLoggerFactory.customTransports(),
      format: winston.format.combine(
        winston.format.label({ label: name }),
        winston.format.timestamp(),
        winston.format.splat(),
        winston.format.json()
      ),
      level: logLevel ?? isTestEnvironment ? "error" : "debug",
    });
  }
}