Chg: loggerをclassに変更 / Feat: loggerでの発生場所の記録
This commit is contained in:
@@ -5,18 +5,20 @@ import staticStream from "@fastify/static";
|
||||
import { styleText } from "node:util";
|
||||
import Routes from "@/routes";
|
||||
import Database from "@/lib/db";
|
||||
import logger from "@/lib/logger";
|
||||
import Logger from "@/lib/logger";
|
||||
import AccessLog from "@/lib/access";
|
||||
import Authorization from "@/lib/auth";
|
||||
import { RequestContext } from "@mikro-orm/core";
|
||||
import { DatabaseError, ErrorBase } from "@/errors";
|
||||
import { ConfigEntity } from "@/modules/entities/Config";
|
||||
|
||||
process.title = "Chat";
|
||||
process.title = "LynqChat";
|
||||
|
||||
const logger = new Logger("Core");
|
||||
logger.info("Process started...");
|
||||
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
process.title = "Chat backend";
|
||||
process.title = "LynqChat backend";
|
||||
logger.warn(styleText(["red", "bold", "bgYellow"], "Development environment avaiable!!"));
|
||||
}
|
||||
|
||||
@@ -38,7 +40,8 @@ try {
|
||||
}));
|
||||
}
|
||||
|
||||
logger.error("Unknown error: ", err);
|
||||
const customLogger = new Logger("Unknown(Error handler)");
|
||||
customLogger.error("Unknown error:", err);
|
||||
|
||||
return res.status(500).send(ErrorBase({
|
||||
bad: "server",
|
||||
@@ -56,7 +59,7 @@ try {
|
||||
index: "/",
|
||||
});
|
||||
} catch (err) {
|
||||
logger.error("Error: It's in production but the frontend dist is not found.");
|
||||
logger.error("It's in production but the frontend dist is not found.");
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
@@ -101,7 +104,8 @@ try {
|
||||
}));
|
||||
}
|
||||
} catch (err) {
|
||||
logger.error("Database Error: Could not check if already initialization:", err);
|
||||
const customLogger = new Logger("Check already initialized hook");
|
||||
customLogger.error("Database error: Could not check if already initialization:", err);
|
||||
|
||||
return res.code(500).send(DatabaseError());
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { FastifyPluginCallback } from "fastify";
|
||||
import logger from "@/lib/logger";
|
||||
import Logger from "@/lib/logger";
|
||||
import fp from "fastify-plugin";
|
||||
|
||||
declare module "fastify" {
|
||||
@@ -8,6 +8,8 @@ declare module "fastify" {
|
||||
}
|
||||
}
|
||||
|
||||
const logger = new Logger("Lib | access.ts");
|
||||
|
||||
const AccessLog: FastifyPluginCallback = (fastify) => {
|
||||
fastify.addHook("onRequest", (req, res, done) => {
|
||||
req.startTime = process.hrtime.bigint();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { FastifyPluginCallback } from "fastify";
|
||||
import fp from "fastify-plugin";
|
||||
import { TokenEntity } from "@/modules/entities/Token";
|
||||
import logger from "./logger";
|
||||
import Logger from "./logger";
|
||||
import { DatabaseError, ErrorBase } from "@/errors";
|
||||
|
||||
declare module "fastify" {
|
||||
@@ -10,6 +10,8 @@ declare module "fastify" {
|
||||
}
|
||||
}
|
||||
|
||||
const logger = new Logger("Lib | auth.ts");
|
||||
|
||||
const Authorization: FastifyPluginCallback = (fastify) => {
|
||||
fastify.addHook("onRequest", async (req, res) => {
|
||||
const token = req.headers["authorization"];
|
||||
|
||||
@@ -2,7 +2,9 @@ import z from "zod/v3";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { parse as yamlParse } from "yaml";
|
||||
import { EOL } from "node:os";
|
||||
import logger from "@/lib/logger";
|
||||
import Logger from "@/lib/logger";
|
||||
|
||||
const logger = new Logger("Lib | config.ts");
|
||||
|
||||
const schema = z.object({
|
||||
server: z.object({
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { FastifyPluginAsync } from "fastify";
|
||||
import fp from "fastify-plugin";
|
||||
import { MikroORM } from "@mikro-orm/postgresql";
|
||||
import config from "@/mikro-orm.config";
|
||||
import logger from "@/lib/logger";
|
||||
import Logger from "@/lib/logger";
|
||||
|
||||
declare module "fastify" {
|
||||
interface FastifyInstance {
|
||||
@@ -10,6 +10,8 @@ declare module "fastify" {
|
||||
}
|
||||
}
|
||||
|
||||
const logger = new Logger("Lib | db.ts");
|
||||
|
||||
const Database: FastifyPluginAsync = async (fastify) => {
|
||||
const orm = await MikroORM.init(config);
|
||||
|
||||
|
||||
@@ -4,13 +4,14 @@ import { EOL } from "node:os";
|
||||
const createLog = (
|
||||
nativeLogger: (...args: any[]) => void,
|
||||
type: string,
|
||||
location: string,
|
||||
...args: any[]
|
||||
) => {
|
||||
if (args[0] instanceof Array) {
|
||||
args = args[0];
|
||||
}
|
||||
|
||||
const content = `${new Date().toLocaleString()} [${type}] ${args.join("\n").replaceAll("\n", "\n ")}`;
|
||||
const content = `${new Date().toLocaleString()} ${type} [${location}] ${args.join("\n").replaceAll("\n", "\n ")}`;
|
||||
|
||||
nativeLogger(content);
|
||||
|
||||
@@ -20,15 +21,30 @@ const createLog = (
|
||||
`${content.toString().replace(/\x1B\[[0-9;]*m/g, '')}${EOL}`
|
||||
);
|
||||
} catch (err) {
|
||||
console.error(`${new Date().toLocaleString()} Logger: Failed to write to file.`);
|
||||
console.error(`${new Date().toLocaleString()} [ERROR] [Lib | logger.ts] Failed to write to file.`);
|
||||
}
|
||||
}
|
||||
|
||||
const logger = {
|
||||
log: (...args: any[]) => createLog(console.log, "LOG", args),
|
||||
info: (...args: any[]) => createLog(console.info, "INFO", args),
|
||||
error: (...args: any[]) => createLog(console.error, "ERROR", args),
|
||||
warn: (...args: any[]) => createLog(console.warn, "WARN", args),
|
||||
}
|
||||
export default class Logger {
|
||||
public location: string;
|
||||
|
||||
export default logger;
|
||||
constructor(location: string) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
public log(...args: any[]) {
|
||||
createLog(console.log, "LOG", this.location, args)
|
||||
}
|
||||
|
||||
public info(...args: any[]) {
|
||||
createLog(console.info, "INFO", this.location, args)
|
||||
}
|
||||
|
||||
public error(...args: any[]) {
|
||||
createLog(console.error, "ERROR", this.location, args)
|
||||
}
|
||||
|
||||
public warn(...args: any[]) {
|
||||
createLog(console.warn, "WARN", this.location, args)
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,9 @@ import { defineConfig } from "@mikro-orm/postgresql";
|
||||
import config from "@/lib/config";
|
||||
import { TsMorphMetadataProvider } from "@mikro-orm/reflection";
|
||||
import { Migrator } from "@mikro-orm/migrations";
|
||||
import logger from "@/lib/logger";
|
||||
import Logger from "@/lib/logger";
|
||||
|
||||
const logger = new Logger("MikroORM");
|
||||
|
||||
export default defineConfig({
|
||||
entities: ["./dist/modules/entities/**/*.js"],
|
||||
@@ -18,7 +20,7 @@ export default defineConfig({
|
||||
metadataProvider: TsMorphMetadataProvider,
|
||||
debug: process.env.NODE_ENV !== "production",
|
||||
logger: (message) => {
|
||||
logger.log(`[MikroORM] ${message}`);
|
||||
logger.log(message);
|
||||
},
|
||||
|
||||
dbName: config.database.database,
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import { DatabaseError, ErrorBase, InputError } from "@/errors";
|
||||
import logger from "@/lib/logger";
|
||||
import Logger from "@/lib/logger";
|
||||
import { TokenEntity } from "@/modules/entities/Token";
|
||||
import { UserEntity } from "@/modules/entities/User";
|
||||
import { UserRepository } from "@/modules/repositories/User";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
export default function SignIn(fastify: FastifyInstance) {
|
||||
const logger = new Logger("Endpoint | primary/signin");
|
||||
|
||||
fastify.post("/", async (req, res) => {
|
||||
res.header("Content-Type", "application/json");
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { UserEntity } from "@/modules/entities/User";
|
||||
import logger from "@/lib/logger";
|
||||
import Logger from "@/lib/logger";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { UserRepository } from "@/modules/repositories/User";
|
||||
import { DatabaseError, InputError } from "@/errors";
|
||||
|
||||
export default function SignUp(fastify: FastifyInstance) {
|
||||
const logger = new Logger("Endpoint | primary/signup");
|
||||
|
||||
fastify.post("/", async (req, res) => {
|
||||
res.header("Content-Type", "application/json");
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { DatabaseError } from "@/errors";
|
||||
import logger from "@/lib/logger";
|
||||
import Logger from "@/lib/logger";
|
||||
import { ConfigEntity } from "@/modules/entities/Config";
|
||||
import { UserEntity } from "@/modules/entities/User";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
export default async function ServerInfo(fastify: FastifyInstance) {
|
||||
const logger = new Logger("Endpoint | server-info");
|
||||
|
||||
fastify.post("/", async (req, res) => {
|
||||
try {
|
||||
const config = fastify.orm.em.getRepository(ConfigEntity);
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { UserEntity } from "@/modules/entities/User";
|
||||
import logger from "@/lib/logger";
|
||||
import Logger from "@/lib/logger";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { UserRepository } from "@/modules/repositories/User";
|
||||
import { DatabaseError, ErrorBase, InputError } from "@/errors";
|
||||
|
||||
export default function CreateAdmin(fastify: FastifyInstance) {
|
||||
const logger = new Logger("Endpoint | setup/create-admin");
|
||||
|
||||
fastify.post("/", async (req, res) => {
|
||||
res.header("Content-Type", "application/json");
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { DatabaseError, ErrorBase, InputError } from "@/errors";
|
||||
import logger from "@/lib/logger";
|
||||
import Logger from "@/lib/logger";
|
||||
import { ConfigEntity } from "@/modules/entities/Config";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import z from "zod/v3";
|
||||
|
||||
export default function Initialization(fastify: FastifyInstance) {
|
||||
const logger = new Logger("Endpoint | setup/initialization");
|
||||
|
||||
fastify.post("/", async (req, res) => {
|
||||
res.header("Content-Type", "application/json");
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import ErrorBase from "../../modules/error";
|
||||
import DatabaseError from "../../modules/error/database";
|
||||
import Success from "../../modules/response/success";
|
||||
|
||||
export default interface SetupInitilization {
|
||||
export default interface SetupInitialization {
|
||||
"setup/initilization": {
|
||||
body: {
|
||||
name: string;
|
||||
|
||||
Reference in New Issue
Block a user