Comparing version 1.0.0 to 1.0.1
import { Request, Response } from 'express'; | ||
declare const levels: readonly ["trace", "info", "warn", "error", "fatal", "security"]; | ||
export declare type LevelType = typeof levels[number]; | ||
export interface LogPresets { | ||
[key: string]: string | undefined; | ||
export interface LogPresets extends Record<string, unknown> { | ||
module?: string; | ||
@@ -16,14 +15,17 @@ trackId?: string; | ||
} | ||
export declare type LogFunction = (logData: any, response?: any) => Promise<any>; | ||
export declare type TimeLogFunction = (label: string, level?: LevelType, extraLogDat?: any) => Promise<any>; | ||
export declare type ParseReqInOut = Request & { | ||
export declare type ParseReqIn = Request & { | ||
user?: unknown; | ||
}; | ||
interface LogData extends Record<string, unknown> { | ||
export declare type ParseReqOut = Pick<ParseReqIn, 'body' | 'headers' | 'method' | 'params' | 'path' | 'query' | 'url' | 'user'>; | ||
export interface LogData extends Record<string, unknown> { | ||
err?: Error; | ||
msg?: string; | ||
req?: ParseReqIn; | ||
} | ||
interface ResponseWrapper { | ||
export interface ResponseWrapper { | ||
res: Response; | ||
code: number; | ||
} | ||
export declare type LogFunction = (logData: LogData, response?: ResponseWrapper) => Promise<any>; | ||
export declare type TimeLogFunction = (label: string, level?: LevelType, extraLogDat?: LogData) => Promise<any>; | ||
export default class Logger { | ||
@@ -66,3 +68,3 @@ isTransient: boolean; | ||
*/ | ||
static parseReq(req: ParseReqInOut): Partial<ParseReqInOut>; | ||
static parseReq(req: ParseReqIn): ParseReqOut; | ||
/** | ||
@@ -69,0 +71,0 @@ * Format milliseconds to a string for logging |
"use strict"; | ||
var __rest = (this && this.__rest) || function (s, e) { | ||
var t = {}; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) | ||
t[p] = s[p]; | ||
if (s != null && typeof Object.getOwnPropertySymbols === "function") | ||
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { | ||
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) | ||
t[p[i]] = s[p[i]]; | ||
} | ||
return t; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -121,2 +132,3 @@ const uuid_1 = require("uuid"); | ||
async write(levelIndex, logData, response) { | ||
var _a; | ||
if (!utils_1.isObject(logData)) { | ||
@@ -127,3 +139,4 @@ // eslint-disable-next-line no-console | ||
} | ||
const logObj = Object.assign(Object.assign({}, this.presets), logData); | ||
const { req } = logData, rest = __rest(logData, ["req"]); | ||
const logObj = Object.assign(Object.assign({}, this.presets), rest); | ||
if (response) { | ||
@@ -160,3 +173,9 @@ // If the response object has been included with the call then it means we need to | ||
// This will happen if we manually created an err prop - it might not have a stack prop | ||
logObj.err.stack = new Error().stack; | ||
// `stack` is a non standard property on the Error object so it can be undefined | ||
// which is why we have to provide the ??. | ||
// Ignoring the line for code coverage for now because we're going to have to | ||
// mock new Error() or extract this line into a local method that can be mocked | ||
// which would add an extra frame to the stack which I don't want. | ||
/* istanbul ignore next */ | ||
logObj.err.stack = (_a = new Error().stack) !== null && _a !== void 0 ? _a : '<no error stack>'; | ||
} | ||
@@ -174,4 +193,4 @@ logObj.fullStack = logObj.err.stack.split('\n').slice(1); | ||
} | ||
if (logObj.req) { | ||
logObj.req = Logger.parseReq(logObj.req); | ||
if (req) { | ||
logObj.req = Logger.parseReq(req); | ||
} | ||
@@ -178,0 +197,0 @@ if (this.logCollector !== null && !this.isTransientTriggered) { |
@@ -9,2 +9,3 @@ import { v4 } from 'uuid'; | ||
} from './loggly-wrapper'; | ||
import { BetterOmit } from './local-types'; | ||
@@ -14,4 +15,3 @@ const levels = ['trace', 'info', 'warn', 'error', 'fatal', 'security'] as const; | ||
export interface LogPresets { | ||
[key: string]: string | undefined; | ||
export interface LogPresets extends Record<string, unknown> { | ||
module?: string; | ||
@@ -29,5 +29,40 @@ trackId?: string; | ||
export type LogFunction = (logData: any, response?: any) => Promise<any>; | ||
export type TimeLogFunction = (label: string, level?: LevelType, extraLogDat?: any) => Promise<any>; | ||
export type ParseReqIn = Request & { user?: unknown }; | ||
export type ParseReqOut = Pick<ParseReqIn, 'body' | | ||
'headers' | | ||
'method' | | ||
'params' | | ||
'path' | | ||
'query' | | ||
'url' | | ||
'user'>; | ||
export interface LogData extends Record<string, unknown> { | ||
err?: Error; | ||
msg?: string; | ||
req?: ParseReqIn; | ||
} | ||
interface LogDataOut extends BetterOmit<LogData, 'req'>, LogPresets { | ||
/** | ||
* The Stack property from the Error object split into lines. | ||
*/ | ||
fullStack?: string[]; | ||
/** | ||
* Created from the fullStack by removing lines containing node_modules | ||
*/ | ||
shortStack?: string[]; | ||
req?: ParseReqOut; | ||
} | ||
export interface ResponseWrapper { | ||
res: Response; | ||
code: number; | ||
} | ||
export type LogFunction = (logData: LogData, response?: ResponseWrapper) => Promise<any>; | ||
export type TimeLogFunction = ( | ||
label: string, level?: LevelType, extraLogDat?: LogData, | ||
) => Promise<any>; | ||
const errorLevel = levels.indexOf('error'); | ||
@@ -45,13 +80,2 @@ | ||
export type ParseReqInOut = Request & { user?: unknown }; | ||
interface LogData extends Record<string, unknown> { | ||
msg?: string; | ||
} | ||
interface ResponseWrapper { | ||
res: Response; | ||
code: number; | ||
} | ||
export default class Logger { | ||
@@ -172,3 +196,3 @@ isTransient: boolean; | ||
*/ | ||
static parseReq(req: ParseReqInOut): Partial<ParseReqInOut> { | ||
static parseReq(req: ParseReqIn): ParseReqOut { | ||
return { | ||
@@ -228,3 +252,4 @@ body: req.body, | ||
const logObj: any = { ...this.presets, ...logData }; | ||
const { req, ...rest } = logData; | ||
const logObj: LogDataOut = { ...this.presets, ...rest }; | ||
@@ -266,3 +291,9 @@ if (response) { | ||
// This will happen if we manually created an err prop - it might not have a stack prop | ||
logObj.err.stack = new Error().stack; | ||
// `stack` is a non standard property on the Error object so it can be undefined | ||
// which is why we have to provide the ??. | ||
// Ignoring the line for code coverage for now because we're going to have to | ||
// mock new Error() or extract this line into a local method that can be mocked | ||
// which would add an extra frame to the stack which I don't want. | ||
/* istanbul ignore next */ | ||
logObj.err.stack = new Error().stack ?? '<no error stack>'; | ||
} | ||
@@ -281,4 +312,4 @@ logObj.fullStack = logObj.err.stack.split('\n').slice(1); | ||
if (logObj.req) { | ||
logObj.req = Logger.parseReq(logObj.req); | ||
if (req) { | ||
logObj.req = Logger.parseReq(req); | ||
} | ||
@@ -285,0 +316,0 @@ |
@@ -57,3 +57,3 @@ { | ||
"types": "dist/index.d.ts", | ||
"version": "1.0.0" | ||
"version": "1.0.1" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
71120
31
918