Skip to content

Commit

Permalink
flat logs
Browse files Browse the repository at this point in the history
  • Loading branch information
rjrodger committed Aug 30, 2019
1 parent aee094a commit a074c22
Show file tree
Hide file tree
Showing 9 changed files with 589 additions and 49 deletions.
23 changes: 13 additions & 10 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ exports.close = function(callpoint) {
seneca.log.debug({
kind: 'close',
notice: 'start',
callpoint: callpoint()
callpoint: callpoint(true)
})

seneca.act('role:seneca,cmd:close,closing$:true', function(err) {
Expand Down Expand Up @@ -201,9 +201,10 @@ exports.delegate = function(fixedargs, fixedmeta) {
(delegate.did ? delegate.did + '/' : '') + self.private$.didnid()


delegate.logx = function logx() { return root.logx.apply(delegate, arguments)}
Object.assign(delegate.logx,root.logx)
delegate.logx.self = () => delegate
delegate.log = function log() { return root.log.apply(delegate, arguments)}
Object.assign(delegate.log,root.log)
delegate.log.self = () => delegate


var strdesc
delegate.toString = function toString() {
Expand Down Expand Up @@ -559,10 +560,11 @@ exports.listen = function(callpoint) {
done = _.noop
}

self.log.debug({
self.log.info({
kind: 'listen',
options: argsarr,
callpoint: callpoint()
case: 'INIT',
data: argsarr,
callpoint: callpoint(true)
})

var opts = self.options().transport || {}
Expand Down Expand Up @@ -592,10 +594,11 @@ exports.client = function(callpoint) {
var argsarr = Array.prototype.slice.call(arguments)
var self = this

self.log.debug({
self.log.info({
kind: 'client',
options: argsarr,
callpoint: callpoint()
case: 'INIT',
data: argsarr,
callpoint: callpoint(true)
})

var legacy = self.options().legacy || {}
Expand Down
96 changes: 82 additions & 14 deletions lib/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,56 @@ const level_abbrev = logging.level_abbrev = {

// TODO: create concise production flat format
function flat_logger(entry) {
Print.log('PRINT:', entry)

//console.log(entry)

var level_str = entry.level_name.toUpperCase()
if(level_str.length < 5) {
level_str += '_'.repeat(5-level_str.length)
}
level_str = level_str.substring(0,5)

var da =
null != entry.err ? [entry.err.message, entry.err.callpoint] :
null != entry.res ? [entry.res] :
null != entry.msg ? [entry.msg] :
Array.isArray(entry.data) ? entry.data :
null != entry.data ? [entry.data] :
[]

for(var i = 0; i < da.length; i++) {
da[i] = 'object' === typeof(da[i]) ? Common.clean(da[i]) : da[i]
da[i] = Util.inspect(da[i],{
compact:true,
depth:(entry.depth||3),
breakLength:Infinity
})
}

var data_str = da.join(' ')

var plugin_str = null==entry.plugin_name?'':
(entry.plugin_name+
((null==entry.plugin_tag||'-'==entry.plugin_tag)?'':'$'+entry.plugin_tag))

var sb = [
entry.isot,
entry.seneca_id,
level_str,
(null==entry.kind?'log':entry.kind),
(null==entry.case?'LOG':entry.case),
plugin_str,
(null==entry.pattern?'':entry.pattern),
(null==entry.action?'':entry.action),
(null==entry.idpath?'':entry.idpath),
data_str,
entry.callpoint ? Util.inspect(entry.callpoint) : ''
]

console.log(sb.join(' ').substring(0,entry.maxlen||11111))
}


// TODO: make default
function json_logger(entry) {
var logstr = Stringify(entry)
Expand Down Expand Up @@ -112,7 +159,8 @@ function load_logger(instance, log_plugin) {



function build_log_spec(self) {
function build_log_spec(self,flags) {
flags = flags || {}
var options = self.options()
var orig_logspec = options.log

Expand Down Expand Up @@ -210,19 +258,22 @@ function build_log_spec(self) {
log:logspec
}

self.options(logopts)

if(false !== flags.set_options) {
self.options(logopts)
}

//console.log('BUILD LOGSPEC OUT', logopts)

return logspec
}


function build_log(self) {
var logspec = build_log_spec(self)
function build_log(self,flags) {
var logspec = build_log_spec(self,flags)
//console.log('LOGSPEC', logspec)

// shortcut for direct access (avoids seneca.options() call)
//self.private$.optioner.set({log:logspec})
self.private$.logspec = logspec

var logger = load_logger(self, logspec.logger)
Expand All @@ -233,13 +284,18 @@ function build_log(self) {

self.log = function log(entry) {
var instance = this

// Handle legacy entity call
if(instance.entity$) {
instance = instance.private$.get_instance()
entry = {data:arguments}
entry = {data:Array.prototype.slice.call(arguments)}
}
else if('string' === typeof(entry)) {
entry = {data:Array.prototype.slice.call(arguments)}
}

//console.log('ENTRY',instance,entry)

var logspec = instance.private$.logspec
entry.level = entry.level || logspec.default_level

Expand Down Expand Up @@ -267,9 +323,20 @@ function build_log(self) {
entry.plugin_tag = entry.plugin_tag || instance.fixedargs.plugin$.tag
}

//console.log('IPA', instance.private$.act)

if(instance.private$.act) {
entry.kind = entry.kind || 'act'
entry.actid = entry.actid || instance.private$.act.meta.id
entry.pattern = entry.pattern || instance.private$.act.def.pattern
entry.pattern = entry.pattern || instance.private$.act.meta.pattern
entry.action = entry.action || instance.private$.act.def.id

entry.idpath = instance.private$.act.meta.tx
for(var i = 0; i < (instance.private$.act.meta.parents || []).length; i++) {
entry.idpath += '.'+(instance.private$.act.meta.parents[i][1].split('/')[0])
}
entry.idpath += '.'+instance.private$.act.meta.mi
//console.log(instance.private$.act.meta)
}

// Log event is called on all logs - they are not filtered by level
Expand Down Expand Up @@ -297,13 +364,14 @@ function make_log_level(level_name, logspec) {

var log_level = function(entry) {
var self = this.self()
if('object' === typeof(entry)) {
entry.level = level
}
else {
entry = {level:level, data:entry}
if('object' !== typeof(entry)) {
entry = {
data:Array.prototype.slice.call(arguments)
}
}

entry.level = level

return self.log(entry)
}

Expand Down
10 changes: 8 additions & 2 deletions lib/outward.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,14 @@ function outward_res_object(ctxt, data) {
}

function outward_announce(ctxt, data) {
if (!ctxt.actdef) return
// if (!ctxt.actdef) return

var meta = data.meta

if(meta.error) {
return
}

if (_.isFunction(ctxt.seneca.on_act_out)) {
ctxt.seneca.on_act_out(ctxt.actdef, data.res, data.meta)
}
Expand All @@ -145,7 +151,7 @@ function outward_announce(ctxt, data) {
kind: 'act',
case: 'OUT',
duration: ctxt.duration,
result: data.res,
res: data.res,
did: ctxt.seneca.did
})
)
Expand Down
10 changes: 6 additions & 4 deletions lib/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ module.exports.register = function(opts, callpoint) {
var seneca = this
var so = seneca.options()

var define_callpoint = callpoint(true)

// NOTE: `define` is the property for the plugin definition action.
// The property `init` will be deprecated in 4.x
plugin.define = plugin.define || plugin.init
Expand Down Expand Up @@ -93,11 +95,11 @@ module.exports.register = function(opts, callpoint) {

plugin_seneca.log.debug({
kind: 'plugin',
case: 'define',
case: 'DEFINE',
name: plugin.name,
tag: plugin.tag,
options: plugin_options,
callpoint: callpoint
callpoint: define_callpoint
})

var meta = define_plugin(
Expand Down Expand Up @@ -150,7 +152,7 @@ module.exports.register = function(opts, callpoint) {

plugin_seneca.log.debug({
kind: 'plugin',
case: 'init',
case: 'INIT',
name: plugin.name,
tag: plugin.tag,
exports: exports
Expand Down Expand Up @@ -191,7 +193,7 @@ module.exports.register = function(opts, callpoint) {

plugin_seneca.log.info({
kind: 'plugin',
case: 'ready',
case: 'READY',
name: plugin.name,
tag: plugin.tag
})
Expand Down
Loading

0 comments on commit a074c22

Please sign in to comment.