Skip to content

Commit

Permalink
fix: class/file names
Browse files Browse the repository at this point in the history
  • Loading branch information
Fyrlex committed Jul 3, 2024
1 parent 4ca78e8 commit 56b3b68
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 105 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ Install loggage by entering `npm i @countbot/loggage` into your CLI.

# Usage
```ts
const logger = new Logger({ name: 'My Project', verbosity: Verbosity.INFO, logToFile: false });
const loggage = new Loggage({ name: 'My Project', verbosity: Verbosity.INFO, logToFile: false });

logger.warning('This is a warning message');
loggage.warning('This is a warning message');
```

# Output
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './formatting/index.js';
export * from './logging/index.js';
export * from './loggage/index.js';
export * from './typings/index.js';
2 changes: 2 additions & 0 deletions src/loggage/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './loggage.js';
export * from './tags.js'
61 changes: 44 additions & 17 deletions src/logging/logger.ts → src/loggage/loggage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,52 @@ import { Console } from 'node:console';
import { createWriteStream } from 'node:fs';
import dayjs from 'dayjs';

import { C, S } from '../formatting/index.js';
import { LoggerOptions, Verbosity } from '../typings/logging.js';
import { TAGS } from './tags.js';
import { B, C, S } from '../formatting/index.js';
import { LoggageOptions, Verbosity } from '../typings/loggage.js';

