Improve Response verbose/debug logging

This commit is contained in:
Garrett Mills 2022-03-30 21:38:25 -05:00
parent 10b3e1ecc3
commit 6f66126d38
4 changed files with 16 additions and 4 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@extollo/lib", "name": "@extollo/lib",
"version": "0.9.12", "version": "0.9.13",
"description": "The framework library that lifts up your code.", "description": "The framework library that lifts up your code.",
"main": "lib/index.js", "main": "lib/index.js",
"types": "lib/index.d.ts", "types": "lib/index.d.ts",

View File

@ -100,6 +100,7 @@ export class Response {
/** Set the value of the response header. */ /** Set the value of the response header. */
public setHeader(name: string, value: string | string[]): this { public setHeader(name: string, value: string | string[]): this {
this.logging.verbose(`Will set header on response: ${name}`)
if ( this.sentHeaders ) { if ( this.sentHeaders ) {
throw new HeadersAlreadySentError(this, name) throw new HeadersAlreadySentError(this, name)
} }
@ -113,6 +114,7 @@ export class Response {
* @param data * @param data
*/ */
public setHeaders(data: {[name: string]: string | string[]}): this { public setHeaders(data: {[name: string]: string | string[]}): this {
this.logging.verbose(`Will set headers on response: ${Object.keys(data).join(', ')}`)
if ( this.sentHeaders ) { if ( this.sentHeaders ) {
throw new HeadersAlreadySentError(this) throw new HeadersAlreadySentError(this)
} }
@ -128,12 +130,16 @@ export class Response {
* @param value * @param value
*/ */
public appendHeader(name: string, value: string | string[]): this { public appendHeader(name: string, value: string | string[]): this {
this.logging.verbose(`Will append header: ${name}`)
if ( this.sentHeaders ) { if ( this.sentHeaders ) {
throw new HeadersAlreadySentError(this, name) throw new HeadersAlreadySentError(this, name)
} }
if ( !Array.isArray(value) ) { if ( !Array.isArray(value) ) {
value = [value] value = [value]
} }
let existing = this.headers[name] ?? [] let existing = this.headers[name] ?? []
if ( !Array.isArray(existing) ) { if ( !Array.isArray(existing) ) {
existing = [existing] existing = [existing]
@ -152,6 +158,7 @@ export class Response {
* Write the headers to the client. * Write the headers to the client.
*/ */
public sendHeaders(): this { public sendHeaders(): this {
this.logging.verbose(`Sending headers...`)
const headers = {} as any const headers = {} as any
const setCookieHeaders = this.cookies.getSetCookieHeaders() const setCookieHeaders = this.cookies.getSetCookieHeaders()
@ -199,6 +206,7 @@ export class Response {
* @param data * @param data
*/ */
public async write(data: string | Buffer | Uint8Array | Readable): Promise<void> { public async write(data: string | Buffer | Uint8Array | Readable): Promise<void> {
this.logging.verbose(`Writing headers & data to response... (destroyed? ${this.serverResponse.destroyed})`)
return new Promise<void>((res, rej) => { return new Promise<void>((res, rej) => {
if ( this.responseEnded || this.serverResponse.destroyed ) { if ( this.responseEnded || this.serverResponse.destroyed ) {
throw new ErrorWithContext('Tried to write to Response after lifecycle ended.') throw new ErrorWithContext('Tried to write to Response after lifecycle ended.')

View File

@ -1,15 +1,17 @@
import {Container} from '../di' import {Container} from '../di'
import { import {
ErrorWithContext, ErrorWithContext,
FileLogger,
globalRegistry, globalRegistry,
ifDebugging,
infer, infer,
isLoggingLevel, logIfDebugging, isLoggingLevel,
LoggingLevel,
logIfDebugging,
PathLike, PathLike,
StandardLogger, StandardLogger,
universalPath, universalPath,
UniversalPath, UniversalPath,
FileLogger,
ifDebugging,
} from '../util' } from '../util'
import {Logging} from '../service/Logging' import {Logging} from '../service/Logging'
import {RunLevelErrorHandler} from './RunLevelErrorHandler' import {RunLevelErrorHandler} from './RunLevelErrorHandler'
@ -221,6 +223,7 @@ export class Application extends Container {
logging.registerLogger(file) logging.registerLogger(file)
} }
logging.level = LoggingLevel.Verbose
logging.verbose('Attempting to load logging level from the environment...') logging.verbose('Attempting to load logging level from the environment...')
const envLevel = this.env('EXTOLLO_LOGGING_LEVEL') const envLevel = this.env('EXTOLLO_LOGGING_LEVEL')

View File

@ -26,6 +26,7 @@ const isLoggingLevel = (something: unknown): something is LoggingLevel => {
LoggingLevel.Info, LoggingLevel.Info,
LoggingLevel.Debug, LoggingLevel.Debug,
LoggingLevel.Verbose, LoggingLevel.Verbose,
LoggingLevel.Trace,
].includes(something as any) ].includes(something as any)
} }