@blacklane/kiev-js
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -12,4 +12,18 @@ declare enum LogLevel { | ||
environment: string; | ||
constructor(application: string, environment: string); | ||
fields: Object; | ||
constructor(application: string, environment: string, fields?: Object); | ||
/** | ||
* Sets the fields to be logged on every log in addition to the payload | ||
* passed to any of the log methods. If any key was already set, it'll be | ||
* overridden. | ||
* @param {Object} fields to be added to all log entries. | ||
*/ | ||
setFields(fields: Object): void; | ||
/** | ||
* Returns a new logger which fields is the union of this logger fields and | ||
* the fields parameter. | ||
* @param {Object} fields to be added to all log entries. | ||
*/ | ||
extend(fields: Object): Logger; | ||
/** | ||
* Changes the current logger level. | ||
@@ -22,3 +36,2 @@ * | ||
* Returns the current logger level. | ||
* | ||
*/ | ||
@@ -29,3 +42,4 @@ getLevel(): LogLevel | null; | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -37,3 +51,4 @@ */ | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -45,3 +60,4 @@ */ | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -53,3 +69,4 @@ */ | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -61,3 +78,4 @@ */ | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -64,0 +82,0 @@ */ |
@@ -35,7 +35,35 @@ 'use strict'; | ||
var Logger = /*#__PURE__*/function () { | ||
function Logger(application, environment) { | ||
function Logger(application, environment, fields) { | ||
if (fields === void 0) { | ||
fields = {}; | ||
} | ||
this.application = application; | ||
this.environment = environment; | ||
this.fields = fields; | ||
} | ||
/** | ||
* Sets the fields to be logged on every log in addition to the payload | ||
* passed to any of the log methods. If any key was already set, it'll be | ||
* overridden. | ||
* @param {Object} fields to be added to all log entries. | ||
*/ | ||
var _proto = Logger.prototype; | ||
_proto.setFields = function setFields(fields) { | ||
this.fields = _extends({}, this.fields, fields); | ||
} | ||
/** | ||
* Returns a new logger which fields is the union of this logger fields and | ||
* the fields parameter. | ||
* @param {Object} fields to be added to all log entries. | ||
*/ | ||
; | ||
_proto.extend = function extend(fields) { | ||
return new Logger(this.application, this.environment, _extends({}, this.fields, fields)); | ||
} | ||
/** | ||
* Changes the current logger level. | ||
@@ -45,6 +73,4 @@ * | ||
*/ | ||
; | ||
var _proto = Logger.prototype; | ||
_proto.setLevel = function setLevel(level) { | ||
@@ -55,3 +81,2 @@ logger.setLevel(level); | ||
* Returns the current logger level. | ||
* | ||
*/ | ||
@@ -79,3 +104,4 @@ ; | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -95,3 +121,4 @@ */ | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -111,3 +138,4 @@ */ | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -127,3 +155,4 @@ */ | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -143,3 +172,4 @@ */ | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -173,3 +203,3 @@ */ | ||
}; | ||
return JSON.stringify(_extends({}, logEvent, payload)); | ||
return JSON.stringify(_extends({}, logEvent, this.fields, payload)); | ||
}; | ||
@@ -176,0 +206,0 @@ |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("loglevel");function o(){return(o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var o=arguments[t];for(var r in o)Object.prototype.hasOwnProperty.call(o,r)&&(e[r]=o[r])}return e}).apply(this,arguments)}(e=exports.LogLevel||(exports.LogLevel={})).TRACE="TRACE",e.DEBUG="DEBUG",e.INFO="INFO",e.WARN="WARN",e.ERROR="ERROR",e.SILENT="SILENT",exports.Logger=function(){function e(e,t){this.application=e,this.environment=t}var r=e.prototype;return r.setLevel=function(e){t.setLevel(e)},r.getLevel=function(){for(var e=null,o=0,r=Object.entries(t.levels);o<r.length;o++){var n=r[o],i=n[0],l=n[1];if(t.getLevel()===l){e=i;break}}return e},r.debug=function(e,o){void 0===o&&(o={}),t.debug(this._buildPayload(exports.LogLevel.DEBUG,e,o))},r.info=function(e,o){void 0===o&&(o={}),t.info(this._buildPayload(exports.LogLevel.INFO,e,o))},r.warn=function(e,o){void 0===o&&(o={}),t.warn(this._buildPayload(exports.LogLevel.WARN,e,o))},r.error=function(e,o){void 0===o&&(o={}),t.error(this._buildPayload(exports.LogLevel.ERROR,e,o))},r.trace=function(e,o){void 0===o&&(o={}),t.trace(this._buildPayload(exports.LogLevel.TRACE,e,o))},r._buildPayload=function(e,t,r){var n={application:this.application,environment:this.environment,level:e,message:t,timestamp:(new Date).toISOString()};return JSON.stringify(o({},n,r))},e}(); | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("loglevel");function i(){return(i=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var i=arguments[t];for(var n in i)Object.prototype.hasOwnProperty.call(i,n)&&(e[n]=i[n])}return e}).apply(this,arguments)}(e=exports.LogLevel||(exports.LogLevel={})).TRACE="TRACE",e.DEBUG="DEBUG",e.INFO="INFO",e.WARN="WARN",e.ERROR="ERROR",e.SILENT="SILENT",exports.Logger=function(){function e(e,t,i){void 0===i&&(i={}),this.application=e,this.environment=t,this.fields=i}var n=e.prototype;return n.setFields=function(e){this.fields=i({},this.fields,e)},n.extend=function(t){return new e(this.application,this.environment,i({},this.fields,t))},n.setLevel=function(e){t.setLevel(e)},n.getLevel=function(){for(var e=null,i=0,n=Object.entries(t.levels);i<n.length;i++){var o=n[i],r=o[0],l=o[1];if(t.getLevel()===l){e=r;break}}return e},n.debug=function(e,i){void 0===i&&(i={}),t.debug(this._buildPayload(exports.LogLevel.DEBUG,e,i))},n.info=function(e,i){void 0===i&&(i={}),t.info(this._buildPayload(exports.LogLevel.INFO,e,i))},n.warn=function(e,i){void 0===i&&(i={}),t.warn(this._buildPayload(exports.LogLevel.WARN,e,i))},n.error=function(e,i){void 0===i&&(i={}),t.error(this._buildPayload(exports.LogLevel.ERROR,e,i))},n.trace=function(e,i){void 0===i&&(i={}),t.trace(this._buildPayload(exports.LogLevel.TRACE,e,i))},n._buildPayload=function(e,t,n){var o={application:this.application,environment:this.environment,level:e,message:t,timestamp:(new Date).toISOString()};return JSON.stringify(i({},o,this.fields,n))},e}(); | ||
//# sourceMappingURL=kiev-js.cjs.production.min.js.map |
@@ -33,7 +33,35 @@ import { setLevel, levels, getLevel, debug, info, warn, error, trace } from 'loglevel'; | ||
var Logger = /*#__PURE__*/function () { | ||
function Logger(application, environment) { | ||
function Logger(application, environment, fields) { | ||
if (fields === void 0) { | ||
fields = {}; | ||
} | ||
this.application = application; | ||
this.environment = environment; | ||
this.fields = fields; | ||
} | ||
/** | ||
* Sets the fields to be logged on every log in addition to the payload | ||
* passed to any of the log methods. If any key was already set, it'll be | ||
* overridden. | ||
* @param {Object} fields to be added to all log entries. | ||
*/ | ||
var _proto = Logger.prototype; | ||
_proto.setFields = function setFields(fields) { | ||
this.fields = _extends({}, this.fields, fields); | ||
} | ||
/** | ||
* Returns a new logger which fields is the union of this logger fields and | ||
* the fields parameter. | ||
* @param {Object} fields to be added to all log entries. | ||
*/ | ||
; | ||
_proto.extend = function extend(fields) { | ||
return new Logger(this.application, this.environment, _extends({}, this.fields, fields)); | ||
} | ||
/** | ||
* Changes the current logger level. | ||
@@ -43,6 +71,4 @@ * | ||
*/ | ||
; | ||
var _proto = Logger.prototype; | ||
_proto.setLevel = function setLevel$1(level) { | ||
@@ -53,3 +79,2 @@ setLevel(level); | ||
* Returns the current logger level. | ||
* | ||
*/ | ||
@@ -77,3 +102,4 @@ ; | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -93,3 +119,4 @@ */ | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -109,3 +136,4 @@ */ | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -125,3 +153,4 @@ */ | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -141,3 +170,4 @@ */ | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -171,3 +201,3 @@ */ | ||
}; | ||
return JSON.stringify(_extends({}, logEvent, payload)); | ||
return JSON.stringify(_extends({}, logEvent, this.fields, payload)); | ||
}; | ||
@@ -174,0 +204,0 @@ |
{ | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
@@ -19,6 +19,6 @@ # Kiev-js | ||
and then import the logger and use it as the example below. | ||
### Usage | ||
```javascript | ||
import { Logger, LogLevel } from '@blacklane/kiev-js' | ||
import {Logger, LogLevel} from '@blacklane/kiev-js' | ||
@@ -28,3 +28,4 @@ const environment = process.env.NODE_ENV || 'development' | ||
// Default level is 'warn' | ||
logger = new Logger('application-name', environment) | ||
// The last parameter is optional and it'll be added to all log entries | ||
logger = new Logger('application-name', environment, { foo: 'foo', bar: 'bar' }) | ||
@@ -35,5 +36,6 @@ // This won't be logged due to the default level | ||
// Next line will be logged | ||
// The payload will override any field defined in the constructor | ||
logger.warn('WARN! Look at this', { foo: 'bar' }) | ||
// => {"application":"application-name","environment":"development","level":"ERROR", message: "WARN! Look at this", "timestamp":"2020-10-15T10:51:32.621Z", "foo": "bar"} | ||
// => {"application":"application-name","environment":"development","level":"WARN", message: "WARN! Look at this", "timestamp":"2020-10-15T10:51:32.621Z", "foo": "bar", "bar": "bar"} | ||
@@ -46,3 +48,10 @@ | ||
// => {"application":"application-name","environment":"development","level":"DEBUG", message: "FooBar", "timestamp":"2020-10-15T10:51:32.621Z", "fizz": "buzz"} | ||
// => {"application":"application-name","environment":"development","level":"DEBUG", message: "FooBar", "timestamp":"2020-10-15T10:51:32.621Z", "fizz": "buzz", "foo": "foo", "bar": "bar"} | ||
// Create a new logger based on the current one | ||
const newLogger = logger.extend({ tracking_id: "an tracking ID" }) | ||
// tracking_id will be present on all logs produced by this logger | ||
newLogger.info('GET / - 200 OK') | ||
// => {"application":"application-name","environment":"development","level":"INFO", message: "GET / - 200 OK", "timestamp":"2020-10-15T10:51:32.621Z", "foo": "foo", "bar": "bar", "tracking_id": "an tracking ID"} | ||
``` | ||
@@ -49,0 +58,0 @@ |
@@ -47,2 +47,85 @@ import * as logLevel from 'loglevel' | ||
describe('constructor with fields', () => { | ||
beforeEach(() => { | ||
mockLogLevel.info.mockClear() | ||
}) | ||
it('passing fields to the constructor', () => { | ||
expect.assertions(4) | ||
const trackingId = 'passing fields to the constructor' | ||
const log = new Logger(applicationName, environment, { | ||
tracking_id: trackingId | ||
}) | ||
log.info(logMessage, logPayload) | ||
const message = JSON.parse(mockLogLevel.info.mock.calls[0][0]) | ||
expect(message.application).toStrictEqual(applicationName) | ||
expect(message.environment).toStrictEqual(environment) | ||
expect(message.tracking_id).toStrictEqual(trackingId) | ||
expect(message).toMatchObject(logPayload) | ||
}) | ||
it('setFields', () => { | ||
expect.assertions(1) | ||
const foo = 'foo' | ||
const bar = 'bar' | ||
const log = new Logger(applicationName, environment, { foo }) | ||
log.setFields({ foo: bar }) | ||
log.info(logMessage) | ||
const message = JSON.parse(mockLogLevel.info.mock.calls[0][0]) | ||
expect(message.foo).toStrictEqual(bar) | ||
}) | ||
it('payload overwrites fields for a single log', () => { | ||
expect.assertions(2) | ||
const trackingIdOld = 'old' | ||
const trackingId = 'new tracking id' | ||
const log = new Logger(applicationName, environment, { | ||
tracking_id: trackingIdOld | ||
}) | ||
log.info(logMessage, { tracking_id: trackingId }) | ||
const message1 = JSON.parse(mockLogLevel.info.mock.calls[0][0]) | ||
log.info(logMessage) | ||
const message2 = JSON.parse(mockLogLevel.info.mock.calls[1][0]) | ||
expect(message1.tracking_id).toStrictEqual(trackingId) | ||
expect(message2.tracking_id).toStrictEqual(trackingIdOld) | ||
}) | ||
}) | ||
describe('extend', () => { | ||
beforeEach(() => { | ||
mockLogLevel.info.mockClear() | ||
}) | ||
it('extend', () => { | ||
expect.assertions(2) | ||
const trackingId = 'tracking id' | ||
const parent = new Logger(applicationName, environment, { | ||
tracking_id: trackingId | ||
}) | ||
const extended = parent.extend({ extend: 'extend' }) | ||
parent.info(logMessage) | ||
const message1 = JSON.parse(mockLogLevel.info.mock.calls[0][0]) | ||
extended.info(logMessage) | ||
const message2 = JSON.parse(mockLogLevel.info.mock.calls[1][0]) | ||
expect(message1.tracking_id).toStrictEqual(trackingId) | ||
expect(message2.extend).toStrictEqual('extend') | ||
}) | ||
}) | ||
describe('debug', () => { | ||
@@ -49,0 +132,0 @@ beforeEach(() => { |
@@ -23,9 +23,33 @@ import * as logger from 'loglevel' | ||
environment: string | ||
fields: Object | ||
constructor (application: string, environment: string) { | ||
constructor (application: string, environment: string, fields: Object = {}) { | ||
this.application = application | ||
this.environment = environment | ||
this.fields = fields | ||
} | ||
/** | ||
* Sets the fields to be logged on every log in addition to the payload | ||
* passed to any of the log methods. If any key was already set, it'll be | ||
* overridden. | ||
* @param {Object} fields to be added to all log entries. | ||
*/ | ||
public setFields (fields: Object) { | ||
this.fields = { ...this.fields, ...fields } | ||
} | ||
/** | ||
* Returns a new logger which fields is the union of this logger fields and | ||
* the fields parameter. | ||
* @param {Object} fields to be added to all log entries. | ||
*/ | ||
public extend (fields: Object): Logger { | ||
return new Logger(this.application, this.environment, { | ||
...this.fields, | ||
...fields | ||
}) | ||
} | ||
/** | ||
* Changes the current logger level. | ||
@@ -41,3 +65,2 @@ * | ||
* Returns the current logger level. | ||
* | ||
*/ | ||
@@ -59,3 +82,4 @@ public getLevel () { | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -70,3 +94,4 @@ */ | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -81,3 +106,4 @@ */ | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -92,3 +118,4 @@ */ | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -103,3 +130,4 @@ */ | ||
* | ||
* @param {String} message is the core information detail of your log entry. By reading it anyone should understand what the log entry is about. | ||
* @param {String} message is the core information detail of your log entry. | ||
* By reading it anyone should understand what the log entry is about. | ||
* @param {object} [payload] is a JSON object, usually a request or event payload. | ||
@@ -131,3 +159,3 @@ */ | ||
return JSON.stringify({ ...logEvent, ...payload }) | ||
return JSON.stringify({ ...logEvent, ...this.fields, ...payload }) | ||
} | ||
@@ -134,0 +162,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
56087
806
86