Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bebe committed Apr 5, 2014
0 parents commit 5f0373d
Show file tree
Hide file tree
Showing 6 changed files with 433 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
demo/*.log
.DS_Store
189 changes: 189 additions & 0 deletions Clrlog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*global DEBUG*/
/*global v8debug*/
/*global console*/
var Clrlog = null;
(function () {
"use strict";

/**
* Lightweight colorful JavaScript application logger with stack trace and logfile support for node.js
* It detect by itself if node.js application is in debug mode.
* Debug mode can be also enabled by set global.DEBUG===true
* There's also logfile support to save logmessages including logging time
*
* Clrlog works called as a plain old javascript function or as an object for more comples purposes
*
* @author Bernhard Bezdek <[email protected]>
* @see https://github.com/BernhardBezdek/Clrlog/blob/master/README.md>
* @license MIT https://github.com/BernhardBezdek/Clrlog/blob/master/LICENSE
* @class Clrlog
* @constructor
* @param {Object} mLogdata The logging data (Any datatype allowed
* @param {String} sType default 'message' (message, success, warning, error available)
* @param {Boolean} sLogFile write messages into a log file (default none)
* @param {Boolean} bTraceErrors Determine if errormessages should output as a trace shutdown application (default false)
*/
Clrlog = function (mLogdata, sType, sLogFile, bTraceErrors) {

if (global.DEBUG === undefined) {
global.DEBUG = false;
}

// Check if Clrlog was called as a function
if (this !== undefined) {


// The end string for colored messages
var logMethod = 'log';

// Determine the output type
if (this.types[sType] !== undefined) {
this.type = sType;
}

// Enable Tracing for Errors
if (typeof bTraceErrors === 'boolean') {
this.trace = bTraceErrors;
}

// Override log method
if (this.trace) {
logMethod = 'trace';
}

if (sLogFile !== undefined) {
this.logFile = sLogFile;
}


// Hanlde writing into a logfile
if (this.logFile !== false && this.logLevel.split(',').indexOf(this.type) !== -1) {


var fs = require('fs');
var sWriteFile = new Date().toString() + ' | ' + sType.toUpperCase() + ' ᑀ ';

try {
if (['boolean', 'number', 'string'].indexOf(typeof (mLogdata)) !== -1) {
sWriteFile += mLogdata.replace(/"/g, '');
} else {
sWriteFile += JSON.stringify(mLogdata, null, "\t");
}

sWriteFile += "\n";

fs.appendFile(sLogFile, sWriteFile, function (error) {
if (error !== null) {
Clrlog(error);
}
});
} catch (e) {
Clrlog(e, 'error');
}
}

// In debug mode colorized messages are dumped out
if (global.DEBUG === true || typeof v8debug === 'object') {
if (['boolean', 'number', 'string'].indexOf(typeof (mLogdata)) !== -1) {
console[logMethod](this.types[this.type] + mLogdata + this.endLog);
} else {
console[logMethod](this.types[this.type]);
console[logMethod](mLogdata);
console[logMethod](this.endLog);
}
} else {
console[logMethod](mLogdata);
}
} else {
// If it was a function call run as a class instance
new Clrlog(mLogdata, sType, sLogFile, bTraceErrors);
}
};

/**
* Determine if trace is used for errors
* @property trace (default false)
* @type {Boolean}
*/
Clrlog.prototype.trace = false;

/**
* The default message type
* @property type (default message)
* @type {String}
*/
Clrlog.prototype.type = 'message';

/**
* Available log types
* @property types
* @type {Object}
*/
Clrlog.prototype.types = {
message: '\x1B[34m', // Blue colored
success: '\x1B[32m', // Green colored
warning: '\x1B[33m', // Yellow colored
error: '\x1B[31m' // Red colored
};

/**
* The finalizing string for noncolored log Messages
* @type {string}
*/
Clrlog.prototype.endLog = '\x1B[39m';

/**
* @property logFile
* @type {undefined}
*/
Clrlog.prototype.logFile = false;

/**
* Specify which types log messages are written into a file
* @property logLevel
* @type {String}
*/
Clrlog.prototype.logLevel = 'error,warning,success,message';

/**
* Throw error message with previously defined settings
*
* @method error
* @param message
*/
Clrlog.prototype.error = function (message) {
this.constructor(message, 'error', this.logFile);
};

/**
* Throw warning with previously defined settings
*
* @method warning
* @param message
*/
Clrlog.prototype.warning = function (message) {
this.constructor(message, 'warning', this.logFile);
};

/**
* Throw success message with previously defined settings
*
* @method success
* @param message
*/
Clrlog.prototype.success = function (message) {
this.constructor(message, 'success', this.logFile);
};

/**
* Throw message with previously defined settings
*
* @method message
* @param message
*/
Clrlog.prototype.message = function (message) {
this.constructor(message, 'message', this.logFile);
};
})();

module.exports = Clrlog;
114 changes: 114 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#Clrlog#
##Lightweight colorful JavaScript application logger with stack trace and logfile support for node.js##

![Image](https://raw.githubusercontent.com/BernhardBezdek/Clrlog/master/previews/example_output.png "Example output")

#How to use#
Require the Clrlog class

var Clrlog = require(__dirname + '/../Clrlog.js');

Clrlog can be used as a function

Clrlog("Hello I'm Clrlog");

Or as an object

var myClrlog = new Clrlog("I also support logging into logfiles", 'success', __dirname + '/application.log');
myClrlog.logLevel = 'error';
myClrlog.error('This line is not written into logfile');


Set custom log levels for a single logmessage type

myClrlog.logLevel = 'error';

Or set log levels for multiple log message types

myClrlog.logLevel = 'error,warning,success';


The logs are stored in the following format

Sat Apr 05 2014 20:32:42 GMT+0200 (CEST) | SUCCESS ᑀ And hold log instances for more complex logging purposes
Sat Apr 05 2014 20:32:42 GMT+0200 (CEST) | SUCCESS ᑀ The current loglevel is message,success
Sat Apr 05 2014 20:32:42 GMT+0200 (CEST) | MESSAGE ᑀ This message goes into the logfile
Sat Apr 05 2014 20:32:42 GMT+0200 (CEST) | SUCCESS ᑀ This line goes into the logfile too
Sat Apr 05 2014 20:32:42 GMT+0200 (CEST) | SUCCESS ᑀ {
"I": "can",
"log": "Objects too"
}


#Full working example (demo/demo.js)#

(function () {
"use strict";

// Enable global.DEBUG for colorful logging purposes in your shell
// OR colorize logging when node is started in debug mode
global.DEBUG = true;

// Fetch Clrlog
var Clrlog = require(__dirname + '/../Clrlog.js');

////////////////////////////////////////////////////////
/////Call Clrlog like a plain old javascript funtion/////
////////////////////////////////////////////////////////

Clrlog("Hello I'm Clrlog");
Clrlog("I was successful", 'success');
Clrlog("I've need to warn you ", 'warning');
Clrlog("I've made a mistake", 'error');

// Object Logging
Clrlog({
I: 'can',
log: 'Objects too'
}, 'success');


// Save log messages in a file
Clrlog("And I can store my logs into a file", 'message', __dirname + '/example.log');

// Show stack trace behind logmessage
Clrlog("My logs can also include a detailed stack trace", 'success', false, true);


////////////////////////////////////////////////////////
/////Call Clrlog as an object for more comples stuff/////
////////////////////////////////////////////////////////

var myClrlog = new Clrlog("And hold log instances for more complex logging purposes", 'success', __dirname + '/application.log');

// LogLevel can be set on custom purposes
myClrlog.logLevel = 'error'; // Only errormessages
myClrlog.logLevel = 'warning'; // Only warnings
myClrlog.logLevel = 'success'; // Only success
myClrlog.logLevel = 'message'; // Only messages


// Combined log levels are possble too
myClrlog.logLevel = 'error,warning';
myClrlog.logLevel = 'error,warning,success';
myClrlog.logLevel = 'message,success';


// Loglevel settings example
myClrlog.logLevel = 'message,success';

myClrlog.success('The current loglevel is ' + myClrlog.logLevel);

myClrlog.message('This message goes into the logfile');
myClrlog.error('This line is not written into logfile');
myClrlog.success('This line goes into the logfile too');

})();

Author:
Clrlog was written by Bernhard Bezdek

Released under MIT License

###Dependencies###
node.js file system api
73 changes: 73 additions & 0 deletions demo/demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
(function () {
"use strict";

// Enable global.DEBUG for colorful logging purposes in your shell
// OR colorize logging when node is started in debug mode
global.DEBUG = true;

// Fetch Clrlog
var Clrlog = require(__dirname + '/../Clrlog.js');

////////////////////////////////////////////////////////
/////Call Clrlog like a plain old javascript funtion/////
////////////////////////////////////////////////////////

Clrlog("Hello I'm Clrlog");
Clrlog("I was successful", 'success');
Clrlog("I've need to warn you ", 'warning');
Clrlog("I've made a mistake", 'error');

// Object Logging
Clrlog({
I: 'can',
log: 'Objects too'
}, 'success');


// Save log messages in a file
Clrlog("And I can store my logs into a file", 'message', __dirname + '/example.log');

console.log("");
console.log("");

// Show stack trace behind logmessage
Clrlog("My logs can also include a detailed stack trace", 'success', false, true);

console.log("");
console.log("");

////////////////////////////////////////////////////////
/////Call Clrlog as an object for more comples stuff/////
////////////////////////////////////////////////////////

var myClrlog = new Clrlog("And hold log instances for more complex logging purposes", 'success', __dirname + '/application.log');

// LogLevel can be set on custom purposes
myClrlog.logLevel = 'error'; // Only errormessages
myClrlog.logLevel = 'warning'; // Only warnings
myClrlog.logLevel = 'success'; // Only success
myClrlog.logLevel = 'message'; // Only messages


// Combined log levels are possble too
myClrlog.logLevel = 'error,warning';
myClrlog.logLevel = 'error,warning,success';
myClrlog.logLevel = 'message,success';


// Loglevel settings example
myClrlog.logLevel = 'message,success';

myClrlog.success('The current loglevel is ' + myClrlog.logLevel);

myClrlog.message('This message goes into the logfile');
myClrlog.error('This line is not written into logfile');
myClrlog.success('This line goes into the logfile too');

myClrlog.success({
I: 'can',
log: 'Objects too'
});


})();
Binary file added previews/example_output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 5f0373d

Please sign in to comment.