Comparing version
@@ -179,3 +179,3 @@ "use strict"; | ||
server.on('request', function (request, response) { | ||
logger.print('Create new request', request.url); | ||
logger.info('Bootstrap.init.request: request', request.url); | ||
@@ -199,7 +199,2 @@ // set paths on each request | ||
}.bind(this)); | ||
// server close event | ||
server.on('close', function () { | ||
logger.close(); | ||
logger.destroy(); | ||
}); | ||
@@ -214,4 +209,4 @@ // this must be last ! | ||
// logger | ||
logger.print(env); | ||
logger.print(this.__dynamic__); | ||
logger.info('Bootstrap.init.env', env); | ||
logger.info('Bootstrap.init.args', this.__dynamic__); | ||
@@ -218,0 +213,0 @@ }, |
@@ -38,3 +38,3 @@ "use strict"; | ||
logger.print('Assets.construct', config); | ||
logger.info('Assets.construct:', config); | ||
this.regex = new RegExp(this.config.hook); | ||
@@ -68,3 +68,6 @@ hook.set(this.regex, this.onRequest.bind(this)); | ||
if (!mimeType) { | ||
logger.print('MimeType', mimeType, filePath); | ||
logger.info('Assets.onRequest.mimeType: missing', { | ||
mimeType: mimeType, | ||
filePath: filePath | ||
}); | ||
return this.handleError(function() { | ||
@@ -102,3 +105,6 @@ new error.HttpError(500, {path: filePath}, 'Invalid mime type'); | ||
logger.print('MimeType', mimeType, filePath); | ||
logger.info('Assets.onRequest.mimeType:', { | ||
mimeType: mimeType, | ||
filePath: filePath | ||
}); | ||
resolve(file); | ||
@@ -105,0 +111,0 @@ }); |
@@ -90,3 +90,3 @@ "use strict"; | ||
splits = splits.slice(1, splits.length - 1).map(function (item) { | ||
return item.slice(2, item.length - 2); | ||
return item.slice(1, item.length - 2); | ||
}); | ||
@@ -93,0 +93,0 @@ |
@@ -33,3 +33,3 @@ "use strict"; | ||
this.readFile(); | ||
logger.print('Favicon.construct', config); | ||
logger.info('Favicon.construct:', config); | ||
hook.set(new RegExp(this.config.hook), this.onRequest.bind(this)); | ||
@@ -71,3 +71,5 @@ }, | ||
var path = di.normalizePath(this.config.path); | ||
logger.print('Favicon.readFile', path); | ||
logger.info('Favicon.readFile:', { | ||
path: path | ||
}); | ||
try { | ||
@@ -74,0 +76,0 @@ this.file = fs.readFileSync(path, {encoding: null}); |
@@ -7,5 +7,5 @@ "use strict"; | ||
fs = di.load('fs'), | ||
http = di.load('http'), | ||
core = di.load('core'), | ||
error = di.load('error'), | ||
HttpServer = di.load('core/http'), | ||
replace = [], | ||
@@ -26,2 +26,9 @@ Logger; | ||
* Logger is used to log stuff in application | ||
* | ||
ALL indicates that all messages should be logged. | ||
INFO is a message level for informational messages. | ||
ERROR is a message level indicating a serious failure. | ||
WARNING is a message level indicating a potential problem. | ||
*/ | ||
@@ -33,10 +40,11 @@ Logger = Type.create({ | ||
length: Type.NUMBER, | ||
hooks: Type.ARRAY | ||
file: Type.STRING, | ||
hooks: Type.ARRAY, | ||
logs: Type.ARRAY | ||
}, | ||
{ | ||
_construct: function Logger(config) { | ||
var file; | ||
this.length = 0; | ||
this.stream = this.server = null; | ||
this.hooks = []; | ||
this.logs = []; | ||
this.config = core.extend({ | ||
@@ -47,50 +55,50 @@ enabled: false, | ||
console: false, | ||
readLength: 20000, | ||
readLength: 50000, | ||
port: 9001, | ||
type: 'ALL', | ||
types: ['ALL', 'ERROR', 'INFO', 'WARNING'], | ||
file: "server.log", | ||
level: 5 | ||
}, config); | ||
this.file = di.normalizePath('@{basePath}/' + this.config.file); | ||
if (this.config.write && this.config.enabled) { | ||
file = di.normalizePath('@{basePath}/' + this.config.file); | ||
this.stream = fs.createWriteStream(file, {encoding: 'utf8'}); | ||
this.createStream(); | ||
if (this.config.publish) { | ||
this.createReadLogServer(); | ||
} | ||
} | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method Logger#createStreams | ||
* | ||
* @description | ||
* Create read log server | ||
*/ | ||
createReadLogServer: function Logger_createReadLogServer() { | ||
var that = this; | ||
this.server = new HttpServer(); | ||
this.server.on('request', function (request, response) { | ||
var startMessage = 'LAST '+ that.config.readLength + ' BYTES:\n\n'; | ||
this.server = http.createServer(); | ||
this.server.on('request', function (request, response) { | ||
var len = this.length, | ||
start = len - this.config.readLength, | ||
blen = 0, | ||
buffer, | ||
blenMessage; | ||
fs.open(that.file, 'r', 755, function(status, fd) { | ||
fs.fstat(fd, function (err, stats) { | ||
var size = stats.size, | ||
start = size > that.config.readLength ? size - that.config.readLength : 0, | ||
end = size > that.config.readLength ? that.config.readLength : size, | ||
buffer = new Buffer(end + startMessage.length); | ||
buffer.fill(startMessage); | ||
fs.read(fd, buffer, startMessage.length, end, start, function() { | ||
response.writeHead(200, {'Content-type': 'text/plain', 'Content-Length': buffer.length}); | ||
response.end(buffer); | ||
}); | ||
}); | ||
}); | ||
if (start < 0) { | ||
start = 0; | ||
blen = 1000; | ||
} else { | ||
blen = len - start; | ||
} | ||
}); | ||
blenMessage = 'LAST '+ blen + ' BYTES:\n\n'; | ||
buffer = new Buffer(blen + blenMessage.length, 'utf8'); | ||
this.server.listen(this.config.port); | ||
fs.open(file, 'r', 755, function(status, fd) { | ||
fs.read(fd, buffer, 0, blen, start, function(err) { | ||
if (err) { | ||
var errorMessage = 'Error reading logger buffer'; | ||
response.writeHead(200, {'Content-type': 'text/plain', 'Content-Length': errorMessage.length}); | ||
response.end(errorMessage); | ||
} else { | ||
response.writeHead(200, {'Content-type': 'text/plain', 'Content-Length': buffer.length}); | ||
response.write(blenMessage); | ||
response.end(buffer); | ||
} | ||
}); | ||
}); | ||
}.bind(this)); | ||
this.server.listen(this.config.port); | ||
this.print('Publishing log write stream on port: ' + this.config.port); | ||
} | ||
} | ||
this.info('Publishing log write stream on port: ', this.config.port); | ||
}, | ||
@@ -100,2 +108,13 @@ /** | ||
* @author Igor Ivanovic | ||
* @method Logger#createStreams | ||
* | ||
* @description | ||
* Create streams | ||
*/ | ||
createStream: function Logger_createStream() { | ||
this.stream = fs.createWriteStream(this.file); | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method Logger#addHook | ||
@@ -107,3 +126,3 @@ * | ||
*/ | ||
addHook: function (callback) { | ||
addHook: function Logger_addHook(callback) { | ||
if (!Type.isFunction(callback)) { | ||
@@ -117,14 +136,27 @@ throw new error.Exception('Logger hook must be function'); | ||
* @author Igor Ivanovic | ||
* @method Logger#close | ||
* @method Logger#trace | ||
* | ||
* @description | ||
* Logger close | ||
* Trace log call | ||
*/ | ||
close: function Logger_close() { | ||
if (this.stream) { | ||
this.stream.close(); | ||
trace: function Logger_trace() { | ||
try { | ||
throw new Error(); | ||
} catch (e) { | ||
return core.trim(e.stack.split('\n').slice(3, 4).shift()); | ||
} | ||
if (this.server) { | ||
this.server.close(); | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method Logger#inspect | ||
* | ||
* @description | ||
* Inspect log data output | ||
*/ | ||
inspect: function Logger_inspect(data) { | ||
if (Type.isObject(data)) { | ||
return util.inspect(data, {colors: true, depth: this.config.level}); | ||
} | ||
return data; | ||
}, | ||
@@ -134,63 +166,106 @@ /** | ||
* @author Igor Ivanovic | ||
* @method Logger#log | ||
* @method Logger#write | ||
* | ||
* @description | ||
* Log something in console | ||
* Write to file and exec hooks | ||
*/ | ||
log: function Logger_log() { | ||
var logs = "", | ||
url = '', | ||
date, | ||
args = Array.prototype.slice.call(arguments); | ||
write: function Logger_write() { | ||
var log = this.logs.shift(); | ||
if (!this.config.enabled) { | ||
return args; | ||
if (log && (this.config.type === log.type || this.config.type === 'ALL')) { | ||
if (this.config.console) { | ||
if (log.type === 'ERROR') { | ||
console.error(log); | ||
} else if (log.type === 'INFO') { | ||
console.info(log); | ||
} else if (log.type === 'WARNING') { | ||
console.warn(log); | ||
} else { | ||
console.log(log); | ||
} | ||
} | ||
if (this.stream) { | ||
this.stream.write('TYPE: ' + log.type); | ||
this.stream.write('\n'); | ||
this.stream.write('CREATED: ' + log.created + '\t '); | ||
this.stream.write('\n'); | ||
this.stream.write('MESSAGE: ' + log.message + '\t ' + log.trace); | ||
this.stream.write('\n'); | ||
this.stream.write('DATA: ' + this.clean(log.data)); | ||
this.stream.write('\n'); | ||
this.stream.write('\n'); | ||
} | ||
// call log | ||
this.hooks.forEach(function (callback) { | ||
callback(log); | ||
}); | ||
} | ||
date = new Date().toISOString(); | ||
logs += date + "\n"; | ||
try { | ||
throw new Error(); | ||
} catch (e) { | ||
url = core.trim(e.stack.split('\n').slice(3, 4).shift()); | ||
logs += url + "\n"; | ||
} | ||
args.forEach(function (item) { | ||
logs += core.trim(item); | ||
}); | ||
logs += '\n'; | ||
logs += '\n'; | ||
if (this.config.console) { | ||
console.log(logs); | ||
} | ||
try { | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method Logger#clean | ||
* | ||
* @description | ||
* Clean message for write | ||
* @return string | ||
*/ | ||
clean: function Logger_clean(message) { | ||
if (Type.isString(message)) { | ||
replace.forEach(function (value) { | ||
logs = logs.replace(value, ''); | ||
message = message.replace(value, ''); | ||
}); | ||
logs = logs.replace(/\\'/g, "'"); | ||
logs = logs.replace(/\\n/g, "\n"); | ||
logs = logs.replace(/\\u001b/g, '\u001b'); | ||
} catch (e) { | ||
this.print(e); | ||
message = message.replace(/\\'/g, "'"); | ||
message = message.replace(/\\n/g, "\n"); | ||
return message.replace(/\\u001b/g, '\u001b'); | ||
} | ||
return message; | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method Logger#warn | ||
* | ||
* @description | ||
* Log warn case | ||
*/ | ||
warn: function Logger_warn(message, data) { | ||
return this.log(message, data, 'WARNING'); | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method Logger#info | ||
* | ||
* @description | ||
* Log info case | ||
*/ | ||
info: function Logger_info(message, data) { | ||
return this.log(message, data, 'INFO'); | ||
}, | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method Logger#error | ||
* | ||
* @description | ||
* Log error case | ||
*/ | ||
error: function Logger_info(message, data) { | ||
return this.log(message, data, 'ERROR'); | ||
}, | ||
if (this.config.write && this.stream) { | ||
try { | ||
this.length += logs.length; | ||
this.stream.write(logs); | ||
} catch (e) { | ||
this.print(e); | ||
} | ||
} | ||
this.hooks.forEach(function (hook) { | ||
try { | ||
hook(logs); | ||
} catch (e) { | ||
this.print(e); | ||
} | ||
}.bind(this)); | ||
/** | ||
* @since 0.0.1 | ||
* @author Igor Ivanovic | ||
* @method Logger#print | ||
* @deprecated | ||
* | ||
* @description | ||
* Logger print to support old syntax | ||
*/ | ||
print: function Logger_print(message, data) { | ||
return this.info(message, data); | ||
}, | ||
@@ -200,18 +275,26 @@ /** | ||
* @author Igor Ivanovic | ||
* @method Logger#inspect | ||
* @method Logger#log | ||
* | ||
* @description | ||
* Inspect | ||
* Log something in console | ||
*/ | ||
print: function Logger_print() { | ||
var log = "", | ||
args = Array.prototype.slice.call(arguments); | ||
log: function Logger_log(message, data, type) { | ||
if (!this.config.enabled) { | ||
return args; | ||
return; | ||
} | ||
util.inspect.styles.string = 'green'; | ||
args.forEach(function (item) { | ||
log += " " + util.inspect(item, {colors: true, depth: this.config.level}); | ||
}.bind(this)); | ||
return this.log(log); | ||
if (this.config.types.indexOf(type) === -1) { | ||
type = 'ALL'; | ||
} | ||
this.logs.push({ | ||
type: type, | ||
message: message, | ||
trace: this.trace(), | ||
data: this.inspect(data), | ||
created: new Date().toISOString() | ||
}); | ||
process.nextTick(this.write.bind(this)); | ||
} | ||
@@ -218,0 +301,0 @@ } |
@@ -342,3 +342,5 @@ "use strict"; | ||
logger.print('Request.forward.url', url); | ||
logger.info('Request.forward.url:', { | ||
url: url | ||
}); | ||
@@ -376,3 +378,6 @@ return request.parse(); | ||
logger.print('Request.forward.route', route, params); | ||
logger.info('Request.forward.route:', { | ||
route: route, | ||
params: params | ||
}); | ||
@@ -391,3 +396,6 @@ return request.parse(); | ||
redirect: function Request_redirect(url, isTemp) { | ||
logger.print('Request.redirect', url, isTemp); | ||
logger.info('Request.redirect:', { | ||
url: url, | ||
isTemp: isTemp | ||
}); | ||
this.addHeader('Location', url); | ||
@@ -571,3 +579,3 @@ this.stopPromiseChain(); | ||
// log error request | ||
logger.print('Request.error', { | ||
logger.error('Request:', { | ||
url: this.url, | ||
@@ -577,4 +585,5 @@ status: this.statusCode, | ||
isRendered: this.isRendered, | ||
content_type: this.getHeader('content-type') | ||
}, response); | ||
content_type: this.getHeader('content-type'), | ||
response: response | ||
}); | ||
// set status codes | ||
@@ -653,3 +662,3 @@ if (response.code) { | ||
logger.print('Request.render', { | ||
logger.info('Request.render:', { | ||
url: this.url, | ||
@@ -784,3 +793,3 @@ status: this.statusCode, | ||
logger.print('LoadRequest', { | ||
logger.info('Controller:', { | ||
controller: controller.__dynamic__, | ||
@@ -787,0 +796,0 @@ controllerToLoad: controllerToLoad, |
@@ -82,3 +82,4 @@ "use strict"; | ||
logger.print('Router.add: route', route); | ||
logger.info('Router.add:', route); | ||
this.routes.push(rule); | ||
@@ -85,0 +86,0 @@ }, |
@@ -56,4 +56,2 @@ "use strict"; | ||
logger.print('route', this.route); | ||
if (this.route.indexOf('<') > -1) { | ||
@@ -124,3 +122,3 @@ matches = core.match(ROUTE_MATCH, this.route); | ||
logger.print('RouteRule', { | ||
logger.info('RouteRule.construct:', { | ||
escapePattern: escapePattern, | ||
@@ -127,0 +125,0 @@ escapeRule: escapeRule, |
@@ -104,3 +104,3 @@ "use strict"; | ||
logger.print("View.construct", this.config); | ||
logger.info('View.construct:', this.config); | ||
}, | ||
@@ -162,3 +162,6 @@ /** | ||
setPreloaded: function View_setPreloaded(key, value) { | ||
logger.log('View.setPreloaded: ', key + '\n' + value); | ||
logger.info('View.setPreloaded:', { | ||
key: key, | ||
value: value | ||
}); | ||
this.preloaded[key] = value; | ||
@@ -195,3 +198,3 @@ }, | ||
} catch (e) { | ||
logger.print('View.isFile path is not valid file', {path: path}); | ||
logger.error('View.isFile:', {path: path, e: e}); | ||
} | ||
@@ -214,3 +217,3 @@ return false; | ||
} catch (e) { | ||
logger.print('View.isDir path is not valid path', {path: path}); | ||
logger.error('View.isDir:', {path: path, e: e}); | ||
} | ||
@@ -347,3 +350,3 @@ return false; | ||
} catch (e) { | ||
logger.print('ViewLoader.load.error', { | ||
logger.error('ViewLoader.load:', { | ||
path: path, | ||
@@ -354,3 +357,3 @@ cb: cb, | ||
} finally { | ||
logger.print('ViewLoader.load', { | ||
logger.info('ViewLoader.load:', { | ||
path: path, | ||
@@ -357,0 +360,0 @@ template: template, |
@@ -51,3 +51,3 @@ "use strict"; | ||
this.db = mongoose.connect(this.config.connection, this.config.options); | ||
logger.print('Initialize mongoose', this.db, this.config); | ||
logger.info('Mongo.construct:', this.config); | ||
}, | ||
@@ -74,4 +74,6 @@ | ||
} | ||
logger.print('Mongo.schema:', definition, options); | ||
logger.info('Mongo.schema:', { | ||
definition: definition, | ||
options: options | ||
}); | ||
return new mongoose.Schema(definition, options); | ||
@@ -92,3 +94,5 @@ }, | ||
} | ||
logger.print('Mongo.model: ', name); | ||
logger.info('Mongo.schema:', { | ||
name: name | ||
}); | ||
return mongoose.model(name, schema); | ||
@@ -95,0 +99,0 @@ } |
@@ -41,3 +41,7 @@ "use strict"; | ||
}); | ||
logger.print('RequestHooks.add', key, value.toString()); | ||
logger.info('RequestHooks.set:', { | ||
key: key, | ||
func: value.toString() | ||
}); | ||
}, | ||
@@ -88,3 +92,3 @@ /** | ||
var that = this, route = api.parsedUrl.pathname; | ||
logger.print('RequestHooks.process', route); | ||
logger.info('RequestHooks.process:', route); | ||
return new Promise(function (resolve, reject) { | ||
@@ -94,3 +98,3 @@ var hook; | ||
hook = that.get(route); | ||
logger.print('RequestHooks.hook', hook); | ||
logger.info('RequestHooks.hook:', hook); | ||
if (!!hook) { | ||
@@ -102,3 +106,6 @@ resolve(hook.func(api)); | ||
} catch (e) { | ||
logger.print('RequestHooks.hook error', e); | ||
logger.error('RequestHooks.hook:', { | ||
hook: hook, | ||
e: e | ||
}); | ||
reject(new error.HttpError(500, {hook: route}, 'Hook error', e)); | ||
@@ -105,0 +112,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"description": "Powerful lightweight mvc framework for nodejs", | ||
"version": "0.1.0-beta-68", | ||
"version": "0.1.0-beta-69", | ||
"dependencies" : { | ||
@@ -8,0 +8,0 @@ "mongoose": "3.8.x", |
154758
1.91%4981
2.2%