Skip to content

Commit

Permalink
My implementation is generic, by design. (#1859)
Browse files Browse the repository at this point in the history
After instantiating a new Debug object with config, a simple JSON object
payload can be passed to the send() function.  If that object's "type"
property is allowed by the current log level, then the output tagged
with metadata and the complete JSON object is passed as a string to
xdmp.log()

A payload type value that is not of an accepted type is rejected.
A payload type value that requires a higher log level than currently specified is rejected.

All rejected payloads return an Error object with message:
"Error: Log level "{this.logLevel}" does not have permission to log type: {payload type}"

If we want, we can add payload validation, ensuring that the payload type has the proper
parameters (ie. error should required a "stack" property in the payload object)

— Edits -

changing function signature to "log", rather than "send"

Debug.log() seems more logical than Debug.send(), so changing function signature.
  • Loading branch information
clockworked247 authored and aebadirad committed Feb 11, 2019
1 parent 4c68766 commit f3765f1
Showing 1 changed file with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
Copyright 2012-2019 MarkLogic Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
'use strict';

export class Debug {
constructor(config) {
this.logLevel = config && config.logLevel || 'default';
this.logLevels = ['default','notice','trace','info'];
this.logTypes = ['error','warning','notice','trace','info'];
}

_addMetadata(payload) {
if (payload) {
payload.currentDateTime = fn.currentDateTime();
payload.user = xdmp.getCurrentUser();
payload.serverID = xdmp.server();
payload.serverName = xdmp.serverName(payload.serverID);
payload.dbID = xdmp.database();
payload.dbName = xdmp.databaseName(payload.dbID);
payload.modulesDbID = xdmp.modulesDatabase();
payload.modulesDbName = xdmp.databaseName(payload.modulesDbID);
}
return payload;
}

_canLog(payload) {
let logLevel = this.logLevel;
let typeCheck = {
'error': function () {
return ['default','notice','trace','info'].includes(logLevel);
},
'warning': function () {
return ['default','notice','trace','info'].includes(logLevel);
},
'notice': function () {
return ['notice','trace','info'].includes(logLevel);
},
'trace': function () {
return ['trace','info'].includes(logLevel);
},
'info': function () {
return ['info'].includes(logLevel);
}
};
return payload.type ? typeCheck[payload.type] && typeCheck[payload.type]() : false;
}

_log(payload) {
payload = this._addMetadata(payload);
xdmp.log(JSON.stringify(payload)); // logging complete JSON
return payload;
}

log(payload) {
return (this._canLog(payload)) ?
this._log(payload) :
new Error('Log level "' + this.logLevel + '" does not have permission to log type: "' + payload.type + '"' || '[unknown]');

// when eventually logging to the database, we
// should return a Promise to be resolved after
// payload has been saved to DB.

// return new Promise((resolve, reject) => {
// resolve(payload);
// })
}
}

0 comments on commit f3765f1

Please sign in to comment.