-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8490a0f
commit cd5594b
Showing
8 changed files
with
409 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
"use strict"; | ||
|
||
var _ = require('lodash'); | ||
|
||
function mergeValue(target, source) { | ||
if (!_.isObject(target) && !_.isArray(target)) { | ||
target = [target]; | ||
} | ||
if (!_.isObject(source)) { | ||
target.push(source); | ||
return target; | ||
} | ||
if (_.isArray(source)) { | ||
return target.concat(source); | ||
} | ||
for (var prop in source) { | ||
if (!target[prop]) { | ||
target[prop] = source[prop]; | ||
} | ||
else { | ||
if (_.isObject(target[prop]) || _.isObject(source[prop])) { | ||
target[prop] = mergeValue(target[prop], source[prop]); | ||
return target; | ||
} | ||
if (!_.isArray(target[prop])) { | ||
target[prop] = [target[prop]]; | ||
} | ||
if (!_.isArray(source[prop])) { | ||
target[prop].push(source[prop]); | ||
} | ||
else { | ||
target[prop] = [...target[prop], ...source[prop]]; | ||
} | ||
} | ||
} | ||
return target; | ||
} | ||
|
||
module.exports = class MDC { | ||
constructor() { | ||
this._data = {}; | ||
} | ||
|
||
get size() { | ||
return this.length; | ||
} | ||
|
||
get length() { | ||
return Object.keys(this._data).length; | ||
} | ||
|
||
get empty() { | ||
return this.length === 0; | ||
} | ||
|
||
get(name) { | ||
if (name) { | ||
return this._data[name]; | ||
} | ||
return this._data; | ||
} | ||
|
||
set(name, value) { | ||
if (!value && _.isObject(name)) { | ||
this._data = name; | ||
return this; | ||
} | ||
this._data[name] = value; | ||
return this; | ||
} | ||
|
||
delete(name) { | ||
delete this._data[name]; | ||
} | ||
|
||
clear() { | ||
let data = this._data; | ||
this._data = {}; | ||
return data; | ||
} | ||
|
||
merge(name, value) { | ||
if (!value) { | ||
if (_.isObject(name)) { | ||
this._data = mergeValue(this._data, name); | ||
} | ||
} | ||
if (name && value) { | ||
if (!this._data[name]) { | ||
this._data[name] = value; | ||
} else { | ||
this._data[name] = mergeValue(this._data[name], value); | ||
} | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
"use strict"; | ||
|
||
module.exports = class Message { | ||
constructor(level, message, mdc, error) { | ||
this._time = Date.now(); | ||
this._level = level; | ||
this._message = message; | ||
this._mdc = mdc; | ||
this._error = error; | ||
this._entry = process.mainModule; | ||
this._process = process.pid; | ||
} | ||
|
||
/** | ||
* The level of this message. | ||
* @returns {Level} | ||
*/ | ||
get level() { | ||
return this._level; | ||
} | ||
|
||
/** | ||
* The rendered message. | ||
* @returns {string} | ||
*/ | ||
get message() { | ||
return this._message; | ||
} | ||
|
||
/** | ||
* The mete data container with contextual information (if any). | ||
* @returns {object|unknown} | ||
*/ | ||
get mdc() { | ||
return this._mdc; | ||
} | ||
|
||
/** | ||
* Returns the error passed in with the message (if any) | ||
* @returns {Error|unknown} | ||
*/ | ||
get error() { | ||
return this._error; | ||
} | ||
|
||
/** | ||
* Returns the time this message was generated. | ||
* @returns {number|*} | ||
*/ | ||
get time() { | ||
return this._time; | ||
} | ||
|
||
/** | ||
* Returns the entry point of this script / process. | ||
* @returns {string|*} | ||
*/ | ||
get entry() { | ||
return this._entry; | ||
} | ||
|
||
/** | ||
* Returns the process id in which this messages has been generated. | ||
* @returns {boolean|Number|*} | ||
*/ | ||
get process() { | ||
return this._process; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.