Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

color-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

color-logger - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

377

out/src/ColorLogger.js
'use strict';
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } };
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
Object.defineProperty(exports, '__esModule', {
Object.defineProperty(exports, "__esModule", {
value: true
});
// ASCII ESCAPE SEQUENCE http://www5c.biglobe.ne.jp/~ecb/assembler2/b_2.html
var levelToColor = {
v: '\u001b[35m[V]', // purple
d: '\u001b[34m[D]', // blue
i: '\u001b[32m[I]', // green
w: '\u001b[33m[W]', // yellow
e: '\u001b[31m[E]' // red
const levelToColor = {
v: '[V]', // purple
d: '[D]', // blue
i: '[I]', // green
w: '[W]', // yellow
e: '[E]' // red
};

@@ -45,269 +41,168 @@

*/
var ColorLogger = (function () {
class ColorLogger {
/**
* create instance.
* @param {string} tag - tag text.
*/
function ColorLogger(tag) {
_classCallCheck(this, ColorLogger);
this._tag = tag;
constructor() {
this._allLogs = [];
}
_createClass(ColorLogger, [{
key: 'v',
/**
* display verbose(purple) log and tag.
* @param {...*} msg - log message.
* @returns {string} formatted log message.
*/
value: function v() {
var _constructor;
for (var _len = arguments.length, msg = Array(_len), _key = 0; _key < _len; _key++) {
msg[_key] = arguments[_key];
}
return (_constructor = this.constructor)._output.apply(_constructor, ['v', '[' + this._tag + ']'].concat(msg));
/**
* log information.
* @return {string} - file name and line number.
* @private
*/
_getInfo() {
let info;
try {
throw new Error();
} catch (e) {
const lines = e.stack.split('\n');
const line = lines[4];
const matched = line.match(/\(.*?([^/]*:\d+:\d+)\)$/);
info = matched[1];
}
}, {
key: 'd',
/**
* display debug(blue) log and tag.
* @param {...*} msg - log message.
* @returns {string} formatted log message.
*/
value: function d() {
var _constructor2;
return info;
}
for (var _len2 = arguments.length, msg = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
msg[_key2] = arguments[_key2];
}
/**
* clear all logs.
*/
clearAllLogs() {
this._allLogs = [];
}
return (_constructor2 = this.constructor)._output.apply(_constructor2, ['d', '[' + this._tag + ']'].concat(msg));
}
}, {
key: 'i',
/**
* all logs
* @type {String[]}
*/
get allLogs() {
return [].concat(this._allLogs);
}
/**
* display info(green) log and tag.
* @param {...*} msg - log message.
* @returns {string} formatted log message.
*/
value: function i() {
var _constructor3;
/**
* if false, not display log. default is true.
*/
set debug(v) {
this._debug = v;
}
for (var _len3 = arguments.length, msg = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
msg[_key3] = arguments[_key3];
}
/**
* display log.
* @param {string} level - log level. v, d, i, w, e.
* @param {...*} msg - log message.
* @returns {string} - formatted log message.
* @private
*/
_output(level) {
const text = [];
return (_constructor3 = this.constructor)._output.apply(_constructor3, ['i', '[' + this._tag + ']'].concat(msg));
for (var _len = arguments.length, msg = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
msg[_key - 1] = arguments[_key];
}
}, {
key: 'w',
/**
* display warning(yellow) log and tag.
* @param {...*} msg - log message.
* @returns {string} formatted log message.
*/
value: function w() {
var _constructor4;
for (var _len4 = arguments.length, msg = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
msg[_key4] = arguments[_key4];
for (const m of msg) {
if (typeof m === 'object') {
text.push(JSON.stringify(m, null, 2));
} else {
text.push(m);
}
return (_constructor4 = this.constructor)._output.apply(_constructor4, ['w', '[' + this._tag + ']'].concat(msg));
}
}, {
key: 'e',
/**
* display error(red) log and tag.
* @param {...*} msg - log message.
* @returns {string} formatted log message.
*/
value: function e() {
var _constructor5;
const color = levelToColor[level];
const info = this._getInfo();
for (var _len5 = arguments.length, msg = Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
msg[_key5] = arguments[_key5];
}
const d = new Date();
let month = d.getMonth() + 1;
if (month < 10) month = `0${ month }`;
let date = d.getDate();
if (date < 10) date = `0${ date }`;
let hour = d.getHours();
if (hour < 10) hour = `0${ hour }`;
let minutes = d.getMinutes();
if (minutes < 10) minutes = `0${ minutes }`;
let sec = d.getSeconds();
if (sec < 10) sec = `0${ sec }`;
const now = `${ d.getFullYear() }-${ month }-${ date }T${ hour }:${ minutes }:${ sec }.${ d.getMilliseconds() }Z`;
return (_constructor5 = this.constructor)._output.apply(_constructor5, ['e', '[' + this._tag + ']'].concat(msg));
}
}], [{
key: '_getInfo',
const log = `${ color } [${ now }] [${ info }] ${ text.join(' ') }`;
/**
* log information.
* @return {string} - file name and line number.
* @private
*/
value: function _getInfo() {
var info = undefined;
try {
throw new Error();
} catch (e) {
var lines = e.stack.split('\n');
var line = lines[4];
var matched = line.match(/\/([^/]*?:\d+:\d+)/);
info = matched[1];
}
this._allLogs.push(log);
if (this._allLogs.length > 10000) this._allLogs.shift();
return info;
}
}, {
key: 'debug',
if (this._debug) console.log(log);
/**
* if false, not display log. default is true.
*/
set: function (v) {
this._debug = v;
}
}, {
key: '_output',
return log;
}
/**
* display log.
* @param {string} level - log level. v, d, i, w, e.
* @param {...*} msg - log message.
* @returns {string} - formatted log message.
* @private
*/
value: function _output(level) {
for (var _len6 = arguments.length, msg = Array(_len6 > 1 ? _len6 - 1 : 0), _key6 = 1; _key6 < _len6; _key6++) {
msg[_key6 - 1] = arguments[_key6];
}
if (!this._debug) {
return '';
}var text = [];
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = msg[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var m = _step.value;
if (typeof m === 'object') {
text.push(JSON.stringify(m, null, 2));
} else {
text.push(m);
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator['return']) {
_iterator['return']();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
var color = levelToColor[level];
var info = this._getInfo();
var d = new Date();
var now = '' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds() + '.' + d.getMilliseconds();
var log = '' + color + ' [' + now + '] [' + info + '] ' + text.join(' ') + '\u001b[0m';
console.log(log);
return log;
/**
* display verbose(purple) log.
* @param {...*} msg - log message.
* @returns {string} formatted log message.
*/
v() {
for (var _len2 = arguments.length, msg = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
msg[_key2] = arguments[_key2];
}
}, {
key: 'v',
/**
* display verbose(purple) log.
* @param {...*} msg - log message.
* @returns {string} formatted log message.
*/
value: function v() {
for (var _len7 = arguments.length, msg = Array(_len7), _key7 = 0; _key7 < _len7; _key7++) {
msg[_key7] = arguments[_key7];
}
return this._output.apply(this, ['v'].concat(msg));
}
return this._output.apply(this, ['v'].concat(msg));
/**
* display debug(blue) log.
* @param {...*} msg - log message.
* @returns {string} formatted log message.
*/
d() {
for (var _len3 = arguments.length, msg = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
msg[_key3] = arguments[_key3];
}
}, {
key: 'd',
/**
* display debug(blue) log.
* @param {...*} msg - log message.
* @returns {string} formatted log message.
*/
value: function d() {
for (var _len8 = arguments.length, msg = Array(_len8), _key8 = 0; _key8 < _len8; _key8++) {
msg[_key8] = arguments[_key8];
}
return this._output.apply(this, ['d'].concat(msg));
}
return this._output.apply(this, ['d'].concat(msg));
/**
* display info(green) log.
* @param {...*} msg - log message.
* @returns {string} formatted log message.
*/
i() {
for (var _len4 = arguments.length, msg = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
msg[_key4] = arguments[_key4];
}
}, {
key: 'i',
/**
* display info(green) log.
* @param {...*} msg - log message.
* @returns {string} formatted log message.
*/
value: function i() {
for (var _len9 = arguments.length, msg = Array(_len9), _key9 = 0; _key9 < _len9; _key9++) {
msg[_key9] = arguments[_key9];
}
return this._output.apply(this, ['i'].concat(msg));
}
return this._output.apply(this, ['i'].concat(msg));
/**
* display warning(yellow) log.
* @param {...*} msg - log message.
* @returns {string} formatted log message.
*/
w() {
for (var _len5 = arguments.length, msg = Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
msg[_key5] = arguments[_key5];
}
}, {
key: 'w',
/**
* display warning(yellow) log.
* @param {...*} msg - log message.
* @returns {string} formatted log message.
*/
value: function w() {
for (var _len10 = arguments.length, msg = Array(_len10), _key10 = 0; _key10 < _len10; _key10++) {
msg[_key10] = arguments[_key10];
}
return this._output.apply(this, ['w'].concat(msg));
}
return this._output.apply(this, ['w'].concat(msg));
/**
* display warning(red) log.
* @param {...*} msg - log message.
* @returns {string} formatted log message.
*/
e() {
for (var _len6 = arguments.length, msg = Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
msg[_key6] = arguments[_key6];
}
}, {
key: 'e',
/**
* display warning(red) log.
* @param {...*} msg - log message.
* @returns {string} formatted log message.
*/
value: function e() {
for (var _len11 = arguments.length, msg = Array(_len11), _key11 = 0; _key11 < _len11; _key11++) {
msg[_key11] = arguments[_key11];
}
return this._output.apply(this, ['e'].concat(msg));
}
}
return this._output.apply(this, ['e'].concat(msg));
}
}]);
return ColorLogger;
})();
exports['default'] = ColorLogger;
ColorLogger.debug = true;
module.exports = exports['default'];
exports.ColorLogger = ColorLogger;
const logger = new ColorLogger();
logger.debug = true;
exports.default = logger;
{
"name": "color-logger",
"version": "0.0.3",
"version": "0.0.4",
"description": "Colorful Logger",

@@ -15,4 +15,3 @@ "author": "h13i32maru",

"test-es5": "bash ./script/test-es5.sh",
"esdoc": "bash ./script/esdoc.sh",
"start": "bash ./script/server.sh"
"esdoc": "bash ./script/esdoc.sh"
},

@@ -22,9 +21,13 @@ "dependencies": {

"devDependencies": {
"babel": "",
"mocha": "",
"power-assert": "",
"espower-loader": "",
"istanbul": "",
"coveralls": "",
"http-server": ""
"esdoc-importpath-plugin": "0.0.1",
"esdoc": "0.4.6",
"mocha": "2.4.5",
"istanbul": "0.4.3",
"coveralls": "2.11.9",
"babel-register": "6.7.2",
"babel-plugin-transform-es2015-modules-commonjs": "6.7.0",
"babel-plugin-transform-es2015-destructuring": "6.6.5",
"babel-plugin-transform-es2015-parameters": "6.7.0",
"babel-plugin-transform-es2015-spread": "6.6.5",
"babel-cli": "6.6.5"
},

@@ -31,0 +34,0 @@ "keywords": [

@@ -22,6 +22,2 @@ [![Build Status](https://travis-ci.org/h13i32maru/color-logger.svg?branch=master)](https://travis-ci.org/h13i32maru/color-logger)

// use tag
let logger = new Logger('MyTag');
logger.w('warning log');
// all log level and colors

@@ -28,0 +24,0 @@ Logger.v('verbose log');

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc