From 6f66126d386ad6048871afda33c7e3a4d6324eff Mon Sep 17 00:00:00 2001 From: garrettmills Date: Wed, 30 Mar 2022 21:38:25 -0500 Subject: [PATCH] Improve Response verbose/debug logging --- package.json | 2 +- src/http/lifecycle/Response.ts | 8 ++++++++ src/lifecycle/Application.ts | 9 ++++++--- src/util/logging/types.ts | 1 + 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 0c2bf3a..137cff1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@extollo/lib", - "version": "0.9.12", + "version": "0.9.13", "description": "The framework library that lifts up your code.", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/http/lifecycle/Response.ts b/src/http/lifecycle/Response.ts index 52ee2b1..0eadf00 100644 --- a/src/http/lifecycle/Response.ts +++ b/src/http/lifecycle/Response.ts @@ -100,6 +100,7 @@ export class Response { /** Set the value of the response header. */ public setHeader(name: string, value: string | string[]): this { + this.logging.verbose(`Will set header on response: ${name}`) if ( this.sentHeaders ) { throw new HeadersAlreadySentError(this, name) } @@ -113,6 +114,7 @@ export class Response { * @param data */ public setHeaders(data: {[name: string]: string | string[]}): this { + this.logging.verbose(`Will set headers on response: ${Object.keys(data).join(', ')}`) if ( this.sentHeaders ) { throw new HeadersAlreadySentError(this) } @@ -128,12 +130,16 @@ export class Response { * @param value */ public appendHeader(name: string, value: string | string[]): this { + this.logging.verbose(`Will append header: ${name}`) + if ( this.sentHeaders ) { throw new HeadersAlreadySentError(this, name) } + if ( !Array.isArray(value) ) { value = [value] } + let existing = this.headers[name] ?? [] if ( !Array.isArray(existing) ) { existing = [existing] @@ -152,6 +158,7 @@ export class Response { * Write the headers to the client. */ public sendHeaders(): this { + this.logging.verbose(`Sending headers...`) const headers = {} as any const setCookieHeaders = this.cookies.getSetCookieHeaders() @@ -199,6 +206,7 @@ export class Response { * @param data */ public async write(data: string | Buffer | Uint8Array | Readable): Promise { + this.logging.verbose(`Writing headers & data to response... (destroyed? ${this.serverResponse.destroyed})`) return new Promise((res, rej) => { if ( this.responseEnded || this.serverResponse.destroyed ) { throw new ErrorWithContext('Tried to write to Response after lifecycle ended.') diff --git a/src/lifecycle/Application.ts b/src/lifecycle/Application.ts index 02437ac..63312d9 100644 --- a/src/lifecycle/Application.ts +++ b/src/lifecycle/Application.ts @@ -1,15 +1,17 @@ import {Container} from '../di' import { ErrorWithContext, + FileLogger, globalRegistry, + ifDebugging, infer, - isLoggingLevel, logIfDebugging, + isLoggingLevel, + LoggingLevel, + logIfDebugging, PathLike, StandardLogger, universalPath, UniversalPath, - FileLogger, - ifDebugging, } from '../util' import {Logging} from '../service/Logging' import {RunLevelErrorHandler} from './RunLevelErrorHandler' @@ -221,6 +223,7 @@ export class Application extends Container { logging.registerLogger(file) } + logging.level = LoggingLevel.Verbose logging.verbose('Attempting to load logging level from the environment...') const envLevel = this.env('EXTOLLO_LOGGING_LEVEL') diff --git a/src/util/logging/types.ts b/src/util/logging/types.ts index f7fe851..934679a 100644 --- a/src/util/logging/types.ts +++ b/src/util/logging/types.ts @@ -26,6 +26,7 @@ const isLoggingLevel = (something: unknown): something is LoggingLevel => { LoggingLevel.Info, LoggingLevel.Debug, LoggingLevel.Verbose, + LoggingLevel.Trace, ].includes(something as any) }