Initial commit
This commit is contained in:
38
wwwroot/core/logging/logger.js
Normal file
38
wwwroot/core/logging/logger.js
Normal file
@@ -0,0 +1,38 @@
|
||||
import path from "path";
|
||||
import { appendFile, mkdir } from "fs/promises";
|
||||
|
||||
export class Logger{
|
||||
|
||||
static log(message){
|
||||
console.log(message)
|
||||
}
|
||||
|
||||
static async error(message, error) {
|
||||
try {
|
||||
const currentDateTime = new Date();
|
||||
const date = currentDateTime.toLocaleDateString("fr-FR", {
|
||||
day: "2-digit",
|
||||
month: "2-digit",
|
||||
year: "2-digit"
|
||||
}).replace(/\//g, "-");
|
||||
|
||||
const time = currentDateTime.toLocaleTimeString("fr-FR", {
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
second: "2-digit",
|
||||
hour12: false
|
||||
});
|
||||
|
||||
const logsDir = path.resolve("./logs");
|
||||
|
||||
const fullPath = path.join(logsDir, `${date}.error.log`);
|
||||
|
||||
await mkdir(logsDir, { recursive: true });
|
||||
|
||||
await appendFile(fullPath, `${time} - ${message} -> ${JSON.stringify(error)} \n`);
|
||||
console.error(`An error occured. The incident has been logged in ${fullPath}`)
|
||||
} catch (err) {
|
||||
console.error("Error writing log:", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user