@aircall/logger
Advanced tools
Comparing version 2.3.3 to 2.3.4
@@ -6,2 +6,17 @@ # Change Log | ||
## [2.3.4](http://bitbucket.org/aircall/front-end-modules/compare/@aircall/logger@2.3.3...@aircall/logger@2.3.4) (2020-04-09) | ||
### Bug Fixes | ||
* **logger:** rename variable ([0457ea8](http://bitbucket.org/aircall/front-end-modules/commits/0457ea8f3830ab5c1c7bd63b3034b86c5ac7cfd1)) | ||
* prettify ([a4f45d6](http://bitbucket.org/aircall/front-end-modules/commits/a4f45d68053b300f8a5ba933242d7d44b2bda6fd)) | ||
* **logger:** create the logger singleton from the consumer context ([b05c79b](http://bitbucket.org/aircall/front-end-modules/commits/b05c79b45ee41f18cdfcc1d9b335f1e73f3d9a00)) | ||
* **logger:** make the middleware accept an instance of the logger ([9783d48](http://bitbucket.org/aircall/front-end-modules/commits/9783d4826f0d05b0f17bee7bccf70bc65b5cfc2c)) | ||
* **logger:** make the middleware accept an instance of the logger ([40f5d5a](http://bitbucket.org/aircall/front-end-modules/commits/40f5d5ab1b32cb4a1845aca86fd651f327031bb6)) | ||
## [2.3.3](http://bitbucket.org/aircall/front-end-modules/compare/@aircall/logger@2.3.2...@aircall/logger@2.3.3) (2020-04-02) | ||
@@ -8,0 +23,0 @@ |
@@ -1,1 +0,1 @@ | ||
export declare const DEUBG_MODE_LIMIT_STORED_ACTIONS = 20; | ||
export declare const DEBUG_MODE_LIMIT_STORED_ACTIONS = 20; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
// Number of stored actions for the debug mode; | ||
exports.DEUBG_MODE_LIMIT_STORED_ACTIONS = 20; | ||
exports.DEBUG_MODE_LIMIT_STORED_ACTIONS = 20; | ||
//# sourceMappingURL=constants.js.map |
@@ -1,6 +0,3 @@ | ||
import Logger from './Logger'; | ||
declare const logger: Logger; | ||
export * from './actions'; | ||
export * from './middleware'; | ||
export * from './Logger'; | ||
export default logger; |
@@ -6,8 +6,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const Logger_1 = require("./Logger"); | ||
const logger = new Logger_1.default(); | ||
__export(require("./actions")); | ||
__export(require("./middleware")); | ||
__export(require("./Logger")); | ||
exports.default = logger; | ||
//# sourceMappingURL=index.js.map |
@@ -30,3 +30,3 @@ import { StatusType } from '@datadog/browser-logs'; | ||
} | ||
export default class Logger { | ||
export declare class Logger { | ||
initialized: boolean; | ||
@@ -33,0 +33,0 @@ queue: Function[]; |
@@ -64,3 +64,3 @@ "use strict"; | ||
} | ||
exports.default = Logger; | ||
exports.Logger = Logger; | ||
//# sourceMappingURL=Logger.js.map |
@@ -1,2 +0,6 @@ | ||
import { Action, Middleware, Dispatch } from 'redux'; | ||
export declare const loggerMiddleware: Middleware<{}, Action, Dispatch<Action>>; | ||
import { Action, Dispatch } from 'redux'; | ||
import { Logger } from './Logger'; | ||
declare type actionInterceptor = (action: Action) => Action; | ||
declare type loggerMiddlewareReturnType = (next: Dispatch<Action>) => actionInterceptor; | ||
export declare const loggerMiddleware: (logger: Logger) => () => loggerMiddlewareReturnType; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const _1 = require("./"); | ||
const browser_logs_1 = require("@datadog/browser-logs"); | ||
const constants_1 = require("./constants"); | ||
const actions_1 = require("./actions"); | ||
exports.loggerMiddleware = () => { | ||
const lastDispatchedActions = []; | ||
return (next) => (action) => { | ||
if (_1.default.level !== browser_logs_1.StatusType.debug) { | ||
exports.loggerMiddleware = (logger) => { | ||
return () => { | ||
const lastDispatchedActions = []; | ||
return (next) => (action) => { | ||
if (logger.level !== browser_logs_1.StatusType.debug) { | ||
return next(action); | ||
} | ||
if (actions_1.isLoggerAction(action)) { | ||
action.properties.lastDispatchedActions = lastDispatchedActions; | ||
} | ||
else { | ||
lastDispatchedActions.push({ type: action.type }); | ||
} | ||
if (lastDispatchedActions.length > constants_1.DEBUG_MODE_LIMIT_STORED_ACTIONS) { | ||
lastDispatchedActions.shift(); | ||
} | ||
return next(action); | ||
} | ||
if (actions_1.isLoggerAction(action)) { | ||
action.properties.lastDispatchedActions = lastDispatchedActions; | ||
} | ||
else { | ||
lastDispatchedActions.push({ type: action.type }); | ||
} | ||
if (lastDispatchedActions.length > constants_1.DEUBG_MODE_LIMIT_STORED_ACTIONS) { | ||
lastDispatchedActions.shift(); | ||
} | ||
return next(action); | ||
}; | ||
}; | ||
}; | ||
//# sourceMappingURL=middleware.js.map |
{ | ||
"name": "@aircall/logger", | ||
"version": "2.3.3", | ||
"version": "2.3.4", | ||
"main": "dist/index.js", | ||
@@ -14,3 +14,3 @@ "types": "dist/index.d.ts", | ||
}, | ||
"gitHead": "b78b28cbf565454ebe6f37580f3f8df9609bc857", | ||
"gitHead": "6574e120a8b34fe9032587850a0aa3f775c2c808", | ||
"dependencies": { | ||
@@ -17,0 +17,0 @@ "@datadog/browser-logs": "1.7.3", |
global.console = { | ||
log: jest.fn(), | ||
log: jest.fn(), | ||
debug: jest.fn(), | ||
@@ -10,9 +10,9 @@ info: jest.fn(), | ||
global.DD_LOGS = { | ||
init: jest.fn(), | ||
logger: { | ||
setContext: jest.fn(), | ||
setLevel: jest.fn(), | ||
setHandler: jest.fn(), | ||
info: jest.fn() | ||
} | ||
init: jest.fn(), | ||
logger: { | ||
setContext: jest.fn(), | ||
setLevel: jest.fn(), | ||
setHandler: jest.fn(), | ||
info: jest.fn() | ||
} | ||
}; |
@@ -1,15 +0,16 @@ | ||
import { isLoggerAction, LOGGER, initLogger,log, debug, info, warn, error } from './actions'; | ||
import { isLoggerAction, LOGGER, initLogger, log, debug, info, warn, error } from './actions'; | ||
import { LOGGER_LEVEL } from './Logger'; | ||
describe('actions', (): void => { | ||
describe('isLoggerAction', (): void => { | ||
it('should return false if it is not a logger action', (): void => { | ||
expect(isLoggerAction({type: 'FOO'})).toBeFalsy(); | ||
expect(isLoggerAction({ type: 'FOO' })).toBeFalsy(); | ||
}); | ||
it('should return true if it is a logger action', (): void => { | ||
[ LOGGER.LOG, LOGGER.DEBUG, LOGGER.INFO, LOGGER.WARN, LOGGER.ERROR ].forEach((type: LOGGER): void => { | ||
expect(isLoggerAction({ type })).toBeTruthy(); | ||
}); | ||
[LOGGER.LOG, LOGGER.DEBUG, LOGGER.INFO, LOGGER.WARN, LOGGER.ERROR].forEach( | ||
(type: LOGGER): void => { | ||
expect(isLoggerAction({ type })).toBeTruthy(); | ||
} | ||
); | ||
}); | ||
@@ -16,0 +17,0 @@ }); |
@@ -19,5 +19,6 @@ import { Action } from 'redux'; | ||
const loggerActionsTypes = [ LOGGER.LOG, LOGGER.DEBUG, LOGGER.INFO, LOGGER.WARN, LOGGER.ERROR ]; | ||
const loggerActionsTypes = [LOGGER.LOG, LOGGER.DEBUG, LOGGER.INFO, LOGGER.WARN, LOGGER.ERROR]; | ||
export const isLoggerAction = (action: Action): action is LoggerLogAction => loggerActionsTypes.includes(action.type); | ||
export const isLoggerAction = (action: Action): action is LoggerLogAction => | ||
loggerActionsTypes.includes(action.type); | ||
@@ -28,3 +29,7 @@ export const initLogger = (): { type: LOGGER.INIT } => ({ | ||
export const log = (level: StatusType, message: string, properties: object = {}): LoggerLogAction & { level: StatusType } => ({ | ||
export const log = ( | ||
level: StatusType, | ||
message: string, | ||
properties: object = {} | ||
): LoggerLogAction & { level: StatusType } => ({ | ||
type: LOGGER.LOG, | ||
@@ -31,0 +36,0 @@ level, |
// Number of stored actions for the debug mode; | ||
export const DEUBG_MODE_LIMIT_STORED_ACTIONS = 20; | ||
export const DEBUG_MODE_LIMIT_STORED_ACTIONS = 20; |
@@ -1,9 +0,3 @@ | ||
import Logger from './Logger'; | ||
const logger = new Logger(); | ||
export * from './actions'; | ||
export * from './middleware'; | ||
export * from './Logger'; | ||
export default logger; |
jest.mock('@datadog/browser-logs'); | ||
import Logger, { LOGGER_ENVIRONMENT, LOGGER_LEVEL } from './Logger'; | ||
import { Logger, LOGGER_ENVIRONMENT, LOGGER_LEVEL } from './Logger'; | ||
import { datadogLogs, StatusType } from '@datadog/browser-logs'; | ||
const properties = { foo: 'BAR' }; | ||
const [ message, token ] = [ 'MESSAGE', 'TOKEN']; | ||
const [message, token] = ['MESSAGE', 'TOKEN']; | ||
@@ -21,3 +21,2 @@ const user_session = { | ||
describe('Logger', (): void => { | ||
it('should be an instance of Logger', (): void => { | ||
@@ -96,5 +95,2 @@ expect(logger).toBeInstanceOf(Logger); | ||
}); | ||
}); | ||
@@ -36,3 +36,3 @@ import { datadogLogs as sdk, StatusType, HandlerType } from '@datadog/browser-logs'; | ||
export default class Logger { | ||
export class Logger { | ||
public initialized = false; | ||
@@ -44,3 +44,4 @@ public queue: Function[] = []; | ||
public init({ level, context, token }: LoggerInitOptions): void { | ||
const isDevelopment: boolean = context.user_session.environment === LOGGER_ENVIRONMENT.DEVELOPMENT; | ||
const isDevelopment: boolean = | ||
context.user_session.environment === LOGGER_ENVIRONMENT.DEVELOPMENT; | ||
@@ -97,3 +98,2 @@ sdk.init({ | ||
} | ||
} |
@@ -1,3 +0,3 @@ | ||
import logger, { LOGGER, LOGGER_LEVEL } from './'; | ||
import { DEUBG_MODE_LIMIT_STORED_ACTIONS } from './constants'; | ||
import { Logger, LOGGER, LOGGER_LEVEL } from './'; | ||
import { DEBUG_MODE_LIMIT_STORED_ACTIONS } from './constants'; | ||
import { loggerMiddleware } from './middleware'; | ||
@@ -8,4 +8,5 @@ | ||
describe('loggerMiddleware', (): void => { | ||
const logger = new Logger(); | ||
const handler = jest.fn(); | ||
const store = createStore(handler, undefined, applyMiddleware(loggerMiddleware)); | ||
const store = createStore(handler, undefined, applyMiddleware(loggerMiddleware(logger))); | ||
const fooAction = { type: 'FOO' }; | ||
@@ -27,13 +28,16 @@ const infoAction = { type: LOGGER.INFO, properties: {} }; | ||
store.dispatch(infoAction); | ||
expect(handler).toHaveBeenCalledWith(undefined, { ...infoAction, properties: { lastDispatchedActions: [fooAction] } }); | ||
expect(handler).toHaveBeenCalledWith(undefined, { | ||
...infoAction, | ||
properties: { lastDispatchedActions: [fooAction] } | ||
}); | ||
}); | ||
it('should keep only the last DEUBG_MODE_LIMIT_STORED_ACTIONS actions dispatched', (): void => { | ||
it('should keep only the last DEBUG_MODE_LIMIT_STORED_ACTIONS actions dispatched', (): void => { | ||
logger.setLevel(LOGGER_LEVEL.debug); | ||
for (let i = 0; i < DEUBG_MODE_LIMIT_STORED_ACTIONS + 5; i ++) { | ||
for (let i = 0; i < DEBUG_MODE_LIMIT_STORED_ACTIONS + 5; i++) { | ||
store.dispatch(fooAction); | ||
} | ||
const lastDispatchedActions = (new Array(DEUBG_MODE_LIMIT_STORED_ACTIONS)).fill(fooAction); | ||
const lastDispatchedActions = new Array(DEBUG_MODE_LIMIT_STORED_ACTIONS).fill(fooAction); | ||
@@ -47,3 +51,2 @@ store.dispatch(infoAction); | ||
}); | ||
}); |
@@ -1,6 +0,6 @@ | ||
import logger from './'; | ||
import { StatusType } from '@datadog/browser-logs'; | ||
import { Action, Middleware, Dispatch } from 'redux'; | ||
import { DEUBG_MODE_LIMIT_STORED_ACTIONS } from './constants'; | ||
import { Action, Dispatch } from 'redux'; | ||
import { DEBUG_MODE_LIMIT_STORED_ACTIONS } from './constants'; | ||
import { isLoggerAction } from './actions'; | ||
import { Logger } from './Logger'; | ||
@@ -10,23 +10,24 @@ type actionInterceptor = (action: Action) => Action; | ||
export const loggerMiddleware: Middleware <{}, Action, Dispatch<Action>> = (): loggerMiddlewareReturnType => { | ||
const lastDispatchedActions: Action[] = []; | ||
export const loggerMiddleware = (logger: Logger) => { | ||
return (): loggerMiddlewareReturnType => { | ||
const lastDispatchedActions: Action[] = []; | ||
return (next: Dispatch<Action>): actionInterceptor => (action: Action): Action => { | ||
return (next: Dispatch<Action>): actionInterceptor => (action: Action): Action => { | ||
if (logger.level !== StatusType.debug) { | ||
return next(action); | ||
} | ||
if (logger.level !== StatusType.debug) { | ||
return next(action); | ||
} | ||
if (isLoggerAction(action)) { | ||
action.properties.lastDispatchedActions = lastDispatchedActions; | ||
} else { | ||
lastDispatchedActions.push({ type: action.type }); | ||
} | ||
if (isLoggerAction(action)) { | ||
action.properties.lastDispatchedActions = lastDispatchedActions; | ||
} else { | ||
lastDispatchedActions.push({ type: action.type }); | ||
} | ||
if (lastDispatchedActions.length > DEBUG_MODE_LIMIT_STORED_ACTIONS) { | ||
lastDispatchedActions.shift(); | ||
} | ||
if (lastDispatchedActions.length > DEUBG_MODE_LIMIT_STORED_ACTIONS) { | ||
lastDispatchedActions.shift(); | ||
} | ||
return next(action); | ||
return next(action); | ||
}; | ||
}; | ||
}; |
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
Sorry, the diff of this file is not supported yet
28136
562
27