diff --git a/README.md b/README.md index ff0ded6..20ca50a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/index.ts b/src/index.ts index 8e3b670..4200f8d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,3 @@ export * from './formatting/index.js'; -export * from './logging/index.js'; +export * from './loggage/index.js'; export * from './typings/index.js'; \ No newline at end of file diff --git a/src/loggage/index.ts b/src/loggage/index.ts new file mode 100644 index 0000000..f1b50aa --- /dev/null +++ b/src/loggage/index.ts @@ -0,0 +1,2 @@ +export * from './loggage.js'; +export * from './tags.js' \ No newline at end of file diff --git a/src/logging/logger.ts b/src/loggage/loggage.ts similarity index 52% rename from src/logging/logger.ts rename to src/loggage/loggage.ts index 21eeffc..0067050 100644 --- a/src/logging/logger.ts +++ b/src/loggage/loggage.ts @@ -2,25 +2,44 @@ 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> = { + 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'); @@ -28,7 +47,7 @@ export class Logger { stdout: process.stdout, }); - if (logToFile) { + if (save) { this.file = new Console({ stdout: createWriteStream(`./logs/${name} ${timestamp}.log`), }); @@ -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; } @@ -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); } } @@ -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); } } diff --git a/src/loggage/tags.ts b/src/loggage/tags.ts new file mode 100644 index 0000000..aea87da --- /dev/null +++ b/src/loggage/tags.ts @@ -0,0 +1,4 @@ +import { B } from '../formatting/background.js'; +import { S } from '../formatting/style.js'; +import { Verbosity } from '../typings/loggage.js'; + diff --git a/src/logging/index.ts b/src/logging/index.ts deleted file mode 100644 index e6b00d0..0000000 --- a/src/logging/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './logger.js'; -export * from './tags.js' \ No newline at end of file diff --git a/src/logging/tags.ts b/src/logging/tags.ts deleted file mode 100644 index 7a7b427..0000000 --- a/src/logging/tags.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { B } from '../formatting/background.js'; -import { S } from '../formatting/style.js'; -import { Verbosity } from '../typings/logging.js'; - -export const TAGS: Readonly> = { - FATAL_ERROR: S.reset(B.red(' ERROR ')) + ' ' + S.reset(B.magenta(' FATAL ')) + pad(' ERROR FATAL '), - ERROR: S.reset(B.red(' ERROR ')) + pad(' ERROR '), - WARNING: S.reset(B.orange(' WARNING ')) + pad(' WARNING '), - INFO: S.reset(B.grey(' INFO ')) + pad(' INFO '), - DEBUG: S.reset(B.cyan(' DEBUG ')) + pad(' DEBUG '), - VERBOSE: S.reset(B.blue(' VERBOSE ')) + pad(' VERBOSE '), -}; - -/** - * Pad a word with spaces to the right. - * @param {string} word The word to pad. - * @param {number} length The length of the padding. - * @returns {string} string - */ -function pad(word: string, length = 18): string { - return ' '.repeat(Math.max(length - word.length, 1)); -} \ No newline at end of file diff --git a/src/tests/loggage.test.ts b/src/tests/loggage.test.ts new file mode 100644 index 0000000..44f0680 --- /dev/null +++ b/src/tests/loggage.test.ts @@ -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'); + }); +}); \ No newline at end of file diff --git a/src/tests/logger.test.ts b/src/tests/logger.test.ts deleted file mode 100644 index 29440b3..0000000 --- a/src/tests/logger.test.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { describe, it } from 'node:test'; -import { Logger, Verbosity } from '../index.js'; -import { strict as assert } from 'node:assert'; - -describe('Test Logger', () => { - describe('Test Logger', () => { - it('should set the verbosity level correctly', () => { - const logger = new Logger({ name: 'test', verbosity: Verbosity.INFO, logToFile: false }); - assert.equal(logger.getVerbosity(), Verbosity.INFO); - - logger.setVerbosity(Verbosity.DEBUG); - assert.equal(logger.getVerbosity(), Verbosity.DEBUG); - }); - - it('should set the name correctly', () => { - const logger = new Logger({ name: 'test', verbosity: Verbosity.INFO, logToFile: false }); - assert.equal(logger.getName(), 'test'); - - logger.setName('newName'); - assert.equal(logger.getName(), 'newName'); - }); - - it('should send the correct error message', () => { - const logger = new Logger({ name: 'test', verbosity: Verbosity.INFO, logToFile: false }); - logger.error('This is an error message'); - }); - - it('should send the correct fatal error message', () => { - const logger = new Logger({ name: 'test', verbosity: Verbosity.FATAL_ERROR, logToFile: false }); - logger.fatal_error('This is an error message'); - }); - - it('should send the correct warning message', () => { - const logger = new Logger({ name: 'test', verbosity: Verbosity.INFO, logToFile: false }); - logger.warning('This is a warning message'); - }); - - it('should send the correct info message', () => { - const logger = new Logger({ name: 'test', verbosity: Verbosity.INFO, logToFile: false }); - logger.info('This is an info message'); - }); - - it('should send the correct debug message', () => { - const logger = new Logger({ name: 'test', verbosity: Verbosity.INFO, logToFile: false }); - - logger.setVerbosity(Verbosity.DEBUG); - logger.debug('This is a debug message'); - }); - - it('should send the correct verbose message', () => { - const logger = new Logger({ name: 'test', verbosity: Verbosity.INFO, logToFile: false }); - logger.verbose('This is a verbose message'); - - logger.setVerbosity(Verbosity.VERBOSE); - logger.verbose('This is a verbose message'); - }); - }); -}); \ No newline at end of file diff --git a/src/typings/index.ts b/src/typings/index.ts index 1c1d408..b5e0d54 100644 --- a/src/typings/index.ts +++ b/src/typings/index.ts @@ -1 +1 @@ -export * from './logging.js'; \ No newline at end of file +export * from './loggage.js'; \ No newline at end of file diff --git a/src/typings/logging.ts b/src/typings/loggage.ts similarity index 70% rename from src/typings/logging.ts rename to src/typings/loggage.ts index 82dda73..744a73d 100644 --- a/src/typings/logging.ts +++ b/src/typings/loggage.ts @@ -7,8 +7,8 @@ export enum Verbosity { VERBOSE, } -export interface LoggerOptions { +export interface LoggageOptions { name: string, verbosity?: Verbosity, - logToFile?: boolean, + save?: boolean, }