@stgdp/fancy-logger
Advanced tools
+31
| # Changelog | ||
| ## [1.0.0-beta.1] - 2020-09-10 | ||
| ### Added | ||
| - Implemented basic mocha tests | ||
| - Timestamps can now be disabled | ||
| - Outputs can be buffered for use at a later point | ||
| - Added a Readme detailing how to use the logger | ||
| - Added this here changelog! | ||
| ### Changed | ||
| - Converted the logger to a class instead of a function with prototypes | ||
| - Implemented more checks to the `modifier`, `reset` and `output` methods | ||
| - Moved functionality out to libs for simplicity of updating | ||
| ### Fixed | ||
| - Fixed issues with the bright colors on the `fg` and `bg` methods | ||
| ## [1.0.0-beta.0] - 2020-09-10 | ||
| ### Added | ||
| - Initial (beta) version | ||
| [unreleased]: https://github.com/stgdp/fancy-logger/compare/v1.0.0-beta.1...HEAD | ||
| [1.0.0-beta.1]: https://github.com/stgdp/fancy-logger/releases/tag/v1.0.0-beta.1 | ||
| [1.0.0-beta.0]: https://github.com/stgdp/fancy-logger/releases/tag/v1.0.0-beta.0 |
| const ansi_codes = require( "@stgdp/ansi-codes" ) | ||
| function _set_prototype( name, code ) { | ||
| this[name] = function () { | ||
| this.write( code ) | ||
| return this | ||
| } | ||
| } | ||
| function _set_fg() { | ||
| var that = this | ||
| Object.keys( ansi_codes.fg ).forEach( function ( color ) { | ||
| if ( color !== "bright" ) { | ||
| let name = `fg_${color}` | ||
| that._set_prototype( name, ansi_codes.fg[color] ) | ||
| that[color] = function () { | ||
| return that[name]() | ||
| } | ||
| } else { | ||
| Object.keys( ansi_codes.fg.bright ).forEach( function ( color ) { | ||
| let name = `fg_bright_${color}` | ||
| that._set_prototype( name, ansi_codes.fg.bright[color] ) | ||
| that[`bright_${color}`] = function () { | ||
| return that[name]() | ||
| } | ||
| } ) | ||
| } | ||
| } ) | ||
| } | ||
| function _set_bg() { | ||
| var that = this | ||
| Object.keys( ansi_codes.bg ).forEach( function ( color ) { | ||
| if ( color !== "bright" ) { | ||
| that._set_prototype( `bg_${color}`, ansi_codes.bg[color] ) | ||
| } else { | ||
| Object.keys( ansi_codes.bg.bright ).forEach( function ( color ) { | ||
| that._set_prototype( `bg_bright_${color}`, ansi_codes.bg.bright[color] ) | ||
| } ) | ||
| } | ||
| } ) | ||
| } | ||
| function _set_mods() { | ||
| var that = this | ||
| Object.keys( ansi_codes.modifier ).forEach( function ( mod ) { | ||
| let name = `mod_${mod}` | ||
| that._set_prototype( name, ansi_codes.modifier[mod] ) | ||
| that[mod] = function () { | ||
| return that[name]() | ||
| } | ||
| } ) | ||
| } | ||
| function _set_reset() { | ||
| var that = this | ||
| Object.keys( ansi_codes.reset ).forEach( function ( reset ) { | ||
| that._set_prototype( `reset_${reset}`, ansi_codes.reset[reset] ) | ||
| } ) | ||
| } | ||
| module.exports = exports = { | ||
| _set_prototype, | ||
| _set_fg, | ||
| _set_bg, | ||
| _set_mods, | ||
| _set_reset | ||
| } |
+140
| const ansi_codes = require( "@stgdp/ansi-codes" ) | ||
| function fg( color ) { | ||
| let name = `fg_${color}` | ||
| if ( typeof this[name] !== "function" ) { | ||
| return this | ||
| } | ||
| this[name]() | ||
| return this | ||
| } | ||
| function bg( color ) { | ||
| let name = `bg_${color}` | ||
| if ( typeof this[name] !== "function" ) { | ||
| return this | ||
| } | ||
| this[name]() | ||
| return this | ||
| } | ||
| function modifier( options ) { | ||
| if ( typeof options === "string" ) { | ||
| if ( !( options in ansi_codes.modifier ) ) { | ||
| options = {} | ||
| } else { | ||
| this[`mod_${options}`]() | ||
| return this | ||
| } | ||
| } | ||
| if ( options == null || typeof options !== "object" || Object.keys( options ).length === 0 ) { | ||
| return this | ||
| } | ||
| let default_options = { | ||
| bold: false, | ||
| dim: false, | ||
| italic: false, | ||
| underline: false, | ||
| inverse: false, | ||
| hidden: false, | ||
| strike: false, | ||
| frame: false, | ||
| encircle: false, | ||
| overline: false, | ||
| } | ||
| options = Object.assign( default_options, options ) | ||
| let _this = this | ||
| Object.keys( options ).forEach( function ( mod ) { | ||
| if ( !( mod in default_options ) ) { | ||
| return | ||
| } | ||
| if ( !!options[mod] ) { | ||
| _this[mod]() | ||
| } | ||
| } ) | ||
| return this | ||
| } | ||
| function reset( options = {} ) { | ||
| if ( typeof options === "string" ) { | ||
| if ( !( options in ansi_codes.reset ) ) { | ||
| options = {} | ||
| } else { | ||
| this[`reset_${options}`]() | ||
| return this | ||
| } | ||
| } | ||
| if ( options == null || typeof options !== "object" ) { | ||
| options = {} | ||
| } | ||
| if ( Object.keys( options ).length === 0 ) { | ||
| this.write( ansi_codes.reset.all ) | ||
| return this | ||
| } | ||
| let default_options = { | ||
| all: false, | ||
| bold: false, | ||
| dim: false, | ||
| italic: false, | ||
| underline: false, | ||
| inverse: false, | ||
| hidden: false, | ||
| strike: false, | ||
| fg: false, | ||
| bg: false, | ||
| frame: false, | ||
| encircle: false, | ||
| overline: false, | ||
| } | ||
| options = Object.assign( default_options, options ) | ||
| let _this = this | ||
| Object.keys( options ).forEach( function ( option ) { | ||
| if ( !( option in default_options ) ) { | ||
| return | ||
| } | ||
| if ( !!options[option] ) { | ||
| _this[`reset_${option}`]() | ||
| } | ||
| } ) | ||
| return this | ||
| } | ||
| module.exports = exports = { | ||
| fg, | ||
| bg, | ||
| modifier, | ||
| reset | ||
| } |
+66
| const ansi_codes = require( "@stgdp/ansi-codes" ) | ||
| function write( data ) { | ||
| if ( this._options.buffer ) { | ||
| this._output += data | ||
| } else { | ||
| this.stdout.write( data ) | ||
| } | ||
| return this | ||
| } | ||
| function end() { | ||
| this.write( ansi_codes.reset.all ) | ||
| this.write( "\n" ) | ||
| return this | ||
| } | ||
| function output( end = false ) { | ||
| if ( typeof end !== "boolean" ) { | ||
| end = false | ||
| } | ||
| if ( end ) { | ||
| this.end() | ||
| } | ||
| if ( this._options.buffer ) { | ||
| this.stdout.write( this._output ) | ||
| } | ||
| return this | ||
| } | ||
| function buffer_return() { | ||
| if ( this._options.buffer ) { | ||
| this.end() | ||
| } | ||
| return this._output | ||
| } | ||
| function write_timestamp() { | ||
| var time = new Date() | ||
| this.write( ansi_codes.reset.all ) | ||
| this.write( "[" ) | ||
| this.write( ansi_codes.fg.bright.black ) | ||
| this.write( ( "0" + time.getHours() ).slice( -2 ) + ":" ) | ||
| this.write( ( "0" + time.getMinutes() ).slice( -2 ) + ":" ) | ||
| this.write( ( "0" + time.getSeconds() ).slice( -2 ) ) | ||
| this.write( ansi_codes.reset.fg ) | ||
| this.write( "] - " ) | ||
| } | ||
| module.exports = exports = { | ||
| write, | ||
| end, | ||
| output, | ||
| return: buffer_return, | ||
| write_timestamp | ||
| } |
+33
-189
@@ -1,201 +0,45 @@ | ||
| const ansi_codes = require( "@stgdp/ansi-codes" ) | ||
| const write = require( "./lib/write" ) | ||
| const modifiers = require( "./lib/modifiers" ) | ||
| const helpers = require( "./lib/helpers" ) | ||
| function Logger( options ) { | ||
| class Logger { | ||
| if ( !( this instanceof Logger ) ) { | ||
| return new Logger( options ) | ||
| } | ||
| constructor ( options ) { | ||
| this.stdout = process.stdout | ||
| this._options = Object.assign( { | ||
| timestamp: true, | ||
| buffer: false, | ||
| }, options ) | ||
| this._output = "" | ||
| this.stdout = process.stdout | ||
| this.options = options | ||
| this._set_fg() | ||
| this._set_bg() | ||
| this._set_mods() | ||
| this._set_reset() | ||
| this.write_timestamp() | ||
| } | ||
| module.exports = exports = Logger | ||
| Logger.prototype.write_timestamp = function() { | ||
| var time = new Date() | ||
| this.write( ansi_codes.reset.all ) | ||
| this.write( "[" ) | ||
| this.write( ansi_codes.fg.bright.black ) | ||
| this.write( ( "0" + time.getHours() ).slice( -2 ) + ":" ) | ||
| this.write( ( "0" + time.getMinutes() ).slice( -2 ) + ":" ) | ||
| this.write( ( "0" + time.getSeconds() ).slice( -2 ) ) | ||
| this.write( ansi_codes.reset.fg ) | ||
| this.write( "] - " ) | ||
| } | ||
| Logger.prototype.write = function( data ) { | ||
| this.stdout.write.apply( this.stdout, arguments ) | ||
| return this | ||
| } | ||
| Logger.prototype.end = function() { | ||
| this.write( ansi_codes.reset.all ) | ||
| this.write( "\n" ) | ||
| } | ||
| function set_prototype( name, code ) { | ||
| Logger.prototype[name] = function() { | ||
| this.write( code ) | ||
| return this | ||
| } | ||
| } | ||
| Logger.prototype.fg = function( color ) { | ||
| if ( color == "bright" || !( color in ansi_codes.fg ) ) { | ||
| return this | ||
| } | ||
| let name = `fg_${color}` | ||
| this[name]() | ||
| return this | ||
| } | ||
| Object.keys( ansi_codes.fg ).forEach( function( color ) { | ||
| if ( color !== "bright" ) { | ||
| let name = `fg_${color}` | ||
| set_prototype( name, ansi_codes.fg[color] ) | ||
| Logger.prototype[color] = function() { | ||
| return this[name]() | ||
| if ( this._options.timestamp ) { | ||
| this._write_timestamp() | ||
| } | ||
| } else { | ||
| Object.keys( ansi_codes.fg.bright ).forEach( function( color ) { | ||
| let name = `fg_bright_${color}` | ||
| set_prototype( name, ansi_codes.fg.bright[color] ) | ||
| Logger.prototype[`bright_${color}`] = function() { | ||
| return this[name]() | ||
| } | ||
| } ) | ||
| } | ||
| } ) | ||
| write = write.write | ||
| end = write.end | ||
| output = write.output | ||
| return = write.return | ||
| _write_timestamp = write.write_timestamp | ||
| Logger.prototype.bg = function( color ) { | ||
| fg = modifiers.fg | ||
| bg = modifiers.bg | ||
| modifier = modifiers.modifier | ||
| reset = modifiers.reset | ||
| if ( color == "bright" || !( color in ansi_codes.bg ) ) { | ||
| return this | ||
| } | ||
| let name = `bg_${color}` | ||
| this[name]() | ||
| return this | ||
| _set_prototype = helpers._set_prototype | ||
| _set_fg = helpers._set_fg | ||
| _set_bg = helpers._set_bg | ||
| _set_mods = helpers._set_mods | ||
| _set_reset = helpers._set_reset | ||
| } | ||
| Object.keys( ansi_codes.bg ).forEach( function( color ) { | ||
| if ( color !== "bright" ) { | ||
| set_prototype( `bg_${color}`, ansi_codes.bg[color] ) | ||
| } else { | ||
| Object.keys( ansi_codes.bg.bright ).forEach( function( color ) { | ||
| set_prototype( `bg_bright_${color}`, ansi_codes.bg.bright[color] ) | ||
| } ) | ||
| } | ||
| } ) | ||
| Logger.prototype.modifier = function( options ) { | ||
| let default_options = { | ||
| bold: false, | ||
| dim: false, | ||
| italic: false, | ||
| underline: false, | ||
| inverse: false, | ||
| hidden: false, | ||
| strike: false, | ||
| frame: false, | ||
| encircle: false, | ||
| overline: false, | ||
| } | ||
| options = Object.assign( default_options, options ) | ||
| let _this = this | ||
| Object.keys( options ).forEach( function( mod ) { | ||
| if ( !( mod in default_options ) ) { | ||
| return | ||
| } | ||
| if ( !!options[mod] ) { | ||
| _this[mod]() | ||
| } | ||
| } ) | ||
| return this | ||
| module.exports = exports = function ( options ) { | ||
| return new Logger( options ) | ||
| } | ||
| Object.keys( ansi_codes.modifier ).forEach( function( mod ) { | ||
| let name = `mod_${mod}` | ||
| set_prototype( name, ansi_codes.modifier[mod] ) | ||
| Logger.prototype[mod] = function() { | ||
| return this[name]() | ||
| } | ||
| } ) | ||
| Logger.prototype.reset = function( options = {} ) { | ||
| if ( typeof options === "string" ) { | ||
| if ( !( options in ansi_codes.reset ) ) { | ||
| options = {} | ||
| } else { | ||
| this[`reset_${options}`]() | ||
| return this | ||
| } | ||
| } | ||
| if ( Object.keys( options ).length === 0 ) { | ||
| this.write( ansi_codes.reset.all ) | ||
| return this | ||
| } | ||
| let default_options = { | ||
| all: false, | ||
| bold: false, | ||
| dim: false, | ||
| italic: false, | ||
| underline: false, | ||
| inverse: false, | ||
| hidden: false, | ||
| strike: false, | ||
| fg: false, | ||
| bg: false, | ||
| frame: false, | ||
| encircle: false, | ||
| overline: false, | ||
| } | ||
| options = Object.assign( default_options, options ) | ||
| let _this = this | ||
| Object.keys( options ).forEach( function( option ) { | ||
| if ( !( option in default_options ) ) { | ||
| return | ||
| } | ||
| if ( !!options[option] ) { | ||
| _this[`reset_${option}`]() | ||
| } | ||
| } ) | ||
| return this | ||
| } | ||
| Object.keys( ansi_codes.reset ).forEach( function( reset ) { | ||
| set_prototype( `reset_${reset}`, ansi_codes.reset[reset] ) | ||
| } ) |
+2
-1
| { | ||
| "name": "@stgdp/fancy-logger", | ||
| "version": "1.0.0-beta.0", | ||
| "version": "1.0.0-beta.1", | ||
| "description": "", | ||
@@ -17,2 +17,3 @@ "license": "MIT", | ||
| "scripts": { | ||
| "publish": "npm publish --access public --scope=@stgdp --tag beta", | ||
| "test": "mocha" | ||
@@ -19,0 +20,0 @@ }, |
+152
-14
| # @stgdp/fancy-logger | ||
| Bring some color to your console outputs! | ||
| [](https://www.npmjs.com/package/@stgdp/fancy-logger) | ||
@@ -8,10 +10,6 @@ [](https://travis-ci.org/stgdp/fancy-logger) | ||
| ## README Coming Soon | ||
| `fancy-logger` is a Node.js module that supplies you with a fully-featured console logger to format your terminal. Change the text color, background color, make the text bold, italic, underline and more. | ||
| <!-- > TODO Opening text --> | ||
| ## Installation | ||
| <!-- TODO description --> | ||
| <!-- ## Installation | ||
| ### With npm | ||
@@ -27,20 +25,160 @@ | ||
| yarn add @stgdp/fancy-logger | ||
| ``` --> | ||
| ``` | ||
| <!-- ## Usage | ||
| ## Usage | ||
| ```javascript | ||
| TODO example here | ||
| const logger = require("@stgdp/fancy-logger") | ||
| // Produces a bold console log with red background and white text | ||
| logger().white().bold().bg_red().write("I'm formatted!").end() | ||
| ``` | ||
| **You can also include this into your project using ES6:** | ||
| ## Reference | ||
| ### logger( ?options: Object ) | ||
| Starts the logger with a timestamp by default. Options can be supplied to the logger in an object. | ||
| | Options | Default | Operation | | ||
| | ----------- | ------- | -------------------------------------------------------------- | | ||
| | `timestamp` | `true` | Enables the timestamp | | ||
| | `buffer` | `false` | Buffers the output to be returned or outputted at a later time | | ||
| #### Usage | ||
| ```javascript | ||
| TODO example here | ||
| ``` --> | ||
| // Without options | ||
| logger() | ||
| <!-- ## Reference | ||
| // With options | ||
| logger({ | ||
| timestamp: true, | ||
| buffer: false, | ||
| }) | ||
| ``` | ||
| TODO fill in reference --> | ||
| ### fg( color: String ) | ||
| The `fg` method allows a foreground color to be applied to the console log. All options listed below are also available as standalone methods (e.g. `red()`) and as methods prefixed with `fg_` (e.g. `fg_red()`) | ||
| - `black` | ||
| - `red` | ||
| - `green` | ||
| - `yellow` | ||
| - `blue` | ||
| - `magenta` | ||
| - `cyan` | ||
| - `white` | ||
| - `bright_black` | ||
| - `bright_red` | ||
| - `bright_green` | ||
| - `bright_yellow` | ||
| - `bright_blue` | ||
| - `bright_magenta` | ||
| - `bright_cyan` | ||
| - `bright_white` | ||
| #### Usage | ||
| ```javascript | ||
| logger().fg("red") | ||
| // Or | ||
| logger().red() | ||
| // Or | ||
| logger().fg_red() | ||
| ``` | ||
| ### bg( color: String ) | ||
| The `bg` method allows a background color to be applied to the console log. All options listed below are available as methods prefixed with `bg_` (e.g. `bg_red()`) | ||
| - `black` | ||
| - `red` | ||
| - `green` | ||
| - `yellow` | ||
| - `blue` | ||
| - `magenta` | ||
| - `cyan` | ||
| - `white` | ||
| - `bright_black` | ||
| - `bright_red` | ||
| - `bright_green` | ||
| - `bright_yellow` | ||
| - `bright_blue` | ||
| - `bright_magenta` | ||
| - `bright_cyan` | ||
| - `bright_white` | ||
| #### Usage | ||
| ```javascript | ||
| logger().bg("red") | ||
| // Or | ||
| logger().bg_red() | ||
| ``` | ||
| ### modifier( ?options: Object|String ) | ||
| The `modifier` method allows a modifier to be applied to the console log. All options listed below can be supplied as an object (e.g. `{ bold: true }`) or as a string (e.g. `"bold"`). All options are also available as standalone methods (e.g. `bold()`) and as methods prefixed with `mod_` (e.g. `mod_bold()`) | ||
| - `bold` | ||
| - `dim` | ||
| - `italic` | ||
| - `underline` | ||
| - `inverse` | ||
| - `hidden` | ||
| - `strike` | ||
| - `frame` | ||
| - `encircle` | ||
| - `overline` | ||
| #### Usage | ||
| ```javascript | ||
| logger().modifier({ bold: true}) | ||
| // Or | ||
| logger().modifier("bold") | ||
| // Or | ||
| logger().bold() | ||
| // Or | ||
| logger().mod_bold() | ||
| ``` | ||
| ### reset( ?options: Object|String ) | ||
| The `reset` method allows you to reset any of the modifiers applied to the console log. All options listed below can be supplied as an object (e.g. `{ bold: true }`) or as a string (e.g. `"bold"`). All options are also available as methods prefixed with `reset_` (e.g. `mod_bold()`) | ||
| - `all` | ||
| - `bold` | ||
| - `dim` | ||
| - `italic` | ||
| - `underline` | ||
| - `inverse` | ||
| - `hidden` | ||
| - `strike` | ||
| - `fg` | ||
| - `bg` | ||
| - `frame` | ||
| - `encircle` | ||
| - `overline` | ||
| #### Usage | ||
| ```javascript | ||
| logger().reset({ bold: true}) | ||
| // Or | ||
| logger().reset("bold") | ||
| // Or | ||
| logger().reset_bold() | ||
| ``` | ||
| ## License | ||
@@ -47,0 +185,0 @@ |
-101
| const ansi_codes = require( "@stgdp/ansi-codes" ) | ||
| const timestamp = require( "fecha" ).format | ||
| function Logger( options ) { | ||
| if ( !( this instanceof Logger ) ) { | ||
| return new Logger( options ) | ||
| } | ||
| this.stdout = process.stdout | ||
| this._options = Object.assign( { | ||
| timestamp: true, | ||
| format: "HH:mm:ss", | ||
| buffer: false, | ||
| }, options ) | ||
| this._output = "" | ||
| if ( this._options.timestamp ) { | ||
| write_timestamp.call( this ) | ||
| } | ||
| this.fg = modifiers.call( this, "fg" ) | ||
| this.bg = modifiers.call( this, "bg" ) | ||
| this.modifiers = modifiers.call( this, "modifier" ) | ||
| this.reset = modifiers.call( this, "reset" ) | ||
| } | ||
| module.exports = exports = Logger | ||
| Logger.prototype.write = function ( data ) { | ||
| if ( this._options.buffer ) { | ||
| this._output += data | ||
| } else { | ||
| this.stdout.write( data ) | ||
| } | ||
| return this | ||
| } | ||
| Logger.prototype.end = function () { | ||
| this.write( ansi_codes.reset.all ) | ||
| this.write( "\n" ) | ||
| return this | ||
| } | ||
| Logger.prototype.output = function () { | ||
| this.end() | ||
| if ( this._options.buffer ) { | ||
| this.stdout.write( this._output ) | ||
| } | ||
| return this | ||
| } | ||
| Logger.prototype.return = function () { | ||
| if ( this._options.buffer ) { | ||
| this.end() | ||
| } | ||
| return this._output | ||
| } | ||
| function write_timestamp() { | ||
| this.write( ansi_codes.reset.all ) | ||
| this.write( "[" ) | ||
| this.write( ansi_codes.fg.bright.black ) | ||
| this.write( timestamp( new Date(), this._options.format ) ) | ||
| this.write( ansi_codes.reset.fg ) | ||
| this.write( "] - " ) | ||
| } | ||
| function modifiers( area ) { | ||
| var that = this, | ||
| mods = {} | ||
| Object.keys( ansi_codes[area] ).forEach( function ( code ) { | ||
| if ( code !== "bright" ) { | ||
| mods[code] = function () { | ||
| that.write( ansi_codes[area][code] ) | ||
| return that | ||
| } | ||
| } else { | ||
| mods.bright = {} | ||
| Object.keys( ansi_codes[area].bright ).forEach( function ( bright_color ) { | ||
| mods.bright[bright_color] = function () { | ||
| that.write( ansi_codes[area].bright[bright_color] ) | ||
| return that | ||
| } | ||
| } ) | ||
| } | ||
| } ) | ||
| return mods | ||
| } |
-32
| const logger = require( "./v2.js" ) | ||
| var test = logger( { | ||
| timestamp: true, | ||
| format: "YYYY-MM-DD HH:mm:ss", | ||
| buffer: true | ||
| } ) | ||
| .modifiers.bold() | ||
| .bg.white() | ||
| .fg.red() | ||
| .write( "hello" ) | ||
| .reset.bold() | ||
| .bg.yellow() | ||
| .fg.bright.red() | ||
| .write( " there!" ) | ||
| .return() | ||
| process.stdout.write( test ) | ||
| logger( { | ||
| timestamp: true, | ||
| format: "YYYY-MM-DD HH:mm:ss" | ||
| } ) | ||
| .modifiers.bold() | ||
| .bg.white() | ||
| .fg.red() | ||
| .write( "hello" ) | ||
| .reset.bold() | ||
| .bg.yellow() | ||
| .fg.bright.red() | ||
| .write( " there!" ) | ||
| .output() |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
15023
31.86%8
33.33%207
200%249
-3.49%1
Infinity%