🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@stgdp/fancy-logger

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stgdp/fancy-logger - npm Package Compare versions

Comparing version
1.0.0-beta.1
to
1.0.0-beta.2
+17
-1
CHANGELOG.md
# Changelog
## [Unreleased]
## [1.0.0-beta.2] - 2020-09-14
### Added
- Implemented formatting the timestamp
### Changed
- Converted all methods except for write to getters as they didn't need to be methods
- `modifier` is now called `decoration`
- Reverted back to the original single file structure as it's now more simplified!
### Removed
- Calling `return` no longer adds an end statement, `end` should be called before this
## [1.0.0-beta.1] - 2020-09-10

@@ -29,4 +44,5 @@

[unreleased]: https://github.com/stgdp/fancy-logger/compare/v1.0.0-beta.1...HEAD
[unreleased]: https://github.com/stgdp/fancy-logger/compare/v1.0.0-beta.2...HEAD
[1.0.0-beta.2]: https://github.com/stgdp/fancy-logger/releases/tag/v1.0.0-beta.2
[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
+123
-24

@@ -1,7 +0,5 @@

const write = require( "./lib/write" )
const modifiers = require( "./lib/modifiers" )
const helpers = require( "./lib/helpers" )
const ansi_codes = require( "@stgdp/ansi-codes" )
const timestamp = require( "fecha" ).format
class Logger {
constructor ( options ) {

@@ -11,36 +9,137 @@ this.stdout = process.stdout

timestamp: true,
format: "HH:mm:ss",
buffer: false,
}, options )
this._output = ""
this._modifier = ""
this._bright = false
this._set_fg()
this._set_bg()
this._set_mods()
this._set_reset()
if ( this._options.timestamp ) {
this._write_timestamp()
write_timestamp.call( this )
}
}
write = write.write
end = write.end
output = write.output
return = write.return
_write_timestamp = write.write_timestamp
write( data ) {
if ( this._options.buffer ) {
this._output += data
} else {
this.stdout.write( data )
}
fg = modifiers.fg
bg = modifiers.bg
modifier = modifiers.modifier
reset = modifiers.reset
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
get end() {
this.all.write( "\n" )
return this
}
get output() {
this.stdout.write( this._output )
return this
}
get return() {
return this._output
}
get bright() {
this._bright = !this._bright
return this
}
}
function write_timestamp() {
this
.all
.write( "[" )
.fg.bright.black
.write( timestamp( new Date(), this._options.format ) )
.all
.write( "] - " )
}
function define_get( name, callback ) {
if ( name === null || typeof name !== "string" ) {
return
}
if ( callback === null || typeof callback !== "function" ) {
return
}
Object.defineProperty( Logger.prototype, name, {
get: callback
} )
}
["fg", "bg", "modifier", "reset"].forEach( function ( area ) {
var name = area
if ( name == "modifier" ) {
name = "decoration"
}
define_get( name, function() {
this._modifier = area
return this
} )
} )
Object.keys( ansi_codes.fg ).forEach( function ( color ) {
if ( color == "bright" ) {
return
}
define_get( color, function() {
if ( this._modifier != "fg" && this._modifier != "bg" ) {
this._modifier = "fg"
}
if ( this._bright ) {
this.write( ansi_codes[this._modifier].bright[color] )
} else {
this.write( ansi_codes[this._modifier][color] )
}
return this
} )
} )
Object.keys( ansi_codes.reset ).forEach( function ( decoration ) {
if ( decoration == "fg" || decoration == "bg" || decoration == "all" ) {
var name = decoration
if ( decoration != "all" ) {
name = `reset_${decoration}`
}
define_get( name, function() {
this._modifier = "reset"
this.write( ansi_codes[this._modifier][decoration] )
this._modifier = ""
this._bright = false
return this
} )
return
}
define_get( decoration, function() {
if ( this._modifier != "modifier" && this._modifier != "reset" ) {
this._modifier = "modifier"
}
this.write( ansi_codes[this._modifier][decoration] )
if ( this._modifier == "reset" ) {
this._modifier = ""
this._bright = false
}
return this
} )
} )
module.exports = exports = function ( options ) {
return new Logger( options )
}
+3
-2
{
"name": "@stgdp/fancy-logger",
"version": "1.0.0-beta.1",
"version": "1.0.0-beta.2",
"description": "",

@@ -22,3 +22,4 @@ "license": "MIT",

"dependencies": {
"@stgdp/ansi-codes": "1.0.0"
"@stgdp/ansi-codes": "1.0.0",
"fecha": "^4.2.0"
},

@@ -25,0 +26,0 @@ "devDependencies": {

+106
-86

@@ -31,4 +31,4 @@ # @stgdp/fancy-logger

// Produces a bold console log with red background and white text
logger().white().bold().bg_red().write("I'm formatted!").end()
// Produces a bold console log with a white background and red text
logger().red.bold.bg.white.write("I'm formatted!").end
```

@@ -42,6 +42,7 @@

| Options | Default | Operation |
| ----------- | ------- | -------------------------------------------------------------- |
| `timestamp` | `true` | Enables the timestamp |
| `buffer` | `false` | Buffers the output to be returned or outputted at a later time |
| Options | Default | Operation |
| ----------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `timestamp` | `true` | Enables the timestamp |
| `format` | `HH:mm:ss` | Sets the format for the timestamp. Check out the [fecha documentation](https://github.com/taylorhakes/fecha#formatting-tokens) for the formatting characters. |
| `buffer` | `false` | Buffers the output to be returned or outputted at a later time |

@@ -57,2 +58,3 @@ #### Usage

timestamp: true,
format: "HH:mm:ss",
buffer: false,

@@ -62,39 +64,64 @@ })

### fg( color: String )
### write( content: 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()`)
Outputs provided content to the console through the logger. When the `buffer` option is set to `true`, this will be stored within the logger until either `output` or `return` are called instead.
- `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
#### Usage
```javascript
// Outputs "hello world" to the console with a timestamp and new line.
logger().write("hello world").end
```
### end
Resets all styles and outputs a new line to the console through the logger.
### Usage
```javascript
logger().fg("red")
// Outputs "hello world" to the console with a timestamp and new line.
logger().write("hello world").end
```
// Or
logger().red()
### output
// Or
logger().fg_red()
Outputs the buffer to the console through the logger.
### Usage
```javascript
// Outputs "hello world" to the console with a timestamp and new line.
logger({ buffer: true }).write("hello world").end.output
```
### bg( color: String )
### return
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()`)
Returns the buffer created by the logger, along with the ansi codes for the modifiers used.
### Usage
```javascript
// Returns "hello world"
var msg = logger({ buffer: true }).write("hello world").end.return
```
### Modifier types
Modifier types adjust a parameter within the logger system, allowing the console to have it's foreground, background or decoration changed. The following modifier types are available;
| Modifier type | Description |
| ------------- | --------------------------------------------------------------------------------------------------------------------- |
| `fg` | Allows for setting a foreground color modifier (e.g. `red`) |
| `bg` | Allows for setting a background color modifier (e.g. `red`) |
| `decoration` | Allows for setting a decoration modifier (e.g. `bold`) |
| `reset` | Allows for issuing a reset modifier (e.g. `all`) |
| `bright` | Alters the foreground and background modifiers to use the bright variant. This modifier type works on a toggle system |
### fg and bg
Foreground and background modifiers allow the logger to change their foreground and background colors respectively. While foreground modifiers only need to be preceeded by the `fg` modifier type if another modifier type was previously used, background modifiers always need to be preceeded by the `bg` modifier type. Bright variants can be accessed by using the `bright` modifier type.
#### Available modifiers
- `black`

