Skip to content

Commit

Permalink
fix: Add properly padded timestamp to logger with date added
Browse files Browse the repository at this point in the history
  • Loading branch information
binaryoverload committed Jan 7, 2025
1 parent d77c15d commit 69609c3
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,41 @@ const streams = {
info: fs.createWriteStream(`${root}/logs/info.log`)
} as const;

function getCurrentTimestamp(): string {
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are 0-indexed
const day = String(date.getDate()).padStart(2, '0');
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');

return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

export function LOG_SUCCESS(input: string): void {
const time = new Date();
input = `[${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}] [SUCCESS]: ${input}`;
input = `[${getCurrentTimestamp()}] [SUCCESS]: ${input}`;
streams.success.write(`${input}\n`);

console.log(`${input}`.green.bold);
}

export function LOG_ERROR(input: string): void {
const time = new Date();
input = `[${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}] [ERROR]: ${input}`;
input = `[${getCurrentTimestamp()}] [ERROR]: ${input}`;
streams.error.write(`${input}\n`);

console.error(`${input}`.red.bold);
}

export function LOG_WARN(input: string): void {
const time = new Date();
input = `[${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}] [WARN]: ${input}`;
input = `[${getCurrentTimestamp()}] [WARN]: ${input}`;
streams.warn.write(`${input}\n`);

console.log(`${input}`.yellow.bold);
}

export function LOG_INFO(input: string): void {
const time = new Date();
input = `[${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}] [INFO]: ${input}`;
input = `[${getCurrentTimestamp()}] [INFO]: ${input}`;
streams.info.write(`${input}\n`);

console.log(`${input}`.cyan.bold);
Expand Down

0 comments on commit 69609c3

Please sign in to comment.