simple-fancy-log v2.1.2
Demo
What is simple-fancy-log
?
This module helps creating clear, colorful, tagged logs
Logs are often ugly because not enough effort is put into making them clear and easy to read:
New user connected: 'Username'
User disconnected: 'Username'
Account created: 'Username#2'
And so on...
With simple-fancy-log
How to use
simple-fancy-log
uses a main class: the SimpleFancyLog
class.
The module exports the class, so you have to create your own instance:
This is what you will have to write in order to obtain the same log as above (comparative log between )
const SimpleFancyLog = require("simple-fancy-log");
const logger = new SimpleFancyLog();
// To write the first line
logger.addTag({color: "green", content: "Connection"});
logger.log("User connected: 'Username'");
// To write the second line
logger.addTag({color: "red", content: "Disconnection"});
logger.log("User disconnected: 'Username'");
// To write the third line
logger.addTags(
{bgColor: "magenta", color: "yellow", content: "New account"},
{color: "green", content: "Connection"}
);
logger.log("Account created: 'Username#2'");
Alternatively, since the 2.1.0, you can use the following example to obtain the same result
const SimpleFancyLog = require("simple-fancy-log");
const logger = new SimpleFancyLog();
// Creating re-usable tags
SimpleFancyLog.createTag({name: "connection", content: "Connection", color: "green"});
SimpleFancyLog.createTag({name: "disconnection", content: "Disconnection", color: "red"});
SimpleFancyLog.createTag({name: "account-creation", content: "New account", color: "yellow", bgColor: "magenta"});
// To write the first line
logger.addTag("connection");
logger.log("User connected: 'Username'");
// To write the second line
logger.addTag("disconnection");
logger.log("User disconnected: 'Username'");
// To write the third line
logger.addTags("account-creation", "connection");
logger.log("Account created: 'Username#2'");
Reference
None of the methods are static, you always need an instance of the SimpleFancyLog class to call them
addTag(tag)
Function that adds a tag to the current line
Kind: Method of SimpleFancyLog
Param | Type | Default | Description |
---|---|---|---|
tag | Object | Object that represents the tag to add | |
tag.content | String | Content of the tag ("test" yields "test") | |
tag.color | String | "default" | Foreground color of the tag |
tag.bgColor | String | "default" | Background color of the tag |
addTags(tags)
Function that adds one or multiple _tags to the current line
Kind: Method of SimpleFancyLog
Param | Type | Default | Description |
---|---|---|---|
tags | Object | Array.<Object> | Object(s) that represents the tag(s) to add | |
tags.content | String | Content of the tag ("test" yields "test") | |
tags.color | String | "default" | Foreground color of the tag |
tags.bgColor | String | "default" | Background color of the tag |
removeTag(tag)
Function that adds a tag to the current line
Kind: Method of SimpleFancyLog
Param | Type | Default | Description |
---|---|---|---|
tag | Object | Object that represents the tag to add | |
tag.content | String | Content of the tag ("test" yields "test") | |
tag.color | String | "" | Foreground color of the tag |
tag.bgColor | String | "" | Background color of the tag |
removeTags(tags)
Function that removes one or multiple _tags from the current line
Kind: method of SimpleFancyLog
Param | Type | Default | Description |
---|---|---|---|
tags | Object | Array.<Object> | Object(s) that represents the tag(s) to remove | |
tags.content | String | Content of the tag ("test" yields "test") | |
tags.color | String | "default" | Foreground color of the tag |
tags.bgColor | String | "default" | Background color of the tag |
toString() ⇒ String
Function that returns the string that will be logged when the log function is called
Kind: Method of SimpleFancyLog
Returns: String - - String that will be logged when the log function is called
log(message)
Function that logs a message with all the set tags. This function resets the message and the tags after it is displayed
Kind: Method of SimpleFancyLog
Param | Type | Description |
---|---|---|
message | String | Message to display along with the tags |
findTag(toFind) ⇒ *
Function that looks for a given tag
Kind: Static method of SimpleFancyLog
Returns: * - - Either {} or the tag object
Param | Type | Default | Description |
---|---|---|---|
toFind | Object | Options to find the tag | |
toFind.type | String | "" | Type of the tag to find (primarily for base predefined tags) |
toFind.name | String | "" | Name of the tag to find (for user defined tags) |
createTag(newTag) ⇒ *
Function that creates and stores a tag
Kind: Static method of SimpleFancyLog Returns: * - - Either {} (in case of an error) or the newly created tag object
Param | Type | Default | Description |
---|---|---|---|
newTag | Object | Tag to create | |
newTag.name | String | Name of the tag to create | |
newTag.content | String | Content of the tag | |
newTag.type | String | "default" | Type (classifier) of the tag |
newTag.color | String | "" | Color of the tag |
newTag.bgColor | String | "" | Background color of the tag |
Available colors
The available colors are:
Color | Foreground (text color) | Background |
---|---|---|
default (terminal default) | Yes | Yes |
red | Yes | Yes |
yellow | Yes | Yes |
green | Yes | Yes |
blue | Yes | Yes |
cyan | Yes | Yes |
white | Yes | Yes |
black | - | Yes |
magenta | - | Yes |