Skip to content
This repository was archived by the owner on Oct 1, 2025. It is now read-only.

Commit 925b3f3

Browse files
committed
feat: added initial Logger implementation
1 parent e210465 commit 925b3f3

File tree

6 files changed

+234
-0
lines changed

6 files changed

+234
-0
lines changed

.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# IDE folders
2+
/.idea
3+
/.vscode
4+
5+
# Node modules
6+
/node_modules
7+
8+
# Environment variables
9+
/**/*.env
10+
11+
# Executable files
12+
/**/*.exe
13+
14+
# Private and public keys
15+
/**/*.ed
16+
/**/*.pub
17+
/**/*.pem
18+
19+
# Commands
20+
/**/*.cmd
21+
/**/*.sh
22+
/**/*.bat
23+
24+
# NPM configuration
25+
/.npmrc

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Logger from "./lib/logger.js";
2+
3+
export default Logger;

lib/date.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Get the current date in the format
2+
function GetFormattedDate({
3+
locales = "en-US",
4+
options = {
5+
year: 'numeric',
6+
month: '2-digit',
7+
day: '2-digit',
8+
hour: '2-digit',
9+
minute: '2-digit',
10+
second: '2-digit'
11+
}
12+
}) {
13+
const date = new Date();
14+
return new Intl.DateTimeFormat(locales, options).format(date);
15+
}

lib/log_entry.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// LogEntry is a class that represents a log entry
2+
import GetFormattedDate from "./date.js";
3+
4+
// Log entry types
5+
export const LogEntryType = {
6+
ERROR: 'ERROR',
7+
INFO: 'INFO',
8+
DEBUG: 'DEBUG',
9+
WARNING: 'WARNING',
10+
CRITICAL: 'CRITICAL'
11+
};
12+
13+
export default class LogEntry {
14+
#type;
15+
#message;
16+
#locales
17+
#options
18+
19+
// Constructor of the class
20+
constructor({type = LogEntryType.INFO, message = "", locales, options}) {
21+
// Set the properties of the log entry
22+
this.#type = type;
23+
this.#message = message;
24+
this.#locales = locales;
25+
this.#options = options;
26+
}
27+
28+
// String representation of the log entry
29+
toString() {
30+
return `${GetFormattedDate()} [${this.#type}]: ${this.#message}`;
31+
}
32+
}

lib/logger.js

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import path from 'path';
2+
import fs from 'fs';
3+
import LogEntry, {LogEntryType} from "./log_entry.js";
4+
5+
// Errors that can be thrown by the Logger class
6+
export const ErrMissingLogPath = new Error('log output directory is required');
7+
export const ErrMissingLogFilename = new Error('log filename is required');
8+
9+
// Logger is a class that logs messages to the console and to a file.
10+
export default class Logger {
11+
#save
12+
#logPath
13+
#logFilename
14+
#logFilePath
15+
#error
16+
#info
17+
#debug
18+
#warning
19+
#format
20+
21+
// The constructor receives an object with the properties to configure the logger
22+
constructor({
23+
save = false,
24+
logPath = "",
25+
logFilename = "",
26+
error = true,
27+
info = true,
28+
debug = true,
29+
warning = true,
30+
format = {
31+
locales: 'en-US',
32+
options: {
33+
year: 'numeric',
34+
month: '2-digit',
35+
day: '2-digit',
36+
hour: '2-digit',
37+
minute: '2-digit',
38+
second: '2-digit',
39+
hour12: false
40+
}
41+
}
42+
}) {
43+
// Set the properties of the logger
44+
this.#save = save;
45+
this.#logPath = logPath;
46+
this.#error = error;
47+
this.#info = info;
48+
this.#debug = debug;
49+
this.#warning = warning;
50+
this.#format = format;
51+
52+
// Check if the save property is true
53+
if (save) {
54+
// Check if the logPath or logFilename property is empty
55+
if (logPath === "") {
56+
throw ErrMissingLogPath;
57+
}
58+
if (logFilename === "") {
59+
throw ErrMissingLogFilename;
60+
}
61+
62+
// Check if the logPath directory exists
63+
this.#logPath = path.join(__dirname, logPath);
64+
if (!fs.existsSync(this.#logPath)) {
65+
fs.mkdirSync(this.#logPath);
66+
}
67+
68+
// Check if the logFilename file exists
69+
this.#logFilename = logFilename
70+
this.#logFilePath = path.join(this.#logPath, logFilename);
71+
if (!fs.existsSync(this.#logFilename)) {
72+
fs.writeFileSync(this.#logFilename, "");
73+
}
74+
}
75+
}
76+
77+
// Append a log entry to the log file
78+
#appendLogEntryToFile(logEntry) {
79+
fs.appendFile(this.#logFilePath, logEntry, err => {
80+
if (err) throw err;
81+
});
82+
}
83+
84+
// Write a log entry to the log file
85+
log({type = LogEntryType.INFO, message = ""}) {
86+
// Create a new LogEntry object
87+
const logEntry = new LogEntry({
88+
type,
89+
message,
90+
locales: this.#format.locales,
91+
options: this.#format.options,
92+
});
93+
94+
// Format the log entry as a string
95+
const logEntryStr = String(logEntry);
96+
97+
// Check if the save property is true
98+
if (this.#save) {
99+
this.#appendLogEntryToFile(logEntryStr + "\n");
100+
}
101+
console.log(logEntryStr);
102+
}
103+
104+
// Write an error log entry to the log file
105+
error(message) {
106+
this.log({
107+
type: LogEntryType.ERROR,
108+
message,
109+
});
110+
}
111+
112+
// Write an info log entry to the log file
113+
info(message) {
114+
this.log({
115+
type: LogEntryType.INFO,
116+
message
117+
});
118+
}
119+
120+
// Write a warning log entry to the log file
121+
warning(message) {
122+
this.log({
123+
type: LogEntryType.WARNING,
124+
message
125+
});
126+
}
127+
128+
// Write a debug log entry to the log file
129+
debug(message) {
130+
this.log({
131+
type: LogEntryType.DEBUG,
132+
message
133+
});
134+
}
135+
136+
// Write a critical log entry to the log file
137+
critical(message) {
138+
this.log({
139+
type: LogEntryType.CRITICAL,
140+
message
141+
});
142+
}
143+
}

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "@ralvarezdev/js-logger",
3+
"version": "0.1.0",
4+
"description": "Logger for JavaScript Projects",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "exit 0"
8+
},
9+
"keywords": [],
10+
"author": "Ramón Álvarez",
11+
"license": "ISC",
12+
"type": "module",
13+
"publishConfig": {
14+
"@ralvarezdev:registry": "https://npm.pkg.github.com"
15+
}
16+
}

0 commit comments

Comments
 (0)