Comparing version 1.0.1 to 1.1.0
118
lib/log.js
@@ -7,2 +7,21 @@ "use strict"; | ||
function _getCallerFile() { | ||
try { | ||
var err = new Error(); | ||
var callerfile; | ||
var currentfile; | ||
Error.prepareStackTrace = function (err, stack) { return stack; }; | ||
currentfile = err.stack.shift().getFileName(); | ||
while (err.stack.length) { | ||
callerfile = err.stack.shift().getFileName(); | ||
if(currentfile !== callerfile) return callerfile; | ||
} | ||
} catch (err) {} | ||
return undefined; | ||
} | ||
function Log(config){ | ||
@@ -13,5 +32,12 @@ this.__config = {}; | ||
Log.globalConfig = { | ||
hrChar: '-', | ||
hrColor: "blue", | ||
showHrTimestamp: false, | ||
showProjectName: true, | ||
adaptScreenSize: true | ||
}; | ||
Log.prototype.init = function(config) { | ||
var pathFinderResult=undefined; | ||
// console.log("Init log: "+JSON.stringify(Log.globalConfig)+" \n"+JSON.stringify(config)); | ||
if(!Log.globalConfig){ | ||
@@ -21,4 +47,4 @@ Log.globalConfig = {}; | ||
Log.globalConfig.col_count = process.stdout.columns; | ||
Log.globalConfig.row_count = process.stdout.rows; | ||
Log.globalConfig.colCount = process.stdout.columns; | ||
Log.globalConfig.rowCount = process.stdout.rows; | ||
@@ -37,7 +63,3 @@ if(!Log.globalConfig.projectName){ | ||
if(config){ | ||
this.__config = config; | ||
}else{ | ||
this.__config = Log.globalConfig; | ||
} | ||
this.__config = Log.globalConfig; | ||
@@ -58,7 +80,13 @@ if(!this.__config.projectName){ | ||
if(config){ | ||
for(var k of Object.keys(config)){ | ||
this.__config[k] = config[k]; | ||
} | ||
} | ||
if(this.__config.adaptScreenSize !== false){ | ||
process.stdout.on('resize', function(){ | ||
if(!Log.USER_DEFINED_LENGTH){ | ||
Log.globalConfig.col_count = process.stdout.columns; | ||
Log.globalConfig.row_count = process.stdout.rows; | ||
Log.globalConfig.colCount = process.stdout.columns; | ||
Log.globalConfig.rowCount = process.stdout.rows; | ||
} | ||
@@ -70,15 +98,22 @@ }); | ||
Log.prototype.sM = function(color, tag, msg){ | ||
var time = new Date(); | ||
var callerFile = _getCallerFile(); | ||
showMsg(color, tag, msg, this.__config, callerFile, time); | ||
} | ||
Log.prototype.error = Log.prototype.e = function(tag, msg){ | ||
showMsg("red", tag, msg, this.__config); | ||
this.sM("red", tag, msg); | ||
} | ||
Log.prototype.warn = Log.prototype.w = function(tag, msg){ | ||
showMsg("yellow", tag, msg, this.__config); | ||
this.sM("yellow", tag, msg); | ||
} | ||
Log.prototype.info = Log.prototype.i = function(tag, msg){ | ||
showMsg("green", tag, msg, this.__config); | ||
this.sM("green", tag, msg); | ||
} | ||
Log.prototype.debug = Log.prototype.d = function(color, tag, msg){ | ||
var callerFile = _getCallerFile(); | ||
if(arguments.length < 3){ | ||
@@ -92,3 +127,3 @@ color = null; | ||
} | ||
showMsg(color, tag, msg, this.__config); | ||
this.sM(color, tag, msg); | ||
} | ||
@@ -100,2 +135,12 @@ | ||
Log.prototype.addConfig = function(key, value){ | ||
if(arguments.length >1){ | ||
this.__config[key] = value; | ||
}else{ | ||
for(var k of Objects.keys(key)){ | ||
this.__config[k] = key[k]; | ||
}; | ||
} | ||
} | ||
Log.prototype.setGlobalConfig = function(config){ | ||
@@ -106,24 +151,38 @@ Log.globalConfig = config; | ||
Log.prototype.addGlobalConfig = function(key, value){ | ||
Log.globalConfig[key] = value; | ||
if(arguments.length >1){ | ||
Log.globalConfig[key] = value; | ||
}else{ | ||
for(var k of Objects.keys(key)){ | ||
Log.globalConfig[k] = key[k]; | ||
}; | ||
} | ||
} | ||
Log.prototype.hr = function(length, tag, timestamp, color){ | ||
length = length || Log.globalConfig.col_count; | ||
if(!Log.globalConfig.show_hr_timestamp){ | ||
color = timestamp; | ||
Log.prototype.hr = function(length, timestamp, color, tag, char){ | ||
length = length || Log.globalConfig.colCount; | ||
if(arguments.length !== 0){ | ||
if(typeof(timestamp)!=="boolean"){ | ||
color = timestamp; | ||
} | ||
if(timestamp !== true){ | ||
if(this.__config.showHrTimestamp!==true){ | ||
char = tag | ||
tag = color; | ||
color = timestamp; | ||
timestamp = undefined; | ||
} | ||
} | ||
} | ||
var str = ""; | ||
for (var i = 0; i < length; i++) { | ||
str+=Log.globalConfig.hr_element || '-'; | ||
var hrChar = this.__config.hrChar || '-'; | ||
for (var i = 0; i < length/hrChar.length; i++) { | ||
str+=char || hrChar; | ||
} | ||
showHr(color || Log.globalConfig.hr_color, tag || '', str, timestamp) | ||
str=str.substring(0,length); | ||
showHr(color || this.__config.hrColor, tag, str, timestamp); | ||
} | ||
Log.globalConfig = { | ||
hr_element: '-', | ||
hr_color: 'blue', | ||
show_hr_timestamp: false | ||
}; | ||
@@ -133,7 +192,8 @@ //Constants | ||
Log.prototype.PROJECT_NAME = "projectName"; | ||
Log.prototype.HORIZONTAL_ELEMENT = "hr_element"; | ||
Log.prototype.HORIZONTAL_LENGTH = "col_count"; | ||
Log.prototype.HORIZONTAL_ELEMENT = "hrChar"; | ||
Log.prototype.HORIZONTAL_LENGTH = "colCount"; | ||
Log.prototype.USER_DEFINED_LENGTH = "userdefinedlength"; | ||
Log.prototype.BOLD_TAG = "tagBold" | ||
module.exports = Log; |
@@ -22,3 +22,3 @@ "use strict"; | ||
_config.projectName = JSON.parse(con).name; | ||
_config.projectRoot = path.resolve(__dirname, '..', filename).replace('package.json', ''); | ||
_config.projectRoot = path.resolve(__dirname, '..', '..', filename).replace('package.json', ''); | ||
// console.log("Found _config: "+JSON.stringify(_config)); | ||
@@ -25,0 +25,0 @@ if(!(_config.projectName === 'java-style-debugger')) |
@@ -10,13 +10,27 @@ "use strict"; | ||
if(!time){ | ||
time = new Date(); | ||
} | ||
if(timestamp){ | ||
str += timeStamp(time)+" "; | ||
str += timeStamp(time)+" : "; | ||
} | ||
// console.log("timestamp: "+timestamp); | ||
// console.log("str: "+str); | ||
if(!tag){ | ||
tag = ''; | ||
}else { | ||
tag=tag+" : "; | ||
} | ||
console.log(colors[color](str+tag+msg)) | ||
if(!color){ | ||
color = "blue"; | ||
} | ||
// console.log(str+tag+msg); | ||
console.log(colors[color](str+tag+msg)); | ||
} | ||
module.exports = showMsg; |
@@ -9,12 +9,15 @@ "use strict"; | ||
function showMsg(color, tag, msg, config){ | ||
function showMsg(color, tag, msg, config , callerFile, time){ | ||
var Log = require('./log'); | ||
if(!time){ | ||
time = new Date(); | ||
} | ||
if(callerFile){ | ||
var path = callerFile.replace(config.projectRoot, ''); | ||
} | ||
var time = new Date(); | ||
var path = __filename.replace(config.projectRoot || Log.globalConfig.projectRoot, ''); | ||
if(!msg){ | ||
msg = tag; | ||
tag = config.defaultTag || Log.globalConfig.defaultTag; | ||
tag = config.defaultTag; | ||
} | ||
@@ -26,5 +29,23 @@ | ||
console.log(colors[color](timeStamp(time)+" "+path+" : ")+colors[color].bold(tag)+colors[color](" : "+msg)); | ||
var string = ""; | ||
string += colors[color](timeStamp(time)+" "); | ||
if(config.projectName && config.showProjectName){ | ||
string+=colors[color](config.projectName+" | "); | ||
} | ||
string+=colors[color](path+" : "); | ||
if(config.tagBold){ | ||
string+=colors[color].bold(tag); | ||
}else{ | ||
string += colors[color](tag); | ||
} | ||
string+=colors[color](" : "+msg); | ||
console.log(string); | ||
} | ||
module.exports = showMsg; |
{ | ||
"name": "jlogger", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -14,2 +14,5 @@ "main": "index.js", | ||
"android", | ||
"node", | ||
"log", | ||
"logcat", | ||
"java", | ||
@@ -16,0 +19,0 @@ "jibinmathews7" |
@@ -10,3 +10,3 @@ # Android Log Cat Inspired Logger for NodeJS | ||
Log.addGlobalConfig("projectName", "<your project name>"); //Usage under development | ||
Log.addGlobalConfig("projectName", "<your project name>"); //Usage under development | ||
Log.addGlobalConfig("projectRoot", "<your project root>"); //Usage under development | ||
@@ -30,2 +30,4 @@ Log.addGlobalConfig("defaultTag", "defaultTag"); //Default tag for every log | ||
Log.debug(color, TAG, "msg"); //Specify color. Default is white | ||
Log.hr(length); //Draw a dashed horizontal line. Default length is that of the terminal | ||
``` | ||
@@ -44,1 +46,29 @@ | ||
``` | ||
### Available customisations are: | ||
``` | ||
Log.setGlobalConfig({ | ||
"defaultTag": "<your default tag name>", //Default tag for when tag is not specified | ||
"tagBold": <true/false>, //If true then tags will be in bold, | ||
"colCount": <length of hr>, //Default length of horizontal line. If not specified then it fits to terminal size | ||
"hrChar": "=", //Draw horizontal line with specified character. Default is "-" | ||
"adaptScreenSize": <true/false>, //If true then hr width will fit the current terminal size | ||
"projectName": "<your project name>", //Display this in your log | ||
"showProjectName": <true/false>, //If false then project name is not printing in log. Default is true | ||
}); | ||
``` | ||
### Using Log.hr() | ||
``` | ||
Log.hr(length, showTimestamp, color, tag, char); | ||
``` | ||
length: *Number* Number of characters to draw. Default is set by global Config | ||
showTimeStamp: *true/false* Whether to show timestamp. Default is false | ||
color: *color name* Color for the horizontal line | ||
tag: *tag* tag for hr | ||
char: *"="* String with which the hr is to be drawn. (tag parameter should be present, atleast an empty placeholder) |
@@ -14,3 +14,3 @@ console.log("Hello world. Starting test"); | ||
console.log("\n\n......................................................................\n\n") | ||
// Log.hr(); | ||
@@ -27,6 +27,6 @@ var Log = require('..'); | ||
Log.hr(); | ||
console.log("\n\n......................................................................\n\n"); | ||
var Log2 = require('..'); | ||
var Log2 = new Log2.Logger({'defaultTag': "Jibin"}); | ||
var Log2 = new Log2.Logger({'defaultTag': "Jibin", "hrChar": "*", "projectName": "MyTestAPp", "tagBold": true,"showProjectName":false}); | ||
// Log2.config(); | ||
@@ -42,2 +42,2 @@ | ||
Log.hr(); | ||
Log2.hr(15, true, "green", "", "="); |
11577
282
72