|
| 1 | +import _ from 'underscore'; |
| 2 | +import { |
| 3 | + ILogger, |
| 4 | + LogObject, |
| 5 | + LoggerConfig, |
| 6 | + LogLevel, |
| 7 | + DebugLogObject, |
| 8 | +} from './logging-types'; |
| 9 | +import levels from './levels'; |
| 10 | +import isDebugLevelOrMoreVerbose from './is-debug-level-or-more-verbose'; |
| 11 | +import { LambdaEventSourceType } from '../request-response-types'; |
| 12 | + |
| 13 | +export default class ConsoleLogger implements ILogger { |
| 14 | + |
| 15 | + protected _level: LogLevel; |
| 16 | + protected _interface: LambdaEventSourceType; |
| 17 | + protected _getTimeUntilFnTimeout: () => number; |
| 18 | + protected _fnStartTime: number; |
| 19 | + |
| 20 | + public constructor(config: LoggerConfig) { |
| 21 | + this._level = config.level || 'info'; |
| 22 | + this._interface = config.interface; |
| 23 | + this._fnStartTime = typeof config.fnStartTime === 'undefined' ? Date.now() : config.fnStartTime; |
| 24 | + this._getTimeUntilFnTimeout = config.getTimeUntilFnTimeout; |
| 25 | + } |
| 26 | + |
| 27 | + public trace(msg: string, data?: unknown): void { |
| 28 | + this._log('trace', msg, data); |
| 29 | + } |
| 30 | + |
| 31 | + public debug(msg: string, data?: unknown): void { |
| 32 | + this._log('debug', msg, data); |
| 33 | + } |
| 34 | + |
| 35 | + public info(msg: string, data?: unknown): void { |
| 36 | + this._log('info', msg, data); |
| 37 | + } |
| 38 | + |
| 39 | + public warn(msg: string, data?: unknown): void { |
| 40 | + this._log('warn', msg, data); |
| 41 | + } |
| 42 | + |
| 43 | + public error(msg: string, data?: unknown): void { |
| 44 | + this._log('error', msg, data); |
| 45 | + } |
| 46 | + |
| 47 | + public fatal(msg: string, data?: unknown): void { |
| 48 | + this._log('fatal', msg, data); |
| 49 | + } |
| 50 | + |
| 51 | + public getLevel(): LogLevel { |
| 52 | + return this._level; |
| 53 | + } |
| 54 | + |
| 55 | + public setLevel(level: LogLevel): void { |
| 56 | + this._level = level; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Perform the actual message logging |
| 61 | + */ |
| 62 | + protected _log(level: LogLevel, msg: string, data?: unknown): void { |
| 63 | + if (this._shouldLog(level)) { |
| 64 | + // eslint-disable-next-line no-console |
| 65 | + console.log(JSON.stringify(this._makeLogObject(level, msg, data))); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @returns `true` if the given level should be logged at this logger's current log |
| 71 | + * level setting. |
| 72 | + */ |
| 73 | + protected _shouldLog(level: LogLevel): boolean { |
| 74 | + // Log if the level is higher priority than the current log level setting. |
| 75 | + // e.g. error (50) >= info (30) |
| 76 | + return levels[level] >= levels[this._level]; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Creates an object to be logged |
| 81 | + */ |
| 82 | + protected _makeLogObject(level: LogLevel, msg: string, data?: unknown): LogObject | DebugLogObject { |
| 83 | + let logLine: LogObject = { level, msg }; |
| 84 | + |
| 85 | + if (!_.isUndefined(data)) { |
| 86 | + logLine.data = data; |
| 87 | + } |
| 88 | + |
| 89 | + if (isDebugLevelOrMoreVerbose(level)) { |
| 90 | + let debugLogLine = logLine as DebugLogObject; |
| 91 | + |
| 92 | + debugLogLine.int = this._interface; |
| 93 | + debugLogLine.remaining = this._getTimeUntilFnTimeout(); |
| 94 | + debugLogLine.timer = this._getTimeSinceFnStart(); |
| 95 | + |
| 96 | + return debugLogLine; |
| 97 | + } |
| 98 | + |
| 99 | + return logLine; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * The approximate time, in milliseconds, since the Lambda function started executing. |
| 104 | + */ |
| 105 | + protected _getTimeSinceFnStart(): number { |
| 106 | + return Date.now() - this._fnStartTime; |
| 107 | + } |
| 108 | +} |
0 commit comments