🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

concol

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

concol - npm Package Compare versions

Comparing version
1.0.1
to
2.0.0
+174
concol.js
/** Color console logging class */
import { styleText } from 'node:util';
export class ConCol {
static #appNameLen = 0;
static #namePad = 34;
static #valuePad = 10;
static #numFormat = new Intl.NumberFormat('en-GB', { maximumFractionDigits: 0 });
static #timeFormat = new Intl.DateTimeFormat('en-GB', { hour: '2-digit', minute: '2-digit', second: '2-digit', fractionalSecondDigits: 3 });
#app = 'ConCol';
#color = 'white';
#levelShow = Infinity;
/**
* Create a new ConCol instance
* @param {string} appName - the application name
* @param {string} color - the base text color: black, red, green, yellow, blue, magenta, cyan, white, gray
* @param {number} levelShow - the maximum log level to output (0 is silent/important only)
*/
constructor(appName, color, levelShow) {
// app name
this.#app = (appName || '').trim();
const aL = this.#app.length;
if (aL > ConCol.#appNameLen) ConCol.#appNameLen = aL;
// app base color
this.#color = color || this.#color;
// app show level
this.#levelShow = levelShow || this.#levelShow;
}
/**
* Outputs log message
* @param {string|Array} msg - 'string', 'str\nstr\n...', ['str','str',...], ['name',value,'unit'], [ str, ['name',value,'unit'],... ]
* @param {Number} level - message log level
*/
log(msg, level) {
this.#output('log', msg, level);
}
/**
* Outputs info message
* @param {string|Array} msg - 'string', 'str\nstr\n...', ['str','str',...], ['name',value,'unit'], [ str, ['name',value,'unit'],... ]
* @param {Number} level - message log level
*/
info(msg, level) {
this.#output('info', msg, level);
}
/**
* Outputs warning message
* @param {string|Array} msg - 'string', 'str\nstr\n...', ['str','str',...], ['name',value,'unit'], [ str, ['name',value,'unit'],... ]
* @param {Number} level - message log level
*/
warn(msg, level) {
this.#output('warn', msg, level, ' WARN: ', 'yellowBright');
}
/**
* Outputs error message
* @param {string|Array} msg - 'string', 'str\nstr\n...', ['str','str',...], ['name',value,'unit'], [ str, ['name',value,'unit'],... ]
* @param {Number} level - message log level
*/
error(msg, level) {
this.#output('error', msg, level, 'ERROR: ', 'redBright');
}
/**
* Outputs message to the console
* @private
* @param {string} method - console method (log, info, warn, error)
* @param {string|Array} msg - message
* @param {Number} level - log level
* @param {string} typeMsg - type of message (prefixed to message)
* @param {string} typeColor - color of message type
*/
#output(method = 'log', msg = '', level = 0, typeMsg, typeColor) {
if (level > this.#levelShow) return;
msg = this.#parseLog(msg, this.#color, typeMsg, typeColor);
const pre = this.#appPrefix();
msg.forEach(m => {
console[method]( pre + m );
});
}
/**
* Returns log prefix of time and app name
* @private
* @returns {string}
*/
#appPrefix() {
return (
styleText( 'grey', ConCol.#timeFormat.format(new Date()) ) +
styleText( ['dim', this.#color], ` [${ this.#app.padEnd( ConCol.#appNameLen, ' ') }] `)
);
}
/**
* Returns array of formatted strings to output
* @private
* @param {string|Array} msg - string with carriage returns or array of strings or [name, value, unit] arrays
* @param {string} color - app color
* @param {string} typeMsg - type of message (prefixed to message)
* @param {string} typeColor - color of message type
* @returns {Array} [{string}]
*/
#parseLog(msg, color, typeMsg = '', typeColor = 'white') {
// split string into array
if (typeof msg === 'string') {
msg = msg.split('\n');
}
// single name/value/unit?
if ((msg.length === 2 || msg.length === 3) && !isNaN(msg[1])) {
msg = [msg];
}
// parse array
msg = msg.map(m => {
let ret = '';
if (Array.isArray(m)) {
// name/value/unit array
const name = m[0] || '', unit = m[2] || '';
let val = m[1];
if (isNaN(val)) {
val = val || '';
}
else {
val = ConCol.#numFormat.format( parseFloat(val) );
}
ret = [
styleText(color, name.padStart(ConCol.#namePad - typeMsg.length, ' ') + ':') +
styleText('white', val.padStart(ConCol.#valuePad, ' ') + unit)
];
}
else {
// basic string
ret = String( m ).split('\n').map(s => styleText(color, s));
}
return ret.map(r => (typeMsg ? styleText(typeColor, typeMsg) : '' ) + r);
});
return msg.flat();
}
};
+1
-1
MIT License
Copyright (c) 2021 Craig Buckler
Copyright (c) 2025 Craig Buckler

@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy

{
"name": "concol",
"version": "1.0.1",
"description": "Output color messages to the console",
"version": "2.0.0",
"description": "Node.js color console logger",
"type": "module",
"main": "index.js",
"bin": "index.js",
"files": [ "index.js" ],
"main": "./concol.js",
"exports": "./concol.js",
"scripts": {
"test": "node --test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/craigbuckler/concol.git"
},
"files": [
"concol.js"
],
"keywords": [
"console",
"terminal",
"text",
"log",
"logging",
"color",
"colour"
"colour",
"error",
"warning",
"info"
],
"author": "Craig Buckler",
"license": "MIT",
"engines": {
"node": ">= 20.0.0"
},
"homepage": "https://github.com/craigbuckler/concol#readme",
"bugs": {
"url": "https://github.com/craigbuckler/concol/issues"
},
"homepage": "https://github.com/craigbuckler/concol#readme",
"dependencies": {
"chalk": "^5.0.0"
}
"repository": {
"type": "git",
"url": "git+https://github.com/craigbuckler/concol.git"
},
"funding": {
"type": "individual",
"url": "https://github.com/sponsors/craigbuckler"
},
"license": "MIT",
"author": "Craig Buckler"
}
+94
-11

@@ -1,19 +0,102 @@

# concol
# ConCol
Output color messages to the console.
A simple but flexible Node.js formatted color logger with no dependencies.
Install: `npm install concol --global`
Time is shown in 24-hour HH:MM:SS.MMM format. Numbers are formatted to 0-dp US/UK format.
Run: `npm <text> <fgHex> <bgHex>`
Where:
## Installation
* `<text>` is the text to output
* `<fgHex>` foreground hex color (optional)
* `<bgHex>` background hex color (optional)
```sh
npm install ConCol
```
Example which outputs blue text on a white background:
## Usage
```sh
concol " Hello World! " "#00c" "#fff"
```javascript
import { ConCol } from 'ConCol';
// create logger instances
const log1 = new ConCol('App One', 'cyan');
const log2 = new ConCol('App Two', 'green');
const log3 = new ConCol('App Three', 'white', 3);
// basic logging
log1.log('output single string');
log1.info('output string\nwith carriage returns');
log1.warn('show warning');
log1.error('show error');
/* output
13:58:10.171 [App One ] output single string
13:58:10.172 [App One ] output string
13:58:10.172 [App One ] with carriage returns
13:58:10.172 [App One ] WARN: show warning
13:58:10.172 [App One ] ERROR: show error
*/
// name/value/unit
log2.log(['ConCol requires', 0, ' modules']);
log2.log([ 'fibonacci sequence', ['first', 1], ['second', 1], ['third', 2] ]);
/* output
13:58:10.223 [App Two ] this requires: 0 modules
13:58:10.223 [App Two ] fibonacci sequence
13:58:10.223 [App Two ] first: 1
13:58:10.223 [App Two ] second: 1
13:58:10.223 [App Two ] third: 2
*/
// log level filtering
log3.info('level0', 0);
log3.info('level3', 3);
log3.info('level4', 4); // not shown - log level set to 3
/* output
13:58:10.275 [App Three] level0
13:58:10.275 [App Three] level3
*/
```
## API
### `new ConCol(appName, color, levelShow)`
Creates a new logger instance.
* `appName` (string): the application/namespace name (optional)
* `color` (string): base text color: black, red, green, yellow, blue, magenta, cyan, white, gray (default: white)
* `levelShow` (number): the maximum log level to output, so 0 is silent/important only (default: Infinity - shows everything)
The following methods are available.
### `log(msg, level)`
Outputs `console.log()` messages.
* `msg` (undefined|string|Array): the message (optional)
* `level` (string|Array): message logging level (default 0)
The `msg` can be any of:
* a string
* a string with carriage returns (outputs to separate lines)
* a `[name, value, unit]` array, e.g. `['errors', 0, ' found']`
* an array containing strings or `[name, value, unit]` arrays
### `info(msg, level)`
Outputs `console.info()` messages. Parameters are identical to [`log()`](#logmsg-level).
### `warn(msg, level)`
Outputs `console.warn()` messages prefixed with "WARN:". Parameters are identical to [`log()`](#logmsg-level).
### `error(msg, level)`
Outputs `console.error()` messages prefixed with "ERROR:". Parameters are identical to [`log()`](#logmsg-level).
#!/usr/bin/env node
import chalk from 'chalk';
const
str = process.argv[2] || '',
foreColor = process.argv[3],
backColor = process.argv[4];
let concol = chalk;
if (backColor) concol = concol.bgHex( backColor );
if (foreColor) concol = concol.hex( foreColor );
console.log( concol.bold( str ) );