Skip to content

Commit

Permalink
feat: remove serverless v2 logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Jun 1, 2022
1 parent f50fe01 commit 404093c
Show file tree
Hide file tree
Showing 27 changed files with 558 additions and 1,224 deletions.
110 changes: 28 additions & 82 deletions src/ServerlessOffline.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import process, { env, exit } from 'node:process'
import chalk from 'chalk'
import semver from 'semver'
import updateNotifier from 'update-notifier'
import debugLog from './debugLog.js'
import serverlessLog, { logWarning, setLog } from './serverlessLog.js'
import { satisfiesVersionRange } from './utils/index.js'
import {
commandOptions,
Expand All @@ -29,14 +27,8 @@ export default class ServerlessOffline {
this.#cliOptions = cliOptions
this.#serverless = serverless

if (v3Utils) {
this.log = v3Utils.log
this.progress = v3Utils.progress
this.writeText = v3Utils.writeText
this.v3Utils = v3Utils
}

setLog((...args) => serverless.cli.log(...args))
this.log = v3Utils.log
this.v3Utils = v3Utils

this.commands = {
offline: {
Expand Down Expand Up @@ -116,11 +108,7 @@ export default class ServerlessOffline {
return
}

if (this.log) {
this.log.info('Halting offline server')
} else {
serverlessLog('Halting offline server')
}
this.log.info('Halting offline server')

const eventModules = []

Expand Down Expand Up @@ -150,7 +138,7 @@ export default class ServerlessOffline {

async cleanupFunctions() {
if (this.#lambda) {
serverlessLog('Forcing cleanup of Lambda functions')
this.log.debug('Forcing cleanup of Lambda functions')
await this.#lambda.cleanup()
}
}
Expand Down Expand Up @@ -178,11 +166,7 @@ export default class ServerlessOffline {
.on('SIGTERM', () => resolve('SIGTERM'))
})

if (this.log) {
this.log.info(`Got ${command} signal. Offline Halting...`)
} else {
serverlessLog(`Got ${command} signal. Offline Halting...`)
}
this.log.info(`Got ${command} signal. Offline Halting...`)
}

async _createLambda(lambdas, skipStart) {
Expand Down Expand Up @@ -288,19 +272,14 @@ export default class ServerlessOffline {
origin: this.#options.corsAllowOrigin,
}

if (this.log) {
this.log.notice()
this.log.notice(
`Starting Offline at stage ${provider.stage} ${chalk.gray(
`(${provider.region})`,
)}`,
)
this.log.notice()
this.log.debug('options:', this.#options)
} else {
serverlessLog(`Starting Offline: ${provider.stage} ${provider.region}.`)
debugLog('options:', this.#options)
}
this.log.notice()
this.log.notice(
`Starting Offline at stage ${provider.stage} ${chalk.gray(
`(${provider.region})`,
)}`,
)
this.log.notice()
this.log.debug('options:', this.#options)
}

_getEvents() {
Expand Down Expand Up @@ -341,15 +320,9 @@ export default class ServerlessOffline {
}
} else if (typeof httpEvent.http === 'object') {
if (!httpEvent.http.method) {
if (this.log) {
this.log.warning(
`Event definition is missing a method for function "${functionKey}"`,
)
} else {
logWarning(
`Event definition is missing a method for function "${functionKey}"`,
)
}
this.log.warning(
`Event definition is missing a method for function "${functionKey}"`,
)
httpEvent.http.method = ''
}
if (
Expand All @@ -369,15 +342,9 @@ export default class ServerlessOffline {
delete httpEvent.http.method
delete httpEvent.http.path
} else {
if (this.log) {
this.log.warning(
`Event definition must be a string or object but received ${typeof httpEvent.http} for function "${functionKey}"`,
)
} else {
logWarning(
`Event definition must be a string or object but received ${typeof httpEvent.http} for function "${functionKey}"`,
)
}
this.log.warning(
`Event definition must be a string or object but received ${typeof httpEvent.http} for function "${functionKey}"`,
)
httpEvent.http.routeKey = ''
}

Expand Down Expand Up @@ -420,26 +387,14 @@ export default class ServerlessOffline {

// for simple API Key authentication model
if (hasPrivateHttpEvent) {
if (this.log) {
this.log.notice(`Key with token: ${this.#options.apiKey}`)
} else {
serverlessLog(`Key with token: ${this.#options.apiKey}`)
}
this.log.notice(`Key with token: ${this.#options.apiKey}`)

if (this.#options.noAuth) {
if (this.log) {
this.log.notice(
'Authorizers are turned off. You do not need to use x-api-key header.',
)
} else {
serverlessLog(
'Authorizers are turned off. You do not need to use x-api-key header.',
)
}
} else if (this.log) {
this.log.notice('Remember to use x-api-key on the request headers')
this.log.notice(
'Authorizers are turned off. You do not need to use x-api-key header.',
)
} else {
serverlessLog('Remember to use x-api-key on the request headers')
this.log.notice('Remember to use x-api-key on the request headers')
}
}

Expand Down Expand Up @@ -472,21 +427,12 @@ export default class ServerlessOffline {
)

if (!versionIsSatisfied) {
if (this.log) {
this.log.warning(
`serverless-offline requires serverless version ${requiredVersionRange} but found version ${currentVersion}.
this.log.warning(
`'serverless-offline' requires 'serverless' version '${requiredVersionRange}' but found version '${currentVersion}'.
Be aware that functionality might be limited or contains bugs.
To avoid any issues update serverless to a later version.
To avoid any issues update 'serverless' to a later version.
`,
)
} else {
logWarning(
`serverless-offline requires serverless version ${requiredVersionRange} but found version ${currentVersion}.
Be aware that functionality might be limited or contains bugs.
To avoid any issues update serverless to a later version.
`,
)
}
)
}
}
}
21 changes: 4 additions & 17 deletions src/events/http/Endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { existsSync, readFileSync } from 'node:fs'
import { dirname, resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
import OfflineEndpoint from './OfflineEndpoint.js'
import debugLog from '../../debugLog.js'

const { keys } = Object

Expand Down Expand Up @@ -35,12 +34,8 @@ export default class Endpoint {
constructor(handlerPath, http, v3Utils) {
this.#handlerPath = handlerPath
this.#http = http
if (v3Utils) {
this.log = v3Utils.log
this.progress = v3Utils.progress
this.writeText = v3Utils.writeText
this.v3Utils = v3Utils
}

this.log = v3Utils.log

// TODO FIXME
// eslint-disable-next-line no-constructor-return
Expand Down Expand Up @@ -80,11 +75,7 @@ export default class Endpoint {
const resFilename = `${this.#handlerPath}.res.vm`

fep.responseContentType = getResponseContentType(fep)
if (this.log) {
this.log.debug('Response Content-Type ', fep.responseContentType)
} else {
debugLog('Response Content-Type ', fep.responseContentType)
}
this.log.debug('Response Content-Type ', fep.responseContentType)

// load response template from http response template, or load file if exists other use default
if (fep.response && fep.response.template) {
Expand All @@ -98,11 +89,7 @@ export default class Endpoint {
defaultResponseTemplate
}
} catch (err) {
if (this.log) {
this.log.debug(`Error: ${err}`)
} else {
debugLog(`Error: ${err}`)
}
this.log.debug(`Error: ${err}`)
}

return fep
Expand Down
Loading

0 comments on commit 404093c

Please sign in to comment.