Skip to content

Commit

Permalink
feat: logger
Browse files Browse the repository at this point in the history
  • Loading branch information
nin0-dev committed Oct 26, 2024
1 parent 0b30b2f commit 0a40a44
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 12 deletions.
46 changes: 46 additions & 0 deletions src/common/Yapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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 TRC\x1b[90;1m \x1b[0;90m", getDateTime(), obj);
}
debug(obj) {
if (__DEV__)
console.log("\x1b[90;1;47m DBG", `\x1b[0m \x1b[90m${getDateTime()}`, obj, "\x1b[0m");
}
info(obj) {
if (__DEV__) console.log("\x1b[44;1m INF", `\x1b[0m \x1b[90m${getDateTime()}\x1b[0m`, obj);
}
warn(obj) {
console.log("\x1b[43;1m WRN", `\x1b[0m \x1b[90m${getDateTime()}\x1b[0m`, obj);
}
error(obj) {
console.log("\x1b[41;1m ERR", `\x1b[0m \x1b[90m${getDateTime()}\x1b[0m`, obj);
}
fatal(obj) {
console.log(
"\x1b[40;31;1m FTL",
`\x1b[0m \x1b[31;1m${getDateTime()}\x1b[0;31m`,
obj,
"\x1b[0m"
);
}
child() {
return new Yapper();
}
level; // unused
silent; // unused
}
24 changes: 12 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,11 @@ 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";
import { __DEV__ } from "./common/constants.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, {
Expand All @@ -26,6 +17,15 @@ server.register(bootstrap, {

server.setErrorHandler(ERROR_HANDLER);

if (__DEV__) {
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);
Expand Down

0 comments on commit 0a40a44

Please sign in to comment.