diff --git a/README.md b/README.md index 73404c3..418bc23 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,8 @@ and improve some features Basic logging out: ```ts -import { Dlog } from 'https://deno.land/x/dlog2@1.1.1/mod.ts'; +import { Dlog } from 'https://deno.land/x/dlog2@1.1.2/classic.ts'; // Classic version with the classic out! +import { Dlog } from 'https://deno.land/x/dlog2@1.1.2/mini.ts'; // Mini version the same features of classic but with a minimalist out! const logger = new Dlog('Test Dlog'); @@ -28,7 +29,8 @@ logger.debug('helloooo from debug'); File log support ```ts -import { Dlog } from 'https://deno.land/x/dlog2@1.1.1/mod.ts'; +import { Dlog } from 'https://deno.land/x/dlog2@1.1.2/classic.ts'; // Classic version with the classic out! +import { Dlog } from 'https://deno.land/x/dlog2@1.1.2/mini.ts'; // Mini version the same features of classic but with a minimalist out! const logger = new Dlog('Test Dlog', true, './test'); diff --git a/mod.ts b/classic.ts similarity index 85% rename from mod.ts rename to classic.ts index 674ea28..0397b93 100644 --- a/mod.ts +++ b/classic.ts @@ -122,6 +122,31 @@ export default class Dlog { } } + /** + * @description Log with the done level + * @param {string} message - The message to log + */ + + done(message: string): void { + const noColorMsg = `[${ + format(new Date(), 'yyyy/MM/dd h:mm:ss a') + }] [${this._app}] [DONE] ${message}`; + console.log( + `${ + colors.brightGreen( + colors.bold( + '[' + format(new Date(), 'yyyy/MM/dd h:mm:ss a') + ']', + ), + ) + } ${colors.italic(colors.green('[' + this._app + ']'))} ${ + colors.brightGreen('[  DONE ]:') + } ${message}`, + ); + if (this._logToFile) { + this.writeToFile(noColorMsg); + } + } + private writeToFile(noColorMsg: string): void { ensureDirSync(this._logFolder); const appName = this._app.replace(' ', '-'); diff --git a/examples/classic.ts b/examples/classic.ts new file mode 100644 index 0000000..4a5ce72 --- /dev/null +++ b/examples/classic.ts @@ -0,0 +1,23 @@ +import Dlog from '../classic.ts'; + +// App without files +const logs = new Dlog('An Example App'); +// App with files +const logsFile = new Dlog('An Example App With Files', true); + +// Without Files examples + +logs.info('Info example'); +logs.debug('Debug Example'); +logs.warn('Warn Example'); +logs.error('Error Example'); +// WTF DONE? Yeah new feature :p +logs.done('Done Example'); + +// Wit Files +logsFile.info('Info example'); +logsFile.debug('Debug Example'); +logsFile.warn('Warn Example'); +logsFile.error('Error Example'); +// WTF DONE? Yeah new feature :p +logsFile.done('Done Example'); diff --git a/examples/debug.ts b/examples/debug.ts deleted file mode 100644 index c3e1f1d..0000000 --- a/examples/debug.ts +++ /dev/null @@ -1,5 +0,0 @@ -import Dlog from '../mod.ts'; - -const logger = new Dlog('helloo'); - -logger.debug('debug'); diff --git a/examples/error.ts b/examples/error.ts deleted file mode 100644 index 33bddb9..0000000 --- a/examples/error.ts +++ /dev/null @@ -1,5 +0,0 @@ -import Dlog from '../mod.ts'; - -const logger = new Dlog('error test'); - -logger.error('error'); diff --git a/examples/info.ts b/examples/info.ts deleted file mode 100644 index 9a1e453..0000000 --- a/examples/info.ts +++ /dev/null @@ -1,5 +0,0 @@ -import Dlog from '../mod.ts'; - -const logger = new Dlog('info test'); - -logger.info('info'); diff --git a/examples/mini.ts b/examples/mini.ts new file mode 100644 index 0000000..81cf7dd --- /dev/null +++ b/examples/mini.ts @@ -0,0 +1,28 @@ +import Dlog from '../mini.ts'; + +// What is this ? +// +// Well if you want a more minimalist out here are the feature you need! +// The sames features of the classic but with other style! + +// App without files +const logs = new Dlog('An Example App'); +// App with files +const logsFile = new Dlog('An Example App With Files', true); + +// Without Files examples + +logs.info('Info example'); +logs.debug('Debug Example'); +logs.warn('Warn Example'); +logs.error('Error Example'); +// WTF DONE? Yeah new feature :p +logs.done('Done Example'); + +// Wit Files +logsFile.info('Info example'); +logsFile.debug('Debug Example'); +logsFile.warn('Warn Example'); +logsFile.error('Error Example'); +// WTF DONE? Yeah new feature :p +logsFile.done('Done Example'); diff --git a/examples/warn.ts b/examples/warn.ts deleted file mode 100644 index 0f5bf99..0000000 --- a/examples/warn.ts +++ /dev/null @@ -1,5 +0,0 @@ -import Dlog from '../mod.ts'; - -const logger = new Dlog('warn test'); - -logger.warn('warn'); diff --git a/examples/writeFile.ts b/examples/writeFile.ts deleted file mode 100644 index 4e738d0..0000000 --- a/examples/writeFile.ts +++ /dev/null @@ -1,8 +0,0 @@ -import Dlog from '../mod.ts'; - -const logger = new Dlog('Test Glogger', true, './test'); - -logger.info('heloooooo'); -logger.warn('heloooooo'); -logger.error('heloooooo'); -logger.debug('helooo'); diff --git a/mini.ts b/mini.ts new file mode 100644 index 0000000..f37c5c2 --- /dev/null +++ b/mini.ts @@ -0,0 +1,129 @@ +/** + * Copyright © 2022 Dpm Land. All Rights Reserved. + */ + +import { colors, ensureDirSync, format, join } from './deps.ts'; + +/** + * @description The constructor for the dlog module + * @param {string} app_name - The application name for the logger output + * @param {boolean} logToFile - Write a log file DEFAULT: true + * @param {string} logFolder - The folder name to create and log + */ +export default class Dlog { + private _app = ''; + private _logToFile: boolean; + private _logFolder: string; + private _date: string; + + constructor( + app_name: string, + logToFile: boolean = false, + logFolder: string = 'logs', + ) { + this._app = app_name; + this._logToFile = logToFile; + this._logFolder = logFolder; + this._date = format(new Date(), 'yyyy|MM|dd'); + } + /** + * @description Log with the info level + * @param {string} message - The message to log + */ + info(message: string): void { + const noColorMsg = `[${ + format(new Date(), 'yyyy/MM/dd h:mm:ss a') + }] [${this._app}] [INFO] ${message}`; + console.log( + `${colors.italic(colors.green(`${this._app.toUpperCase()} ->`))} ${ + colors.blue('[  INFO ]:') + } ${message}`, + ); + if (this._logToFile) { + this.writeToFile(noColorMsg); + } + } + /** + * @description Log with the debug level + * @param {string} message - The message to log + */ + debug(message: string): void { + const noColorMsg = `[${ + format(new Date(), 'yyyy/MM/dd h:mm:ss a') + }] [${this._app}] [DEBUG] ${message}`; + console.log( + `${colors.italic(colors.green(`${this._app.toUpperCase()} ->`))} ${ + colors.magenta('[  DEBUG ]:') + } ${message}`, + ); + if (this._logToFile) { + this.writeToFile(noColorMsg); + } + } + + /** + * @description Log with the warn level + * @param {string} message - The message to log + */ + warn(message: string): void { + const noColorMsg = `[${ + format(new Date(), 'yyyy/MM/dd h:mm:ss a') + }] [${this._app}] [WARN] ${message}`; + console.log( + `${colors.italic(colors.green(`${this._app.toUpperCase()} ->`))} ${ + colors.brightYellow('[  WARN ]:') + } ${message}`, + ); + if (this._logToFile) { + this.writeToFile(noColorMsg); + } + } + + /** + * @description Log with the error level + * @param {string} message - The message to log + */ + + error(message: string): void { + const noColorMsg = `[${ + format(new Date(), 'yyyy/MM/dd h:mm:ss a') + }] [${this._app}] [ERROR] ${message}`; + console.log( + `${colors.italic(colors.green(`${this._app.toUpperCase()} ->`))} ${ + colors.brightRed('[ ✗ ERROR ]:') + } ${message}`, + ); + if (this._logToFile) { + this.writeToFile(noColorMsg); + } + } + + /** + * @description Log with the done level + * @param {string} message - The message to log + */ + + done(message: string): void { + const noColorMsg = `[${ + format(new Date(), 'yyyy/MM/dd h:mm:ss a') + }] [${this._app}] [DONE] ${message}`; + console.log( + `${colors.italic(colors.green(`${this._app.toUpperCase()} ->`))} ${ + colors.brightGreen('[  DONE ]:') + } ${message}`, + ); + if (this._logToFile) { + this.writeToFile(noColorMsg); + } + } + + private writeToFile(noColorMsg: string): void { + ensureDirSync(this._logFolder); + const appName = this._app.replace(' ', '-'); + const path = join(this._logFolder, `${appName}.log`); + Deno.writeTextFileSync(path, noColorMsg + '\n', { + append: true, + create: true, + }); + } +}