Skip to content

Commit

Permalink
Changes in module behavior in version 1.4 (see README)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Bezdek committed Jan 31, 2016
1 parent 3e4f33b commit 6d7e91e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@

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

#Changes in version 1.4#

##Log level changes##
- Now the default log level is error & warning. Log ``message`` and ``success`` is disabled by default. It can be enabled again by setting the logLevel (``logLevel='error,warning,success,message'``)

- Any log message can be enabled temporary by
- - A) via process.env.DEBUG=true or
- - B) via context based environment variable which can be assigned to constructor

##Activation switch changes##
Clrlog has now a new property inside the constructor.
An env variable (``process.env.CUSTOM_VARIALBE``) can be registered (e.g by starting the node app ``CUSTOM_VARIALBE=true node app.js `` )
which enables any log level in the specific context for a better app behave logging.


#How to use#
Instal Clrlog via npm
```js
Expand All @@ -18,17 +33,17 @@ Require the Clrlog class
```
To give log messages color set global.DEBUG=true or run application in node.js's debug mode
```js
global.DEBUG = true;
process.env.DEBUG = true; // or on startup DEBUG=true node app.js
```
Or create a file named ``debug`` in applications root folder

Clrlog can be used as a function
```js
Clrlog("Hello I'm Clrlog");
```
Or as an object of the Clrlog class
Or as an object of the Clrlog class (The logger can be registered to a process.env property to enable logging for different purposes)
```js
var myClrlog = new Clrlog("I support logging into logfiles too", 'success', __dirname + '/application.log');
var myClrlog = new Clrlog("I support logging into logfiles too", 'success', __dirname + '/application.log', 'MY_CUSTOM_LOG');
myClrlog.logLevel = 'error';
myClrlog.warning('This line is not written into logfile');
myClrlog.error('This line is written into logfile');
Expand Down
7 changes: 5 additions & 2 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@
/////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');

// When running node app using env var LOG_CONTEXT_ONLY any log is shown in terminal and not written in logfile
// This is a useful feature to enable logging only for a specific context
var myClrlog = new Clrlog("And hold log instances for more complex logging purposes", 'success', __dirname + '/application.log', 'LOG_CONTEXT_ONLY');
var myClrlog = new Clrlog("And hold log instances for more complex logging purposes", 'success', undefined, 'LOG_CONTEXT_ONLY');

// LogLevel can be set on custom purposes
myClrlog.logLevel = 'error'; // Only errormessages
Expand All @@ -71,7 +75,6 @@
myClrlog.success('This line goes into the logfile too');



myClrlog.success({
I: 'can',
log: 'Objects too'
Expand Down
34 changes: 26 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*global DEBUG*/
/*global v8debug*/
/*global console*/
/*global process*/
var Clrlog = null;
(function () {
"use strict";
Expand All @@ -22,18 +23,19 @@ var Clrlog = null;
* @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)
* @param {String} sProcessEnv Determine a process variable which enables the specific logging (Useful when several parts should have separate logging)
* @return {Object} A Clrlog object (if called as a function
*/
Clrlog = function (mLogdata, sType, sLogFile, bTraceErrors) {
Clrlog = function (mLogdata, sType, sLogFile, sProcessEnv, bTraceErrors) {


// Check if Clrlog was called as a function
if (this !== undefined) {
var fs = require('fs');


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

// The end string for colored messages
Expand All @@ -46,10 +48,19 @@ var Clrlog = null;
}

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

if (this.explicityDebug === undefined) {
this.explicityDebug = false;
}

if (process !== undefined && typeof process.env === 'object' && typeof sProcessEnv === 'string' && process.env[sProcessEnv] !== undefined) {
this.explicityDebug = true;
}


// Override log method
if (this.trace) {
logMethod = 'trace';
Expand Down Expand Up @@ -111,20 +122,20 @@ var Clrlog = null;
}

// In debug mode colorized messages are dumped out
if (global.DEBUG === true || typeof v8debug === 'object') {
if (this.explicityDebug === true || (this.logLevel.indexOf(sType) !== -1 && (process.env.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 {
} else if (this.logLevel.indexOf(sType) !== -1) {
console[logMethod](mLogdata);
}
} else {
// If it was a function call run as a class instance
return new Clrlog(mLogdata, sType, sLogFile, bTraceErrors);
return new Clrlog(mLogdata, sType, sLogFile, sProcessEnv, bTraceErrors);
}
};

Expand All @@ -135,6 +146,13 @@ var Clrlog = null;
*/
Clrlog.prototype.trace = false;

/**
* Determine specific debugin is enabled
* @property debug (default false)
* @type {Boolean}
*/
Clrlog.prototype.debug = false;

/**
* The default message type
* @property type (default message)
Expand Down Expand Up @@ -171,7 +189,7 @@ var Clrlog = null;
* @property logLevel
* @type {String}
*/
Clrlog.prototype.logLevel = 'error,warning,success,message';
Clrlog.prototype.logLevel = 'error,warning';

/**
* Throw error message with previously defined settings
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "clrlog",
"description": "Lightweight colorful JavaScript application logger with stack trace and logfile support for node.js",
"version": "1.1.2",
"version": "1.4.1",
"author": {
"name": "Bernhard Bezdek",
"email": "[email protected]"
Expand Down

0 comments on commit 6d7e91e

Please sign in to comment.