Use static methods for logger
This commit is contained in:
parent
d605ee07f2
commit
2ec6dd5476
|
@ -1,7 +1,7 @@
|
||||||
import { PostListener, MessagesResponder } from "./routes";
|
import { PostListener, MessagesResponder } from "./routes";
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import dotenv from "dotenv";
|
import dotenv from "dotenv";
|
||||||
import { emp, Logger } from "./utils";
|
import { Logger } from "./utils";
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
@ -15,5 +15,5 @@ post.start();
|
||||||
query.start();
|
query.start();
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
logger.log(`Server started on ${emp(`http://localhost:${port}`)}`);
|
logger.log(`Server started on ${Logger.emp(`http://localhost:${port}`)}`);
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { userReviewSchema, typeJson } from "../types";
|
import { userReviewSchema, typeJson } from "../types";
|
||||||
import { bold, emp, Logger, responder, data } from "../utils";
|
import { Logger, responder, data } from "../utils";
|
||||||
import { Express, Request, Response } from "express";
|
import { Express, Request, Response } from "express";
|
||||||
|
|
||||||
export class PostListener {
|
export class PostListener {
|
||||||
|
@ -13,7 +13,7 @@ export class PostListener {
|
||||||
public async start(): Promise<void> {
|
public async start(): Promise<void> {
|
||||||
this.server.post("/post", (req: Request, res: Response) => {
|
this.server.post("/post", (req: Request, res: Response) => {
|
||||||
req.on("data", async (chunk) => {
|
req.on("data", async (chunk) => {
|
||||||
const sender = `${bold(emp("Client:"))} ${emp(req.headers["user-agent"])}`;
|
const sender = `${Logger.bold(Logger.emp("Client:"))} ${Logger.emp(req.headers["user-agent"])}`;
|
||||||
|
|
||||||
let temp: any;
|
let temp: any;
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { Express, Request, Response } from "express";
|
import { Express, Request, Response } from "express";
|
||||||
import { Logger, data, emp } from "../utils";
|
import { Logger, data } from "../utils";
|
||||||
import { IdRequest, typeJson } from "../types";
|
import { IdRequest, typeJson } from "../types";
|
||||||
|
|
||||||
export class MessagesResponder {
|
export class MessagesResponder {
|
||||||
|
@ -19,7 +19,7 @@ export class MessagesResponder {
|
||||||
res.write(JSON.stringify(result));
|
res.write(JSON.stringify(result));
|
||||||
res.end();
|
res.end();
|
||||||
|
|
||||||
this.logger.log(`${emp(receiver)} <~ "${req.path}"`);
|
this.logger.log(`${Logger.emp(receiver)} <~ "${req.path}"`);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.server.get("/reviews/:id", (req: IdRequest, res: Response) => {
|
this.server.get("/reviews/:id", (req: IdRequest, res: Response) => {
|
||||||
|
@ -30,7 +30,7 @@ export class MessagesResponder {
|
||||||
res.write(JSON.stringify(result));
|
res.write(JSON.stringify(result));
|
||||||
res.end();
|
res.end();
|
||||||
|
|
||||||
this.logger.log(`${emp(receiver)} <~ "${req.path}"`);
|
this.logger.log(`${Logger.emp(receiver)} <~ "${req.path}"`);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.logger.log("Loading complete.");
|
this.logger.log("Loading complete.");
|
||||||
|
|
|
@ -9,12 +9,12 @@ export class Logger {
|
||||||
public isMainFunction: boolean;
|
public isMainFunction: boolean;
|
||||||
|
|
||||||
constructor(origin?: string) {
|
constructor(origin?: string) {
|
||||||
this._wrn = chalk.yellow("[WARN] ");
|
this._wrn = Logger.wrn("[WARN] ");
|
||||||
this._err = chalk.red("[ERROR] ");
|
this._err = Logger.err("[ERROR] ");
|
||||||
this.name = origin ?? "Anonymous";
|
this.name = origin ?? "Anonymous";
|
||||||
this.isMainFunction = this.name === "Main";
|
this.isMainFunction = this.name === "Main";
|
||||||
|
|
||||||
this._main = chalk.bold.gray(`[SRS | ${emp(this.name)}] `);
|
this._main = chalk.bold.gray(`[SRS | ${Logger.emp(this.name)}] `);
|
||||||
}
|
}
|
||||||
|
|
||||||
public log(text?: any, args?: any): void {
|
public log(text?: any, args?: any): void {
|
||||||
|
@ -38,12 +38,12 @@ export class Logger {
|
||||||
? console.error(this._main + this._err + text, args)
|
? console.error(this._main + this._err + text, args)
|
||||||
: console.error(this._main + this._err + text);
|
: console.error(this._main + this._err + text);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static readonly emp = chalk.green;
|
||||||
|
public static readonly wrn = chalk.yellow;
|
||||||
|
public static readonly err = chalk.red;
|
||||||
|
|
||||||
|
public static readonly bold = chalk.bold;
|
||||||
|
public static readonly itl = chalk.italic;
|
||||||
|
public static readonly udln = chalk.underline;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const emp = chalk.green;
|
|
||||||
export const wrn = chalk.yellow;
|
|
||||||
export const err = chalk.red;
|
|
||||||
|
|
||||||
export const bold = chalk.bold;
|
|
||||||
export const itl = chalk.italic;
|
|
||||||
export const udln = chalk.underline;
|
|
||||||
|
|
Loading…
Reference in a new issue