@blacklane/kiev-js
Advanced tools
Comparing version 1.2.0 to 2.0.0
@@ -1,2 +0,3 @@ | ||
declare enum LogLevel { | ||
import { AttributesFilter } from './filter'; | ||
export declare enum LogLevel { | ||
TRACE = "TRACE", | ||
@@ -9,7 +10,14 @@ DEBUG = "DEBUG", | ||
} | ||
declare class Logger { | ||
export interface LoggerConfig { | ||
application: string; | ||
environment: string; | ||
fields: Object; | ||
constructor(application: string, environment: string, fields?: Object); | ||
filterFields?: string[]; | ||
initializedFields?: Object; | ||
} | ||
export declare class Logger { | ||
application: string; | ||
environment: string; | ||
fields?: Object; | ||
filter: AttributesFilter; | ||
constructor(config: LoggerConfig); | ||
/** | ||
@@ -87,2 +95,1 @@ * Sets the fields to be logged on every log in addition to the payload | ||
} | ||
export { Logger, LogLevel }; |
@@ -25,2 +25,55 @@ 'use strict'; | ||
var AttributesFilter = /*#__PURE__*/function () { | ||
function AttributesFilter(filters) { | ||
if (filters === void 0) { | ||
filters = []; | ||
} | ||
this.filteredText = '[FILTERED]'; | ||
this.filters = filters; | ||
} | ||
/** | ||
* Returns a new Object with the keys which match the filters wit data filtered. | ||
* | ||
* @param {Object} attrs to be filtered | ||
* @return {Object} a new Object with filtered data | ||
*/ | ||
var _proto = AttributesFilter.prototype; | ||
_proto.run = function run(attrs) { | ||
return this.recursiveFilter(attrs); | ||
}; | ||
_proto.recursiveFilter = function recursiveFilter(attrs) { | ||
var _this = this; | ||
var map = new Map(); | ||
Object.entries(attrs).forEach(function (pair) { | ||
var key = pair[0], | ||
value = pair[1]; | ||
if (_this.typeOfObject(value)) { | ||
map.set(key, _this.recursiveFilter(value)); | ||
} else if (_this.shouldKeep(key)) { | ||
map.set(key, value); | ||
} else { | ||
map.set(key, _this.filteredText); | ||
} | ||
}); | ||
return Object.fromEntries(map); | ||
}; | ||
_proto.typeOfObject = function typeOfObject(value) { | ||
return typeof value === 'object'; | ||
}; | ||
_proto.shouldKeep = function shouldKeep(key) { | ||
return !this.filters.includes(key); | ||
}; | ||
return AttributesFilter; | ||
}(); | ||
(function (LogLevel) { | ||
@@ -36,10 +89,7 @@ LogLevel["TRACE"] = "TRACE"; | ||
var Logger = /*#__PURE__*/function () { | ||
function Logger(application, environment, fields) { | ||
if (fields === void 0) { | ||
fields = {}; | ||
} | ||
this.application = application; | ||
this.environment = environment; | ||
this.fields = fields; | ||
function Logger(config) { | ||
this.application = config.application; | ||
this.environment = config.environment; | ||
this.fields = config.initializedFields; | ||
this.filter = new AttributesFilter(config.filterFields); | ||
} | ||
@@ -67,3 +117,8 @@ /** | ||
_proto.extend = function extend(fields) { | ||
return new Logger(this.application, this.environment, _extends({}, this.fields, fields)); | ||
var config = { | ||
application: this.application, | ||
environment: this.environment, | ||
initializedFields: _extends({}, this.fields, fields) | ||
}; | ||
return new Logger(config); | ||
} | ||
@@ -191,3 +246,5 @@ /** | ||
_proto._buildPayload = function _buildPayload(severity, message, payload) { | ||
var logEvent = { | ||
var filteredPayload = this.filter.run(payload); | ||
var log = _extends({ | ||
application: this.application, | ||
@@ -198,4 +255,5 @@ environment: this.environment, | ||
timestamp: new Date().toISOString() | ||
}; | ||
return JSON.stringify(_extends({}, logEvent, this.fields, payload)); | ||
}, this.fields, filteredPayload); | ||
return JSON.stringify(_extends({}, log)); | ||
}; | ||
@@ -202,0 +260,0 @@ |
@@ -1,2 +0,2 @@ | ||
"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}(); | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("loglevel");function t(){return(t=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)}var i,n=function(){function e(e){void 0===e&&(e=[]),this.filteredText="[FILTERED]",this.filters=e}var t=e.prototype;return t.run=function(e){return this.recursiveFilter(e)},t.recursiveFilter=function(e){var t=this,i=new Map;return Object.entries(e).forEach((function(e){var n=e[0],r=e[1];t.typeOfObject(r)?i.set(n,t.recursiveFilter(r)):t.shouldKeep(n)?i.set(n,r):i.set(n,t.filteredText)})),Object.fromEntries(i)},t.typeOfObject=function(e){return"object"==typeof e},t.shouldKeep=function(e){return!this.filters.includes(e)},e}();(i=exports.LogLevel||(exports.LogLevel={})).TRACE="TRACE",i.DEBUG="DEBUG",i.INFO="INFO",i.WARN="WARN",i.ERROR="ERROR",i.SILENT="SILENT",exports.Logger=function(){function i(e){this.application=e.application,this.environment=e.environment,this.fields=e.initializedFields,this.filter=new n(e.filterFields)}var r=i.prototype;return r.setFields=function(e){this.fields=t({},this.fields,e)},r.extend=function(e){return new i({application:this.application,environment:this.environment,initializedFields:t({},this.fields,e)})},r.setLevel=function(t){e.setLevel(t)},r.getLevel=function(){for(var t=null,i=0,n=Object.entries(e.levels);i<n.length;i++){var r=n[i],o=r[0],s=r[1];if(e.getLevel()===s){t=o;break}}return t},r.debug=function(t,i){void 0===i&&(i={}),e.debug(this._buildPayload(exports.LogLevel.DEBUG,t,i))},r.info=function(t,i){void 0===i&&(i={}),e.info(this._buildPayload(exports.LogLevel.INFO,t,i))},r.warn=function(t,i){void 0===i&&(i={}),e.warn(this._buildPayload(exports.LogLevel.WARN,t,i))},r.error=function(t,i){void 0===i&&(i={}),e.error(this._buildPayload(exports.LogLevel.ERROR,t,i))},r.trace=function(t,i){void 0===i&&(i={}),e.trace(this._buildPayload(exports.LogLevel.TRACE,t,i))},r._buildPayload=function(e,i,n){var r=this.filter.run(n),o=t({application:this.application,environment:this.environment,level:e,message:i,timestamp:(new Date).toISOString()},this.fields,r);return JSON.stringify(t({},o))},i}(); | ||
//# sourceMappingURL=kiev-js.cjs.production.min.js.map |
@@ -21,2 +21,55 @@ import { setLevel, levels, getLevel, debug, info, warn, error, trace } from 'loglevel'; | ||
var AttributesFilter = /*#__PURE__*/function () { | ||
function AttributesFilter(filters) { | ||
if (filters === void 0) { | ||
filters = []; | ||
} | ||
this.filteredText = '[FILTERED]'; | ||
this.filters = filters; | ||
} | ||
/** | ||
* Returns a new Object with the keys which match the filters wit data filtered. | ||
* | ||
* @param {Object} attrs to be filtered | ||
* @return {Object} a new Object with filtered data | ||
*/ | ||
var _proto = AttributesFilter.prototype; | ||
_proto.run = function run(attrs) { | ||
return this.recursiveFilter(attrs); | ||
}; | ||
_proto.recursiveFilter = function recursiveFilter(attrs) { | ||
var _this = this; | ||
var map = new Map(); | ||
Object.entries(attrs).forEach(function (pair) { | ||
var key = pair[0], | ||
value = pair[1]; | ||
if (_this.typeOfObject(value)) { | ||
map.set(key, _this.recursiveFilter(value)); | ||
} else if (_this.shouldKeep(key)) { | ||
map.set(key, value); | ||
} else { | ||
map.set(key, _this.filteredText); | ||
} | ||
}); | ||
return Object.fromEntries(map); | ||
}; | ||
_proto.typeOfObject = function typeOfObject(value) { | ||
return typeof value === 'object'; | ||
}; | ||
_proto.shouldKeep = function shouldKeep(key) { | ||
return !this.filters.includes(key); | ||
}; | ||
return AttributesFilter; | ||
}(); | ||
var LogLevel; | ||
@@ -34,10 +87,7 @@ | ||
var Logger = /*#__PURE__*/function () { | ||
function Logger(application, environment, fields) { | ||
if (fields === void 0) { | ||
fields = {}; | ||
} | ||
this.application = application; | ||
this.environment = environment; | ||
this.fields = fields; | ||
function Logger(config) { | ||
this.application = config.application; | ||
this.environment = config.environment; | ||
this.fields = config.initializedFields; | ||
this.filter = new AttributesFilter(config.filterFields); | ||
} | ||
@@ -65,3 +115,8 @@ /** | ||
_proto.extend = function extend(fields) { | ||
return new Logger(this.application, this.environment, _extends({}, this.fields, fields)); | ||
var config = { | ||
application: this.application, | ||
environment: this.environment, | ||
initializedFields: _extends({}, this.fields, fields) | ||
}; | ||
return new Logger(config); | ||
} | ||
@@ -189,3 +244,5 @@ /** | ||
_proto._buildPayload = function _buildPayload(severity, message, payload) { | ||
var logEvent = { | ||
var filteredPayload = this.filter.run(payload); | ||
var log = _extends({ | ||
application: this.application, | ||
@@ -196,4 +253,5 @@ environment: this.environment, | ||
timestamp: new Date().toISOString() | ||
}; | ||
return JSON.stringify(_extends({}, logEvent, this.fields, payload)); | ||
}, this.fields, filteredPayload); | ||
return JSON.stringify(_extends({}, log)); | ||
}; | ||
@@ -200,0 +258,0 @@ |
{ | ||
"version": "1.2.0", | ||
"version": "2.0.0", | ||
"license": "MIT", | ||
@@ -4,0 +4,0 @@ "main": "dist/index.js", |
@@ -22,9 +22,15 @@ # Kiev-js | ||
```javascript | ||
import {Logger, LogLevel} from '@blacklane/kiev-js' | ||
import {Logger, LoggerConfig, LogLevel} from '@blacklane/kiev-js' | ||
const environment = process.env.NODE_ENV || 'development' | ||
let loggerConfiguration: LoggerConfig = { | ||
application: 'application-name', | ||
environment: environment, | ||
initializedFields: { foo: 'foo', bar: 'bar' }, // it is optional and will be added to all log entries | ||
filterFields: ["password", "accessToken"] // it is optional and will filter the content of the field | ||
} | ||
// Default level is 'warn' | ||
// The last parameter is optional and it'll be added to all log entries | ||
logger = new Logger('application-name', environment, { foo: 'foo', bar: 'bar' }) | ||
logger = new Logger(loggerConfiguration) | ||
@@ -36,5 +42,5 @@ // This won't be logged due to the default level | ||
// The payload will override any field defined in the constructor | ||
logger.warn('WARN! Look at this', { foo: 'bar' }) | ||
logger.warn('WARN! Look at this', { foo: 'bar', password: "Hard_One" }) | ||
// => {"application":"application-name","environment":"development","level":"WARN", message: "WARN! Look at this", "timestamp":"2020-10-15T10:51:32.621Z", "foo": "bar", "bar": "bar"} | ||
// => {"application":"application-name","environment":"development","level":"WARN", message: "WARN! Look at this", "timestamp":"2020-10-15T10:51:32.621Z", "foo": "bar", "bar": "bar", "password": "[FILTERED]} | ||
@@ -41,0 +47,0 @@ |
import * as logLevel from 'loglevel' | ||
import { Logger, LogLevel } from '.' | ||
import { Logger, LoggerConfig, LogLevel } from '.' | ||
@@ -7,19 +7,20 @@ jest.mock('loglevel') | ||
let defaultLogAttributes: string[] | ||
let logger: Logger | ||
let applicationName: string | ||
let environment: string | ||
let logMessage: string | ||
let loggerInitConfig: LoggerConfig | ||
const application = 'application-name' | ||
const environment = 'development' | ||
const logPayload = { | ||
user: { | ||
first_name: 'John', | ||
last_name: 'Doe' | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
email: 'john.doe@example.com' | ||
} | ||
} | ||
let defaultLogAttributes: string[] | ||
describe('logger', () => { | ||
beforeAll(() => { | ||
applicationName = 'application-name' | ||
environment = 'development' | ||
defaultLogAttributes = [ | ||
@@ -36,3 +37,8 @@ 'application', | ||
beforeEach(() => { | ||
logger = new Logger(applicationName, environment) | ||
loggerInitConfig = { | ||
application: application, | ||
environment: environment | ||
} | ||
logger = new Logger(loggerInitConfig) | ||
}) | ||
@@ -49,2 +55,29 @@ | ||
describe('constructor with filters', () => { | ||
beforeEach(() => { | ||
mockLogLevel.info.mockClear() | ||
}) | ||
it('changes the current log level', () => { | ||
expect.assertions(1) | ||
loggerInitConfig.filterFields = ['email'] | ||
const logger = new Logger(loggerInitConfig) | ||
logger.info(logMessage, logPayload) | ||
const message = JSON.parse(mockLogLevel.debug.mock.calls[0][0]) | ||
const expectedPayload = { | ||
user: { | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
email: '[FILTERED]' | ||
} | ||
} | ||
expect(message).toMatchObject(expectedPayload) | ||
}) | ||
}) | ||
describe('constructor with fields', () => { | ||
@@ -59,5 +92,7 @@ beforeEach(() => { | ||
const trackingId = 'passing fields to the constructor' | ||
const log = new Logger(applicationName, environment, { | ||
loggerInitConfig.initializedFields = { | ||
tracking_id: trackingId | ||
}) | ||
} | ||
const log = new Logger(loggerInitConfig) | ||
@@ -68,3 +103,3 @@ log.info(logMessage, logPayload) | ||
expect(message.application).toStrictEqual(applicationName) | ||
expect(message.application).toStrictEqual(application) | ||
expect(message.environment).toStrictEqual(environment) | ||
@@ -80,4 +115,7 @@ expect(message.tracking_id).toStrictEqual(trackingId) | ||
const bar = 'bar' | ||
const log = new Logger(applicationName, environment, { foo }) | ||
loggerInitConfig.initializedFields = { foo } | ||
const log = new Logger(loggerInitConfig) | ||
log.setFields({ foo: bar }) | ||
@@ -95,6 +133,7 @@ log.info(logMessage) | ||
const trackingId = 'new tracking id' | ||
const log = new Logger(applicationName, environment, { | ||
tracking_id: trackingIdOld | ||
}) | ||
loggerInitConfig.initializedFields = { tracking_id: trackingIdOld } | ||
const log = new Logger(loggerInitConfig) | ||
log.info(logMessage, { tracking_id: trackingId }) | ||
@@ -121,5 +160,6 @@ const message1 = JSON.parse(mockLogLevel.info.mock.calls[0][0]) | ||
const parent = new Logger(applicationName, environment, { | ||
tracking_id: trackingId | ||
}) | ||
loggerInitConfig.initializedFields = { tracking_id: trackingId } | ||
const parent = new Logger(loggerInitConfig) | ||
const extended = parent.extend({ extend: 'extend' }) | ||
@@ -154,3 +194,3 @@ | ||
) | ||
expect(message.application).toStrictEqual(applicationName) | ||
expect(message.application).toStrictEqual(application) | ||
expect(message.level).toStrictEqual(LogLevel.DEBUG) | ||
@@ -187,3 +227,3 @@ expect(message.environment).toStrictEqual(environment) | ||
) | ||
expect(message.application).toStrictEqual(applicationName) | ||
expect(message.application).toStrictEqual(application) | ||
expect(message.level).toStrictEqual(LogLevel.INFO) | ||
@@ -221,3 +261,3 @@ expect(message.environment).toStrictEqual(environment) | ||
) | ||
expect(message.application).toStrictEqual(applicationName) | ||
expect(message.application).toStrictEqual(application) | ||
expect(message.level).toStrictEqual(LogLevel.WARN) | ||
@@ -257,3 +297,3 @@ expect(message.environment).toStrictEqual(environment) | ||
) | ||
expect(message.application).toStrictEqual(applicationName) | ||
expect(message.application).toStrictEqual(application) | ||
expect(message.level).toStrictEqual(LogLevel.ERROR) | ||
@@ -291,3 +331,3 @@ expect(message.environment).toStrictEqual(environment) | ||
) | ||
expect(message.application).toStrictEqual(applicationName) | ||
expect(message.application).toStrictEqual(application) | ||
expect(message.level).toStrictEqual(LogLevel.TRACE) | ||
@@ -294,0 +334,0 @@ expect(message.environment).toStrictEqual(environment) |
import * as logger from 'loglevel' | ||
import { AttributesFilter } from './filter' | ||
interface LogMessage { | ||
application: string | ||
environment: string | ||
level: string | ||
message: string | ||
timestamp: string | ||
} | ||
enum LogLevel { | ||
export enum LogLevel { | ||
TRACE = 'TRACE', | ||
@@ -20,11 +13,20 @@ DEBUG = 'DEBUG', | ||
class Logger { | ||
export interface LoggerConfig { | ||
application: string | ||
environment: string | ||
fields: Object | ||
filterFields?: string[] | ||
initializedFields?: Object | ||
} | ||
constructor (application: string, environment: string, fields: Object = {}) { | ||
this.application = application | ||
this.environment = environment | ||
this.fields = fields | ||
export class Logger { | ||
application: string | ||
environment: string | ||
fields?: Object | ||
filter: AttributesFilter | ||
constructor (config: LoggerConfig) { | ||
this.application = config.application | ||
this.environment = config.environment | ||
this.fields = config.initializedFields | ||
this.filter = new AttributesFilter(config.filterFields) | ||
} | ||
@@ -48,6 +50,11 @@ | ||
public extend (fields: Object): Logger { | ||
return new Logger(this.application, this.environment, { | ||
...this.fields, | ||
...fields | ||
}) | ||
const config: LoggerConfig = { | ||
application: this.application, | ||
environment: this.environment, | ||
initializedFields: { | ||
...this.fields, | ||
...fields | ||
} | ||
} | ||
return new Logger(config) | ||
} | ||
@@ -146,3 +153,5 @@ | ||
): string { | ||
const logEvent: LogMessage = { | ||
const filteredPayload = this.filter.run(payload) | ||
const log = { | ||
application: this.application, | ||
@@ -152,9 +161,9 @@ environment: this.environment, | ||
message: message, | ||
timestamp: new Date().toISOString() | ||
timestamp: new Date().toISOString(), | ||
...this.fields, | ||
...filteredPayload | ||
} | ||
return JSON.stringify({ ...logEvent, ...this.fields, ...payload }) | ||
return JSON.stringify({ ...log }) | ||
} | ||
} | ||
export { Logger, LogLevel } |
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
72598
16
1052
92