From 3da8de2664cfb56301e3d87e180e0a1ac7df9643 Mon Sep 17 00:00:00 2001 From: "Seo Myunggyun (Jonathan)" Date: Thu, 3 Aug 2023 09:17:13 +0000 Subject: [PATCH] Log for testWebhookReceiver --- app.ts | 5 +++++ logUtils.ts | 1 + testWebhookReceiver.ts | 38 +++++++++++++++++++++++++++++++++++--- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/app.ts b/app.ts index c59e93a..24da4b5 100644 --- a/app.ts +++ b/app.ts @@ -22,6 +22,7 @@ import { EmitterLoggerName, getInternalLoggers, ObserverLoggerName, + TestWebhookReceiverLoggerName, WebLoggerName, } from "./logUtils.ts"; import { observer } from "./observer.ts"; @@ -101,6 +102,10 @@ async function main() { level: "DEBUG", handlers: ["console"], }, + [TestWebhookReceiverLoggerName]: { + level: "INFO", + handlers: ["console"], + }, lop: { level: "INFO", handlers: ["console"], diff --git a/logUtils.ts b/logUtils.ts index b9ad49a..91b3767 100644 --- a/logUtils.ts +++ b/logUtils.ts @@ -19,6 +19,7 @@ export const WebLoggerName = "web"; // dev utility logger names export const DevLoggerName = "dev"; export const DataproxyLoggerName = "dataproxy"; +export const TestWebhookReceiverLoggerName = "testWebhookReceiver"; // internal logger names export const ControlLoggerName = "control"; diff --git a/testWebhookReceiver.ts b/testWebhookReceiver.ts index a0cbf59..aeed0d2 100644 --- a/testWebhookReceiver.ts +++ b/testWebhookReceiver.ts @@ -1,7 +1,19 @@ +import { ConsoleHandler } from "https://deno.land/std@0.196.0/log/handlers.ts"; +import { + getLogger, + setup as setupLog, +} from "https://deno.land/std@0.196.0/log/mod.ts"; + import { Application as OakApplication, } from "https://deno.land/x/oak@v12.5.0/mod.ts"; + import { parse } from "npm:lossless-json"; + +import { + defaultLogFormatter, + TestWebhookReceiverLoggerName, +} from "./logUtils.ts"; import { runAndCleanup } from "./runHelpers.ts"; function numberParser(value: string) { @@ -11,10 +23,11 @@ function numberParser(value: string) { } export async function testWebhookReceiver() { + const logger = getLogger(TestWebhookReceiverLoggerName); const abortController = new AbortController(); const app = new OakApplication(); app.use(async (ctx) => { - console.log( + logger.info( "Received Webhook:", parse( await ctx.request.body({ type: "text" }).value, @@ -25,12 +38,15 @@ export async function testWebhookReceiver() { ctx.response.body = ""; }); + const port = 8888; + logger.info(`Test webhook receiver listening on port ${port}.`); const runningPromise = app.listen({ - port: 8888, + port, signal: abortController.signal, }); async function cleanup() { + logger.info(`Stopping test webhook receiver.`); abortController.abort(); await runningPromise; } @@ -38,4 +54,20 @@ export async function testWebhookReceiver() { return await Promise.resolve({ runningPromise, cleanup }); } -if (import.meta.main) await runAndCleanup(testWebhookReceiver); +if (import.meta.main) { + setupLog({ + handlers: { + console: new ConsoleHandler("DEBUG", { + formatter: defaultLogFormatter, + }), + }, + + loggers: { + [TestWebhookReceiverLoggerName]: { + level: "DEBUG", + handlers: ["console"], + }, + }, + }); + await runAndCleanup(testWebhookReceiver); +}