Skip to content

Commit

Permalink
로그 출력 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
if1live committed Oct 2, 2023
1 parent 6d456da commit c699cb3
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import {
PostgresInput,
ProviderInput,
RedisNativeInput,
TouchRejectedResult,
UpstashRedisInput,
} from "./types.js";
import { providerInputs, DISCORD_WEBHOOK_URL } from "./settings.js";
import * as settings from "./settings.js";
import { dynamodb } from "./instances.js";

const execute_mysql = async (input: MysqlInput) => {
Expand Down Expand Up @@ -58,7 +59,7 @@ const execute = async (input: ProviderInput) => {

export const touch = async () => {
const entries = await Promise.all(
providerInputs.map(async (input) => execute(input)),
settings.providerInputs.map(async (input) => execute(input)),
);

for (const entry of entries) {
Expand All @@ -74,27 +75,44 @@ export const touch = async () => {

// discord 기록은 실패할지 모르니까 마지막에 배치
{
const blocks = entries.map((entry) => {
const block_env = [
"## environment",
`* NODE_ENV: ${settings.NODE_ENV}`,
`* STAGE: ${settings.STAGE}`,
].join("\n");

// 성공/실패만 간단하게 보고싶다
const lines_status = entries.map((entry) => {
const { label, result } = entry;
const ok = result.status === "fulfilled" ? "ok" : "error";
const line_header = `## ${label}: ${ok}`;
const line_detail =
result.status === "fulfilled"
? "```" + JSON.stringify(result.value, null, 2) + "```"
: "```" + JSON.stringify(result.reason, null, 2) + "```";
return `* ${label}: ${ok}`;
});
const block_status = ["## status", ...lines_status].join("\n");

// 실패는 상세 로그가 필요하다. 성공 로그는 노이즈에 불과하다.
const lines_error = entries.flatMap((entry) => {
if (entry.result.status === "fulfilled") {
return null;
}

const block = [line_header, line_detail].join("\n");
return block;
const { label, result } = entry;
const line_header = `### ${label}`;
const line_detail =
"```" + JSON.stringify(result.reason, null, 2) + "```";
return [line_header, line_detail];
});
const text = blocks.join("\n\n");
const block_error =
lines_error.length > 0 ? ["## error", ...lines_error].join("\n") : "";

const text = [block_env, block_status, block_error].join("\n\n");
await sendMessageToDiscord(text);
}

return entries;
};

export const sendMessageToDiscord = async (text: string) => {
const url = DISCORD_WEBHOOK_URL;
const url = settings.DISCORD_WEBHOOK_URL;
if (!url) {
return { ok: false, reason: "no webhook url" };
}
Expand Down

0 comments on commit c699cb3

Please sign in to comment.