@@ -108,10 +135,2 @@ - `red`

- `white`
- `bright_black`
- `bright_red`
- `bright_green`
- `bright_yellow`
- `bright_blue`
- `bright_magenta`
- `bright_cyan`
- `bright_white`

@@ -121,66 +140,67 @@ #### Usage

```javascript
logger().bg("red")
// Sets the logger foreground color to red
logger().red.write("hello world").end
logger().fg.red.write("hello world").end
// Or
logger().bg_red()
// Sets the logger background color to red
logger().bg.red.write("hello world").end
// Sets the logger background color to bright red
logger().bg.bright.red.write("hello world").end
```
### modifier( ?options: Object|String )
### decoration
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()`)
Decoration modifiers allow the logger to change their decorations. These modifiers only have to be preceeded by the `decoration` modifier type is another modifier type was previously used.
- `bold`
- `dim`
- `italic`
- `underline`
- `inverse`
- `hidden`
- `strike`
- `frame`
- `encircle`
- `overline`
#### Available modifiers
- `bold`
- `dim`
- `italic`
- `underline`
- `inverse`
- `hidden`
- `strike`
- `frame`
- `encircle`
- `overline`
#### Usage
```javascript
logger().modifier({ bold: true})
// Sets the logger decoration to bold
logger().bold.write("hello world").end
logger().decoration.bold.write("hello world").end
```
// Or
logger().modifier("bold")
### reset
// Or
logger().bold()
Reset modifiers allow the logger to reset previously applied modifiers back to their default values. All modifiers except for `all`, `reset_fg` and `reset_bg` have to be preceeded by the `reset` modifier type.
// Or
logger().mod_bold()
```
#### Available modifiers
### reset( ?options: Object|String )
- `all`
- `bold`
- `dim`
- `italic`
- `underline`
- `inverse`
- `hidden`
- `strike`
- `reset_fg`
- `reset_bg`
- `frame`
- `encircle`
- `overline`
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})
// Resets all modifiers
logger().red.bold.write("hello").all.write(" world").end
logger().red.bold.write("hello").reset.all.write(" world").end
// Or
logger().reset("bold")
// Or
logger().reset_bold()
// Resets the bold modifier, leaving the foreground modifier alone
logger().red.bold.write("hello").reset.bold.write(" world").end
```

@@ -187,0 +207,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
}
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
}
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
}