add file logging

parent a8b5173b
......@@ -2,26 +2,106 @@
* Created by danilaeremin on 28.07.16.
*/
var logger = {
debug : function (string) {
if (this._level >= this.DEBUG)
console.log(this._prefix + '\033[34m Debug: \033[30m' + string);
if (!('toJSON' in Error.prototype))
Object.defineProperty(Error.prototype, 'toJSON', {
value: function () {
var alt = {};
Object.getOwnPropertyNames(this).forEach(function (key) {
alt[key] = this[key];
}, this);
return alt;
},
configurable: true,
writable: true
});
const fs = require('fs');
const path = require('path');
const logger = {
_console : function (string) {
if (this._loggers.indexOf(this.CONSOLE) >= 0) {
console.log(string);
}
},
_file : function (string) {
if (this._loggers.indexOf(this.FILE) >= 0) {
let date = new Date();
let dateStr = date.getFullYear() + '.' + (date.getMonth()+1) + '.' + date.getDate() + '.txt';
fs.appendFile(path.join(this._path, dateStr), date + ' : ' + string);
}
},
_mongo : function (string) {
if (this._loggers.indexOf(this.MONGO) >= 0) {
}
},
debug : function () {
let string = '';
for (var i = 0; i < arguments.length; i++) {
string += JSON.stringify(arguments[i]) + ' ';
}
let str = this._prefix + '\033[34m Debug: \033[30m' + string;
if (this._level >= this.DEBUG) {
this._console(str);
this._file(str);
this._mongo(str);
}
},
info : function (string) {
if (this._level >= this.INFO)
console.log(this._prefix + '\033[32m Info: \033[30m' + string);
info : function () {
let string = '';
for (var i = 0; i < arguments.length; i++) {
string += JSON.stringify(arguments[i]) + ' ';
}
let str = this._prefix + '\033[32m Info: \033[30m' + string;
if (this._level >= this.INFO) {
this._console(str);
this._file(str);
this._mongo(str);
}
},
warning : function (string) {
if (this._level >= this.WARNING)
console.log(this._prefix + '\033[33m Warning: \033[30m' + string);
warning : function () {
let string = '';
for (var i = 0; i < arguments.length; i++) {
string += JSON.stringify(arguments[i]) + ' ';
}
let str = this._prefix + '\033[33m Warning: \033[30m' + string;
if (this._level >= this.WARNING) {
this._console(str);
this._file(str);
this._mongo(str);
}
},
error : function (string) {
if (this._level >= this.ERROR)
console.log(this._prefix + '\033[31m Error: \033[30m' + string);
error : function () {
let string = '';
for (var i = 0; i < arguments.length; i++) {
string += JSON.stringify(arguments[i]) + ' ';
}
let str = this._prefix + '\033[31m Error: \033[30m' + string;
if (this._level >= this.ERROR) {
this._console(str);
this._file(str);
this._mongo(str);
}
},
fatal : function (string) {
if (this._level >= this.FATAL)
console.log(this._prefix + '\033[35m Fatal: \033[30m' + string);
fatal : function () {
let string = '';
for (var i = 0; i < arguments.length; i++) {
string += JSON.stringify(arguments[i]) + ' ';
}
let str = this._prefix + '\033[35m Fatal: \033[30m' + string;
if (this._level >= this.FATAL) {
this._console(str);
this._file(str);
this._mongo(str);
}
},
DEBUG : 4,
......@@ -31,6 +111,15 @@ var logger = {
FATAL : 0,
CONSOLE : -1,
FILE : -2,
MONGO : -3,
_loggers : [],
_path : '',
_connection : '',
_level : this.WARNING,
_prefix : '',
......@@ -40,6 +129,20 @@ var logger = {
addPrefix : function (prefix) {
this._prefix = prefix + ':';
},
addLoger : function (logger, options) {
if (logger === this.MONGO) {
if (!options)
return;
this._connection = options;
} else if (logger === this.FILE) {
if (!options)
return;
this._path = options;
}
if (this._loggers.indexOf(logger) < 0)
this._loggers.push(logger);
}
};
......
{
"name": "nodejs-logger",
"version": "1.0.0",
"version": "1.1.0",
"description": "Logger for mirera node-js server",
"main": "index.js",
"scripts": {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment