From 172d68ec9fdffd6f10487de8e0b7f580464b229f Mon Sep 17 00:00:00 2001 From: nin0dev Date: Sat, 26 Oct 2024 13:10:13 -0400 Subject: [PATCH] feat: logger --- src/common/Yapper.ts | 47 ++++++++++++++++++++++++++++++++++++++++++++ src/index.ts | 21 +++++++++----------- 2 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 src/common/Yapper.ts diff --git a/src/common/Yapper.ts b/src/common/Yapper.ts new file mode 100644 index 0000000..b4ff76a --- /dev/null +++ b/src/common/Yapper.ts @@ -0,0 +1,47 @@ +import { __DEV__ } from "./constants"; + +function getDateTime() { + const now = new Date(); + const hours = String(now.getHours()).padStart(2, "0"); + const minutes = String(now.getMinutes()).padStart(2, "0"); + const seconds = String(now.getSeconds()).padStart(2, "0"); + const day = String(now.getDate()).padStart(2, "0"); + const month = String(now.getMonth() + 1).padStart(2, "0"); + const year = now.getFullYear(); + + return `${hours}:${minutes}:${seconds} ${day}-${month}-${year}`; +} + +export class Yapper { + constructor() {} + trace(obj) { + if (__DEV__) console.log("\x1b[90;1m Trace\x1b[90;1m \x1b[0;90m", getDateTime(), obj); + } + debug(obj) { + if (__DEV__) + console.log("\x1b[90;1;47m Debug", `\x1b[0m \x1b[90m${getDateTime()}`, obj, "\x1b[0m"); + } + info(obj) { + if (__DEV__) + console.log("\x1b[44;1m Info ", `\x1b[0m \x1b[90m${getDateTime()}\x1b[0m`, obj); + } + warn(obj) { + console.log("\x1b[43;1m Warn ", `\x1b[0m \x1b[90m${getDateTime()}\x1b[0m`, obj); + } + error(obj) { + console.log("\x1b[41;1m Error", `\x1b[0m \x1b[90m${getDateTime()}\x1b[0m`, obj); + } + fatal(obj) { + console.log( + "\x1b[40;31;1m Fatal", + `\x1b[0m \x1b[31;1m${getDateTime()}\x1b[0;31m`, + obj, + "\x1b[0m" + ); + } + child() { + return new Yapper(); + } + level; // unused + silent; // unused +} diff --git a/src/index.ts b/src/index.ts index bf9444a..6c04100 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,20 +3,10 @@ import fastify from "fastify"; import { bootstrap } from "fastify-decorators"; import { resolve } from "path"; import { ERROR_HANDLER } from "./common/error.js"; +import { Yapper } from "./common/Yapper.js"; export const server = fastify({ - logger: { - development: { - transport: { - target: "pino-pretty", - options: { - translateTime: "HH:MM:ss Z", - ignore: "pid,hostname" - } - } - }, - production: false - }[process.env.NODE_ENV] + loggerInstance: new Yapper("a") }); server.register(bootstrap, { @@ -26,6 +16,13 @@ server.register(bootstrap, { server.setErrorHandler(ERROR_HANDLER); +server.log.trace("Trace message"); +server.log.debug("Debug message"); +server.log.info("Info message"); +server.log.warn("Warn message"); +server.log.error("Error message"); +server.log.fatal("I killed myself"); + server.listen({ port: parseInt(process.env.PORT), host: process.env.HOST }, (err, address) => { if (err) { server.log.error(err);