Skip to content

Commit

Permalink
v1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Abadima committed Oct 15, 2024
1 parent 8af0ac4 commit c11e162
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 129 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Package Management
package-lock.json
pnpm-lock.yaml
node_modules

# Dist Files
dist
logger.d.ts
logger.js
9 changes: 5 additions & 4 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# Package Management
package-lock.json
pnpm-lock.yaml
node_modules

# TypeScript Files
tsconfig.json
*.ts
logger.ts

# Source Files
assets
.gitignore
dist/src
.gitattributes
CHANGELOGS.md
eslint.config.mjs
src
Tests
src
35 changes: 35 additions & 0 deletions CHANGELOGS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Notable Changes & Versioning
All notable changes to this project will be documented in this file :3

This project adheres to [Semantic Versioning](https://semver.org/) starting `v1.0.2`.


## 1.0.2 [2024-OCT-15]
### Changed
- Class name from `Logger` to `Logena`.

### Fixed
- Class methods, now they're actually accessible from the class instance.

## 1.0.1 [2024-OCT-15]
### Changed
- Moved `logger.js` from `/dist` to project root.
- Update `package.json` to reflect changes.
- Lowered minimum NodeJS version to `14.0.0`, let's be fr, we don't need to be that strict.

### Removed
- `/dist` directory.
- `/Tests` directory.
- `clean.mjs` (Now runs via npm script `uglifyjs ...`)

## 1.0.0 [2024-OCT-15]

### Added
- Initial release of the `Logger` class.
- Added `terminalColors` object for terminal text styling.
- Implemented `Logger` class with the following methods:
- `set(config: { debug?: boolean, appName?: string, useTimestamps?: boolean, colors?: { timestamp?: keyof typeof terminalColors.textColors, appName?: keyof typeof terminalColors.textColors, message?: keyof typeof terminalColors.textColors, levels?: { info?: keyof typeof terminalColors.textColors, warn?: keyof typeof terminalColors.textColors, error?: keyof typeof terminalColors.textColors, debug?: keyof typeof terminalColors.textColors } } }): void` - Configure the logger settings.
- `info(message: string | object): void` - Log an info message to the console.
- `warn(message: string | object): void` - Log a warning to the console.
- `error(message: string | object): void` - Log an error to the console.
- `debug(message: string | object): void` - Log a debug message to the console (only if `debugMode` is enabled).
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ npm install logena
- `appName` (optional): `string` - Set the application name (Overrides preset name).
- `message`: `string | object` - The message to log.

# Examples
# JS Examples
- TypeScript works as well, via import syntax.
```js
const logena = require('logena');
// or import logena from 'logena';
const {Logena} = require('logena');
// or import Logena from 'logena';

logena.set({
Logena.set({
debug: true,
appName: "LOGENA",
colors: {
Expand All @@ -78,10 +79,10 @@ logena.set({
useTimestamps: true
});

logena.info("Hello, world!"); // 2024-10-15 18:00:00Z [LOGENA] INFO: Hello, world!
logena.warn("Hello, world!"); // 2024-10-15 18:00:00Z [LOGENA] WARN: Hello, world!
logena.error("Hello, world!"); // 2024-10-15 18:00:00Z [LOGENA] ERROR: Hello, world!
logena.debug("Hello, world!"); // 2024-10-15 18:00:00Z [LOGENA] DEBUG: Hello, world!
Logena.info("Hello, world!"); // 2024-10-15 18:00:00Z [LOGENA] INFO: Hello, world!
Logena.warn("Hello, world!"); // 2024-10-15 18:00:00Z [LOGENA] WARN: Hello, world!
Logena.error("Hello, world!"); // 2024-10-15 18:00:00Z [LOGENA] ERROR: Hello, world!
Logena.debug("Hello, world!"); // 2024-10-15 18:00:00Z [LOGENA] DEBUG: Hello, world!
```

# License
Expand Down
43 changes: 0 additions & 43 deletions Tests/clean.mjs

This file was deleted.

2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const compat = new FlatCompat({
});

export default [{
ignores: ["**/dist/", "eslint.config.mjs"],
ignores: ["src/tests.js", "eslint.config.mjs"],
}, ...compat.extends("eslint:recommended", "plugin:@typescript-eslint/recommended"), {
plugins: {
"@typescript-eslint": typescriptEslint,
Expand Down
104 changes: 52 additions & 52 deletions logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,52 +28,51 @@ const terminalColors = {

/**
* Logger Class
* @class Logger
* @class Logena
* @description A simple logger class that can be used to log messages to the console.
* @example const logger = new Logger();
* @example logger.set({ debug: true, appName: 'MyApp', useTimestamps: true });
* @example logger.info('This is a log message');
*/
class Logger {
private appName: string = "";
private colors: {
timestamp?: keyof typeof terminalColors.textColors,
appName?: keyof typeof terminalColors.textColors,
message?: keyof typeof terminalColors.textColors,
levels: {
info: keyof typeof terminalColors.textColors,
warn: keyof typeof terminalColors.textColors,
error: keyof typeof terminalColors.textColors,
debug: keyof typeof terminalColors.textColors
}
} = {
class Logena {
private static appName: string = "";
private static colors: {
timestamp?: keyof typeof terminalColors.textColors,
appName?: keyof typeof terminalColors.textColors,
message?: keyof typeof terminalColors.textColors,
levels: {
info: keyof typeof terminalColors.textColors,
warn: keyof typeof terminalColors.textColors,
error: keyof typeof terminalColors.textColors,
debug: keyof typeof terminalColors.textColors
}
} = {
levels: {
info: "blue",
warn: "yellow",
error: "red",
debug: "cyan"
}
};
private debugMode: boolean = false;
private useTimestamps: boolean = false;

private static debugMode: boolean = false;
private static useTimestamps: boolean = false;

set(config: {
debug?: boolean,
appName?: string,
useTimestamps?: boolean,
colors?: {
timestamp?: keyof typeof terminalColors.textColors,
appName?: keyof typeof terminalColors.textColors,
message?: keyof typeof terminalColors.textColors,
levels?: {
info?: keyof typeof terminalColors.textColors,
warn?: keyof typeof terminalColors.textColors,
error?: keyof typeof terminalColors.textColors,
debug?: keyof typeof terminalColors.textColors
}
}
}): void {
static set(config: {
debug?: boolean,
appName?: string,
useTimestamps?: boolean,
colors?: {
timestamp?: keyof typeof terminalColors.textColors,
appName?: keyof typeof terminalColors.textColors,
message?: keyof typeof terminalColors.textColors,
levels?: {
info?: keyof typeof terminalColors.textColors,
warn?: keyof typeof terminalColors.textColors,
error?: keyof typeof terminalColors.textColors,
debug?: keyof typeof terminalColors.textColors
}
}
}): void {
if (config.appName !== undefined) this.appName = config.appName;
if (config.debug !== undefined) this.debugMode = config.debug;
if (config.useTimestamps !== undefined) this.useTimestamps = config.useTimestamps;
Expand All @@ -89,7 +88,7 @@ class Logger {
}
}

private formatMessage(level: string, message: string | object): string {
private static formatMessage(level: string, message: string | object): string {
const formatTimestamp = (date: Date): string => date.toISOString().replace("T", " ").replace(/\..+/, "Z");

const timestamp = this.useTimestamps ? `${terminalColors.textColors[this.colors.timestamp || "white"]}${formatTimestamp(new Date())}${terminalColors.reset} ` : "";
Expand All @@ -100,39 +99,40 @@ class Logger {
}

/**
* Log an info message to the console
* @param (string | object) message - The message to log
* @returns void
*/
info(message: string | object): void {
* Log an info message to the console
* @param (string | object) message - The message to log
* @returns void
*/
static info(message: string | object): void {
console.log(this.formatMessage("INFO", message));
}

/**
* Log a warning to the console
* @param (string | object) message - The message to log
*/
warn(message: string | object): void {
* Log a warning to the console
* @param (string | object) message - The message to log
*/
static warn(message: string | object): void {
console.warn(this.formatMessage("WARN", message));
}

/**
* Log an error to the console
* @param (string | object) message - The message to log
*/
error(message: string | object): void {
* Log an error to the console
* @param (string | object) message - The message to log
*/
static error(message: string | object): void {
console.error(this.formatMessage("ERROR", message));
}

/**
* Log a debug message to the console
* @param (string | object) message - The message to log
*/
debug(message: string | object): void {
* Log a debug message to the console
* @param (string | object) message - The message to log
*/
static debug(message: string | object): void {
if (this.debugMode) {
console.debug(this.formatMessage("DEBUG", message));
}
}
}

export default new Logger();
export {Logena};
export default Logena;
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"name": "logena",
"version": "1.0.0",
"version": "1.0.2",
"description": "Jena Logger",
"main": "dist/src/logger.js",
"main": "./logger.js",
"types": "./logger.d.ts",
"scripts": {
"build": "eslint . --fix && tsc && node Tests/clean.mjs",
"test": "npm run build && node dist/src/tests.js"
"build": "eslint . --fix && tsc && uglifyjs logger.js --comments all -o logger.js",
"test": "npm run build && node src/tests.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/abadima/logena"
"url": "git+https://github.com/abadima/logena.git"
},
"keywords": [
"colors",
Expand All @@ -30,12 +31,13 @@
"homepage": "https://github.com/abadima/logena#readme",
"devDependencies": {
"@eslint/js": "^9.12.0",
"@types/node": "^22.7.5",
"eslint": "^9.12.0",
"globals": "^15.11.0",
"typescript-eslint": "^8.9.0",
"uglify-js": "^3.19.3"
},
"engines": {
"node": ">=16.0.0"
"node": ">=14.0.0"
}
}
1 change: 1 addition & 0 deletions src/tests.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
15 changes: 15 additions & 0 deletions src/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = require("../logger");
logger_1.Logena.set({
appName: "LOGENA",
colors: {
appName: "brightGreen",
timestamp: "black",
},
debug: true,
useTimestamps: true
});
logger_1.Logena.info("This is an info message");
logger_1.Logena.warn("This is a warning message");
logger_1.Logena.error("This is an error message");
10 changes: 5 additions & 5 deletions src/tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Logger from "../logger";
import {Logena} from "../logger";

Logger.set({
Logena.set({
appName: "LOGENA",
colors: {
appName: "brightGreen",
Expand All @@ -10,6 +10,6 @@ Logger.set({
useTimestamps: true
});

Logger.info("This is an info message");
Logger.warn("This is a warning message");
Logger.error("This is an error message");
Logena.info("This is an info message");
Logena.warn("This is a warning message");
Logena.error("This is an error message");
Loading

0 comments on commit c11e162

Please sign in to comment.