Flagmark
A very simple, lightweight, dependency free library for beautifully displaying messages to the console and writing simple logs to files.
Just try it!
NOTE`: I test it (terminal colors support) on Zorin OS 16 (linux) and VS Code. > If you have a problem with colors support in your system, let me know.
Let's start
How to install
npm install flagmark
or
yarn add flagmark
How to use
Import Flag class from library and create new instance
import { Flag } from 'flagmark'
const flag = new Flag({
// Add local date and time to output. Default true
useTimestamp: true,
// Add your own titles to messages
flags: {
success: 'Success', // Default "Success"
info: 'Información', // Default "Info"
warning: 'ध्यान', // Default "Warning"
error: '错误', // Default "Error"
performance: 'Function execution time'
},
// Enable logs to file. Default true
log: true,
// Add pathname for log files
path: 'log',
// Add filenames to log files
filenames: {
success: 'success', // Default "success"
info: 'Información', // Default "info"
warning: 'ध्यान', // Default "warning"
error: '错误', // Default "error"
performance: 'performance'
}
})
That's all. Now you can use flags in your code! Let's do sommething like this:
// with tag for log message
flag.success({
tag: 'database', // Optional
message: `I set flag here` // Message is required
})
flag.info({
tag: 'server', // Optional
message: `I set flag here` // Message is required
})
flag.warning({
tag: 'validation', // Optional
message: `I set flag here` // Message is required
})
// and with no tag
flag.error({
message: `I set flag here` // Message is required
})
Let's see console output:
What about log files?
After creating a new instance of the Flag() and configuring it, if you set log: true, Flagmark will create a new folder with the name from your configuration and {filename}.log files.
-log
--Información.log
--success.log
--ध्यान.log
--错误.log
Now each flag will be written to a file:
You can add the same name for each flag to write it to a single file:
filenames: {
success: 'log', // Default "success"
info: 'log', // Default "info"
warning: 'log', // Default "warning"
error: 'log', // Default "error"
performance: 'log'
}
Result:
Now let's create a performance measurement of the function.
This is work on the node js perf_hooks API, and all we need to do is wrap our function in flag.performance(), like this:
const func1 = () => {
const arr = []
for( let i = 0; i < 1000000; i++ ) {
arr.push(i*i)
}
}
const func2 = () => {
const arr = []
for( let i = 0; i < 1000000; i++ ) {
arr.push(i*i)
}
}
const func1Wrap = flag.performance(func1)
const func2Wrap = flag.performance(func2)
func1Wrap()
func2Wrap()
now every time the function is executed, we can see the console output:
and file output:
Finaly
So I think it's really simple and useful :)
Do you like this library?
TON: EQCRZ6N1UMrCOXEmd39ICtZ9aOrLFA4k91nul5yRf-Tnzci5