2019-03-24 08:27:49 +01:00

105 lines
2.3 KiB
JavaScript

/**
* File: app/logging.js
* Author: Gerrit Linnemann
*
* Logging with several log-level.
* - debug: Printed only in development-state.
* - log: Needed log-output.
* - error: Prints log in error-level.
* - inspect: Prints a message and details of an object.
*/
// load the things we need
var Conf = require('../config.json')
, Inspect = require('eyespect').inspector()
, Helper = require('./helper.js')
, DateFormat = require('dateformat');
var eyeFriendly = (Conf.logging.mode === 'friendly');
var isTrace = (Conf.logging.trace === 'yes');
var isDebug = true;
exports.init = function(express) {
return this;
}
exports.isEyeFriendlyMode = function() {
return eyeFriendly;
}
exports.isTrace = function() {
return isTrace;
}
exports.isDebug = function() {
return isDebug;
}
exports.trace = function(msg) {
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " TRACE " + msg;
if(isTrace) {
consoleOut(msg);
}
}
exports.debug = function(msg) {
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " DEBUG " + msg;
if(isDebug) {
consoleOut(msg);
}
}
exports.log = function(msg) {
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " INFO " + msg;
consoleOut(msg);
}
exports.error = function(msg) {
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " ERROR " + msg;
consoleErr(msg);
}
exports.inspect = function(msg, content) {
msg = DateFormat(new Date(), "yyyy-mm-dd h:MM:ss TT") + " INSPECT " + msg;
if(eyeFriendly) {
Inspect(content, msg);
} else {
var msgOut = msg + (Helper.isDefinedAndNotNull(content) ? ": "+JSON.stringify(content) : "");
consoleOut(msgOut);
}
}
/* private */
function consoleOut(msg, content) {
var hasContent = Helper.isDefinedAndNotNull(content);
if(hasContent) {
var msgAddon = (hasContent ? " | " + content : "");
if(Helper.isDefinedAndNotNull(msg)) {
console.log(msg + msgAddon);
}
} else if(Helper.isDefinedAndNotNull(msg)) {
console.log(msg);
}
}
/* private */
function consoleErr(msg, content) {
var hasContent = Helper.isDefinedAndNotNull(content);
if(hasContent) {
var msgAddon = (hasContent ? " | " + content : "");
if(Helper.isDefinedAndNotNull(msg)) {
console.error(msg + msgAddon);
}
} else if(Helper.isDefinedAndNotNull(msg)) {
console.error(msg);
}
}