Skip to content

Commit

Permalink
fix: hide debug logging by default
Browse files Browse the repository at this point in the history
  • Loading branch information
jagregory committed May 3, 2020
1 parent 688fd4a commit 48de4e5
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 18 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion src/bin/start.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node

import log from "../log";
import { createDefaultServer } from "../server";

createDefaultServer()
Expand All @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions src/log.ts
Original file line number Diff line number Diff line change
@@ -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);
}
},
};
3 changes: 2 additions & 1 deletion src/server/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import log from "../log";
import { createCodeDelivery } from "../services";
import { ConsoleCodeSender } from "../services/codeDelivery/consoleCodeSender";
import { otp } from "../services/codeDelivery/otp";
Expand All @@ -14,7 +15,7 @@ import * as AWS from "aws-sdk";
export const createDefaultServer = async (): Promise<Server> => {
const config = await loadConfig();

console.log("Loaded config:", config);
log.debug("Loaded config:", config);

const cognitoClient = await createCognitoClient(
config.UserPoolDefaults,
Expand Down
19 changes: 10 additions & 9 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/services/codeDelivery/consoleCodeSender.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import boxen from "boxen";
import log from "../../log";
import { User } from "../userPoolClient";
import { CodeSender } from "./codeSender";

Expand All @@ -7,7 +8,7 @@ const sendToConsole = (
destination: string,
code: string
): Promise<void> => {
console.log(
log.info(
boxen(
`Confirmation Code Delivery
Expand Down
7 changes: 4 additions & 3 deletions src/services/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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)
);
Expand All @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions src/services/userPoolClient.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import log from "../log";
import { AppClient, newId } from "./appClient";
import { CreateDataStore, DataStore } from "./dataStore";

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -137,14 +138,14 @@ export const createUserPoolClient = async (
},

async listUsers(): Promise<readonly User[]> {
console.log("listUsers");
log.debug("listUsers");
const users = await dataStore.get<Record<string, User>>("Users", {});

return Object.values(users);
},

async saveUser(user) {
console.log("saveUser", user);
log.debug("saveUser", user);

const attributes = attributesInclude("sub", user.Attributes)
? user.Attributes
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 48de4e5

Please sign in to comment.