From 48de4e5be1e1a1c47768910355c3010e6923865d Mon Sep 17 00:00:00 2001 From: James Gregory Date: Sun, 3 May 2020 14:26:09 +1000 Subject: [PATCH] fix: hide debug logging by default --- package.json | 2 ++ src/bin/start.ts | 3 ++- src/log.ts | 13 +++++++++++++ src/server/defaults.ts | 3 ++- src/server/server.ts | 19 ++++++++++--------- .../codeDelivery/consoleCodeSender.ts | 3 ++- src/services/lambda.ts | 7 ++++--- src/services/userPoolClient.ts | 7 ++++--- yarn.lock | 5 +++++ 9 files changed, 44 insertions(+), 18 deletions(-) create mode 100644 src/log.ts diff --git a/package.json b/package.json index 6d0e815a..80f82f87 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "@types/aws-lambda": "^8.10.48", "@types/body-parser": "^1.19.0", "@types/cors": "^2.8.6", + "@types/debug": "^4.1.5", "@types/express": "^4.17.6", "@types/jest": "^25.2.1", "@types/jsonwebtoken": "^8.3.9", @@ -57,6 +58,7 @@ "body-parser": "^1.19.0", "boxen": "^4.2.0", "cors": "^2.8.5", + "debug": "^4.1.1", "deepmerge": "^4.2.2", "express": "^4.17.1", "jsonwebtoken": "^8.5.1", diff --git a/src/bin/start.ts b/src/bin/start.ts index dfa67deb..5ae7e8e3 100644 --- a/src/bin/start.ts +++ b/src/bin/start.ts @@ -1,5 +1,6 @@ #!/usr/bin/env node +import log from "../log"; import { createDefaultServer } from "../server"; createDefaultServer() @@ -18,7 +19,7 @@ createDefaultServer() ? address : `${address.address}:${address.port}`; - console.log(`Cognito Local running on http://${url}`); + log.info(`Cognito Local running on http://${url}`); }) .catch((err) => { console.error(err); diff --git a/src/log.ts b/src/log.ts new file mode 100644 index 00000000..d808d7ef --- /dev/null +++ b/src/log.ts @@ -0,0 +1,13 @@ +import debug from "debug"; + +const logger = debug("CognitoLocal"); + +export default { + info: console.log, + error: console.error, + debug(...args: any[]) { + if (logger.enabled) { + logger.log(...args); + } + }, +}; diff --git a/src/server/defaults.ts b/src/server/defaults.ts index c0ee9154..87316b35 100644 --- a/src/server/defaults.ts +++ b/src/server/defaults.ts @@ -1,3 +1,4 @@ +import log from "../log"; import { createCodeDelivery } from "../services"; import { ConsoleCodeSender } from "../services/codeDelivery/consoleCodeSender"; import { otp } from "../services/codeDelivery/otp"; @@ -14,7 +15,7 @@ import * as AWS from "aws-sdk"; export const createDefaultServer = async (): Promise => { const config = await loadConfig(); - console.log("Loaded config:", config); + log.debug("Loaded config:", config); const cognitoClient = await createCognitoClient( config.UserPoolDefaults, diff --git a/src/server/server.ts b/src/server/server.ts index 77e15268..ce778da3 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -3,6 +3,7 @@ import cors from "cors"; import express from "express"; import * as http from "http"; import { CognitoError, unsupported, UnsupportedError } from "../errors"; +import log from "../log"; import { Router } from "../targets/router"; import PublicKey from "../keys/cognitoLocal.public.json"; @@ -64,15 +65,15 @@ export const createServer = ( console.error(`Error handling target: ${target}`, ex); if (ex instanceof UnsupportedError) { if (options.development) { - console.log("======"); - console.log(); - console.log("Unsupported target"); - console.log(""); - console.log(`x-amz-target: ${xAmzTarget}`); - console.log("Body:"); - console.log(JSON.stringify(req.body, undefined, 2)); - console.log(); - console.log("======"); + log.info("======"); + log.info(); + log.info("Unsupported target"); + log.info(""); + log.info(`x-amz-target: ${xAmzTarget}`); + log.info("Body:"); + log.info(JSON.stringify(req.body, undefined, 2)); + log.info(); + log.info("======"); } return unsupported(ex.message, res); diff --git a/src/services/codeDelivery/consoleCodeSender.ts b/src/services/codeDelivery/consoleCodeSender.ts index c5a7c4f2..3cedf1cc 100644 --- a/src/services/codeDelivery/consoleCodeSender.ts +++ b/src/services/codeDelivery/consoleCodeSender.ts @@ -1,4 +1,5 @@ import boxen from "boxen"; +import log from "../../log"; import { User } from "../userPoolClient"; import { CodeSender } from "./codeSender"; @@ -7,7 +8,7 @@ const sendToConsole = ( destination: string, code: string ): Promise => { - console.log( + log.info( boxen( `Confirmation Code Delivery diff --git a/src/services/lambda.ts b/src/services/lambda.ts index 9aa47a8a..8c8bdbcd 100644 --- a/src/services/lambda.ts +++ b/src/services/lambda.ts @@ -3,6 +3,7 @@ import * as AWS from "aws-sdk"; import { InvocationResponse } from "aws-sdk/clients/lambda"; import * as fs from "fs"; import { UnexpectedLambdaExceptionError } from "../errors"; +import log from "../log"; const awsSdkPackageJson = fs.readFileSync( require.resolve("aws-sdk/package.json"), @@ -85,7 +86,7 @@ export const createLambda: CreateLambda = (config, lambdaClient) => ({ lambdaEvent.request.validationData = {}; } - console.log( + log.debug( `Invoking "${functionName}" with event`, JSON.stringify(lambdaEvent, undefined, 2) ); @@ -99,11 +100,11 @@ export const createLambda: CreateLambda = (config, lambdaClient) => ({ }) .promise(); } catch (ex) { - console.log(ex); + log.error(ex); throw new UnexpectedLambdaExceptionError(); } - console.log( + log.debug( `Lambda completed with StatusCode=${result.StatusCode} and FunctionError=${result.FunctionError}` ); if (result.StatusCode === 200) { diff --git a/src/services/userPoolClient.ts b/src/services/userPoolClient.ts index e2ce95b2..e3738142 100644 --- a/src/services/userPoolClient.ts +++ b/src/services/userPoolClient.ts @@ -1,3 +1,4 @@ +import log from "../log"; import { AppClient, newId } from "./appClient"; import { CreateDataStore, DataStore } from "./dataStore"; @@ -104,7 +105,7 @@ export const createUserPoolClient = async ( }, async getUserByUsername(username) { - console.log("getUserByUsername", username); + log.debug("getUserByUsername", username); const aliasEmailEnabled = config.UsernameAttributes?.includes("email"); const aliasPhoneNumberEnabled = config.UsernameAttributes?.includes( @@ -137,14 +138,14 @@ export const createUserPoolClient = async ( }, async listUsers(): Promise { - console.log("listUsers"); + log.debug("listUsers"); const users = await dataStore.get>("Users", {}); return Object.values(users); }, async saveUser(user) { - console.log("saveUser", user); + log.debug("saveUser", user); const attributes = attributesInclude("sub", user.Attributes) ? user.Attributes diff --git a/yarn.lock b/yarn.lock index b2413691..4994d1aa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1491,6 +1491,11 @@ dependencies: "@types/express" "*" +"@types/debug@^4.1.5": + version "4.1.5" + resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.5.tgz#b14efa8852b7768d898906613c23f688713e02cd" + integrity sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ== + "@types/eslint-visitor-keys@^1.0.0": version "1.0.0" resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d"