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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | 1x 1x 1x 15x 15x 15x 15x 15x 15x 15x 15x 5x 5x 5x 2x 2x 8x 8x 8x 5x 5x 5x 5x 5x 2x 2x 6x 6x 6x 5x 5x 5x 1x 1x 4x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 22x 7x 14x 5x 1x 4x 7x 2x 2x 4x 1x 1x | import { prettyPrint } from './prettyPrint'; import { LogInput } from './types/LogInput'; import { LogInputError } from './types/LogInputError'; import { LogLevels } from './types/logLevels'; import { LogOptions } from './types/LogOptions'; import { LogOutput } from './types/LogOutput'; /** * Provides methods for writing logs to in different log-levels. Logs are written in JSON format to stdout using console.log(). */ class Logger { /** * This governs which logs will be written. If log level is * INFO all logs with INFO and above will be written. So: INFO * WARN and ERROR. */ public level: LogLevels = this.setInitialLogLevel(); /** * Sets a reference to the AWS lambda context object and will enrich the logs with context meta data to make it easier to track. */ public context: AWSLambda.Context | undefined; /** * The id that came from the service that called this lambda. * It is best practice to send this through a header called X-REQUEST-ID. * In most cases you would set this to that header if it exist. */ public xRequestId: string | undefined; /** * The user agent that called this service. * In API Gateway events it can be found here: event.headers["User-Agent"] */ public useragent?: string; /** * The http verb (GET|POST...) * * In API Gateway events it can be found here: event.httpMethod */ public verb?: string; /** * The host of this service. * * In API Gateway events it can be found here: event.headers["Host"] */ public host?: string; /** * The host of this service. * * In API Gateway events it can be found here: event.queryStringParameters */ public query?: any; /** * The http path of this service. * * In API Gateway events it can be found here: event.path */ public path?: string; /** * Setup the meta data that will be added to all logs, this gives richer logs that will provide more value when they are sent to a log aggregator. */ public setup(options: LogOptions) { this.level = options.level || this.level; this.context = options.context || this.context; this.xRequestId = options.xRequestId || this.xRequestId; this.host = options.host || this.host; this.path = options.path || this.path; this.verb = options.verb || this.verb; this.query = options.query || this.query; this.useragent = options.useragent || this.useragent; } /** * Prints an enriched log as JSON to stdout with newline. * @param logInput {message: 'This is my message', context: AwsLambdaContext, xRequestId: 'id-from-external-system', something: 'my property', somethingElse: 'another extra property' } */ public debug(logInput: LogInput | string) { if (typeof logInput === 'string') { logInput = this.stringToLogInput(logInput); } let debugLevel = LogLevels.DEBUG; if (debugLevel >= this.level) { let log = this.createLogJson(logInput, LogLevels.DEBUG); console.log( prettyPrint(log) ); } } /** * Prints an enriched log as JSON to stdout with newline. * @param logInput {message: 'This is my message', context: AwsLambdaContext, xRequestId: 'id-from-external-system', something: 'my property', somethingElse: 'another extra property' } */ public info(logInput: LogInput | string) { if (typeof logInput === 'string') { logInput = this.stringToLogInput(logInput); } let infoLevel = LogLevels.INFO; if (infoLevel >= this.level) { let log = this.createLogJson(logInput, LogLevels.INFO); console.log( prettyPrint(log) ); } } /** * Prints an enriched log as JSON to stdout with newline. * @param logInput {message: 'This is my message', context: AwsLambdaContext, xRequestId: 'id-from-external-system', something: 'my property', somethingElse: 'another extra property' } */ public warn(logInput: LogInput | string) { if (typeof logInput === 'string') { logInput = this.stringToLogInput(logInput); } let warnLevel = LogLevels.WARN; if (warnLevel >= this.level) { let log = this.createLogJson(logInput, LogLevels.WARN); console.log( prettyPrint(log) ); } } /** * Prints an enriched log as JSON to stdout with newline. * @param logInput { * message: 'This is my message', * context: AwsLambdaContext, * xRequestId: 'id-from-external-system', * something: 'my property', * somethingElse: 'another extra property' } */ public error(logInput: LogInputError | string) { if (typeof logInput === 'string') { logInput = this.stringToLogInput(logInput); } let errorLevel = LogLevels.ERROR; if (errorLevel >= this.level) { logInput = this.preProcessErrorProperties(logInput); let log = this.createLogJson(logInput, LogLevels.ERROR); console.log( prettyPrint(log) ); } } /** * Set initial log level from environment param PN_LOG_LEVEL, with fallback to INFO. */ private setInitialLogLevel(): LogLevels { try { Eif (!process.env.PN_LOG_LEVEL) { return LogLevels.INFO; } let level: string = process.env.PN_LOG_LEVEL; const possibleLogLevel: LogLevels | undefined = (LogLevels as any)[level]; // Explicit undefined check to avoid any false negatives if (possibleLogLevel !== undefined) { return possibleLogLevel; } else { return LogLevels.INFO; } } catch (ex) { return LogLevels.INFO; } } private stringToLogInput(message: string): LogInput { return { message }; } private createLogJson(logInput: LogInput, level: LogLevels): LogOutput { // @ts-ignore let logOutput: LogOutput = {}; let context = this.context; logOutput['@timestamp'] = Date.now(); logOutput.level = LogLevels[level]; logOutput.message = logInput.message; Eif (this.xRequestId) { logOutput.xRequestId = this.xRequestId; } Eif (this.host) { logOutput.host = this.host; } Eif (this.verb) { logOutput.verb = this.verb; } Eif (this.path) { logOutput.path = this.path; } Eif (this.useragent) { logOutput.useragent = this.useragent; } Eif (this.query) { logOutput.query = this.query; } /** * By using the lambda context we can easily add more rich logging. */ Eif (context) { if (context.awsRequestId) { logOutput.awsRequestId = context.awsRequestId; } if (context.functionName) { logOutput.functionName = context.functionName; } if (context.invokedFunctionArn) { logOutput.invokedFunctionArn = context.invokedFunctionArn; } if (context.memoryLimitInMB) { logOutput.memoryLimitInMB = context.memoryLimitInMB; } if (context.getRemainingTimeInMillis) { logOutput.remainingTime = context.getRemainingTimeInMillis(); } } for (let prop of Object.keys(logInput)) { if (prop === 'message' || prop === 'xRequestId' || prop === 'context') { continue; } logOutput[prop] = logInput[prop]; } return logOutput; } /** * If we find a property of type Error we cannot stringify it directly because it * will just be outputted as "{}", instead if we find an instanceof Error we add * the stack to a stack property. */ private preProcessErrorProperties(logInput: LogInput) { if (logInput instanceof Error) { return Object.assign( {}, logInput, { name: logInput.name, message: logInput.message, stack: logInput.stack ? logInput.stack.split('\n') : '' } ); } for (let prop of Object.keys(logInput)) { if (logInput[prop] instanceof Error) { let ex = logInput[prop]; logInput[prop] = { ...ex, name: ex.name, message: ex.message, stack: ex.stack ? ex.stack.split('\n') : '' }; } } return logInput; } } const log = new Logger(); export { log, LogLevels, LogInput, LogOutput }; |