export class Logger {
export class Loggage {
private readonly console: Console;
private readonly file!: Console;
private readonly file: Console | undefined;
private save: boolean;

private name: string;
private verbosity: Verbosity;
private readonly logToFile: boolean;

public static TAGS: Readonly<Record<keyof typeof Verbosity, string>> = {
FATAL_ERROR: S.reset(B.red(' ERROR ')) + ' ' + S.reset(B.magenta(' FATAL ')) + this.pad(' ERROR FATAL '),
ERROR: S.reset(B.red(' ERROR ')) + this.pad(' ERROR '),
WARNING: S.reset(B.orange(' WARNING ')) + this.pad(' WARNING '),
INFO: S.reset(B.grey(' INFO ')) + this.pad(' INFO '),
DEBUG: S.reset(B.cyan(' DEBUG ')) + this.pad(' DEBUG '),
VERBOSE: S.reset(B.blue(' VERBOSE ')) + this.pad(' VERBOSE '),
};

/**
* Pad a word with spaces to the right.
* @param word The word to pad.
* @param length The length of the padding.
* @returns string
*/
private static pad(word: string, length = 18): string {
return ' '.repeat(Math.max(length - word.length, 1));
}

constructor ({
name,
verbosity = Verbosity.INFO,
logToFile = true,
}: LoggerOptions) {
save = true,
}: LoggageOptions) {
this.name = name;
this.verbosity = verbosity;
this.logToFile = logToFile;
this.save = save;

const timestamp = dayjs(new Date()).format('YYYY-MM-DD HH-mm-ss');

this.console = new Console({
stdout: process.stdout,
});

if (logToFile) {
if (save) {
this.file = new Console({
stdout: createWriteStream(`./logs/${name} ${timestamp}.log`),
});
Expand All @@ -43,6 +62,14 @@ export class Logger {
this.verbosity = verbosity;
}

public getSaveToFile(): boolean {
return this.save;
}

public setSaveToFile(logToFile: boolean): void {
this.save = logToFile;
}

public getName(): string {
return this.name;
}
Expand All @@ -53,37 +80,37 @@ export class Logger {

public fatal_error(message: unknown): void {
if (this.verbosity >= Verbosity.FATAL_ERROR) {
this.log(TAGS.FATAL_ERROR, message);
this.log(Loggage.TAGS.FATAL_ERROR, message);
}
}

public error(message: unknown): void {
if (this.verbosity >= Verbosity.ERROR) {
this.log(TAGS.ERROR, message);
this.log(Loggage.TAGS.ERROR, message);
}
}

public warning(message: unknown): void {
if (this.verbosity >= Verbosity.WARNING) {
this.log(TAGS.WARNING, message);
this.log(Loggage.TAGS.WARNING, message);
}
}

public info(message: unknown): void {
if (this.verbosity >= Verbosity.INFO) {
this.log(TAGS.INFO, message);
this.log(Loggage.TAGS.INFO, message);
}
}

public debug(message: unknown): void {
if (this.verbosity >= Verbosity.DEBUG) {
this.log(TAGS.DEBUG, message);
this.log(Loggage.TAGS.DEBUG, message);
}
}

public verbose(message: unknown): void {
if (this.verbosity >= Verbosity.VERBOSE) {
this.log(TAGS.VERBOSE, message);
this.log(Loggage.TAGS.VERBOSE, message);
}
}

Expand All @@ -92,7 +119,7 @@ export class Logger {

this.console.log(log, message);

if (this.logToFile) {
if (this.save && this.file) {
this.file.log(log, message);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/loggage/tags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { B } from '../formatting/background.js';
import { S } from '../formatting/style.js';
import { Verbosity } from '../typings/loggage.js';

2 changes: 0 additions & 2 deletions src/logging/index.ts

This file was deleted.

22 changes: 0 additions & 22 deletions src/logging/tags.ts

This file was deleted.

66 changes: 66 additions & 0 deletions src/tests/loggage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { describe, it } from 'node:test';
import { Loggage, Verbosity } from '../index.js';
import { strict as assert } from 'node:assert';

describe('Test Loggage', () => {
it('should set the verbosity level correctly', () => {
const loggage = new Loggage({ name: 'My Project', verbosity: Verbosity.INFO, save: false });
assert.equal(loggage.getVerbosity(), Verbosity.INFO);

loggage.setVerbosity(Verbosity.DEBUG);
assert.equal(loggage.getVerbosity(), Verbosity.DEBUG);
});

it('should set the name correctly', () => {
const loggage = new Loggage({ name: 'My Project', verbosity: Verbosity.INFO, save: false });
assert.equal(loggage.getName(), 'My Project');

loggage.setName('newName');
assert.equal(loggage.getName(), 'newName');
});

it('should set the log to file correctly', () => {
const loggage = new Loggage({ name: 'My Project', verbosity: Verbosity.INFO, save: false });
assert.equal(loggage.getSaveToFile(), false);

loggage.setSaveToFile(true);
assert.equal(loggage.getSaveToFile(), true);
});


it('should send the correct error message', () => {
const loggage = new Loggage({ name: 'My Project', verbosity: Verbosity.INFO, save: false });

loggage.error('This is an error message');
});

it('should send the correct fatal error message', () => {
const loggage = new Loggage({ name: 'My Project', verbosity: Verbosity.FATAL_ERROR, save: false });

loggage.fatal_error('This is an error message');
});

it('should send the correct warning message', () => {
const loggage = new Loggage({ name: 'My Project', verbosity: Verbosity.INFO, save: false });

loggage.warning('This is a warning message');
});

it('should send the correct info message', () => {
const loggage = new Loggage({ name: 'My Project', verbosity: Verbosity.INFO, save: false });

loggage.info('This is an info message');
});

it('should send the correct debug message', () => {
const loggage = new Loggage({ name: 'My Project', verbosity: Verbosity.DEBUG, save: false });

loggage.debug('This is a debug message');
});

it('should send the correct verbose message', () => {
const loggage = new Loggage({ name: 'My Project', verbosity: Verbosity.VERBOSE, save: false });

loggage.verbose('This is a verbose message');
});
});
58 changes: 0 additions & 58 deletions src/tests/logger.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/typings/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './logging.js';
export * from './loggage.js';
4 changes: 2 additions & 2 deletions src/typings/logging.ts → src/typings/loggage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export enum Verbosity {
VERBOSE,
}

export interface LoggerOptions {
export interface LoggageOptions {
name: string,
verbosity?: Verbosity,
logToFile?: boolean,
save?: boolean,
}

0 comments on commit 56b3b68

Please sign in to comment.