Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@aircall/logger

Package Overview
Dependencies
Maintainers
6
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aircall/logger - npm Package Compare versions

Comparing version 1.2.0 to 2.0.0

4

dist/actions.d.ts

@@ -9,4 +9,3 @@ import { Action } from 'redux';

WARN = "LOGGER_WARN",
ERROR = "LOGGER_ERROR",
TRACK = "LOGGER_TRACK"
ERROR = "LOGGER_ERROR"
}

@@ -31,2 +30,1 @@ export interface LoggerLogAction {

export declare const error: (message: string, properties?: object) => LoggerLogAction;
export declare const track: (message: string, properties?: object) => LoggerLogAction;

@@ -11,3 +11,2 @@ "use strict";

LOGGER["ERROR"] = "LOGGER_ERROR";
LOGGER["TRACK"] = "LOGGER_TRACK";
})(LOGGER = exports.LOGGER || (exports.LOGGER = {}));

@@ -45,7 +44,2 @@ const loggerActionsTypes = [LOGGER.LOG, LOGGER.DEBUG, LOGGER.INFO, LOGGER.WARN, LOGGER.ERROR];

});
exports.track = (message, properties = {}) => ({
type: LOGGER.TRACK,
message,
properties
});
//# sourceMappingURL=actions.js.map

@@ -1,1 +0,1 @@

export declare const VERBOSE_MODE_LIMIT_STORED_ACTIONS = 20;
export declare const DEUBG_MODE_LIMIT_STORED_ACTIONS = 20;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
// Number of stored actions for the debug mode;
exports.VERBOSE_MODE_LIMIT_STORED_ACTIONS = 20;
exports.DEUBG_MODE_LIMIT_STORED_ACTIONS = 20;
//# sourceMappingURL=constants.js.map

@@ -1,42 +0,79 @@

/// <reference types="segment-analytics" />
export declare enum LOGGER_LEVEL {
DEBUG = "DEBUG",
INFO = "INFO",
WARN = "WARN",
ERROR = "ERROR"
DEBUG = "debug",
INFO = "info",
WARN = "warn",
ERROR = "error"
}
export declare interface LoggerIdentificationOptions {
user_id: string;
export declare enum LOGGER_HANDLER {
HTTP = "http",
CONSOLE = "console",
SILENT = "silent"
}
export declare enum LOGGER_ENVIRONMENT {
DEVELOPMENT = "development",
TEST = "test",
STAGING = "staging",
BETA = "beta",
PRODUCTION = "production"
}
export declare interface UserSession {
app_version: string;
company: number;
debugging_session_id: string;
platform: string;
environment: string;
company_id: number;
cti_name?: string;
device: string;
device_details?: string;
electron_version?: string;
environment: LOGGER_ENVIRONMENT;
release: string;
user_id: number;
}
export declare interface LoggerInitOptions {
development?: boolean;
key: string;
verbose?: boolean;
context: LoggerContext;
token: string;
level: LOGGER_LEVEL;
}
export declare interface WindowWithAnalytics extends Window {
analytics: SegmentAnalytics.AnalyticsJS | AnalyticsJSMock;
export declare interface LoggerContext {
service: string;
user_session: UserSession;
}
export declare class AnalyticsJSMock extends Array {
SNIPPET_VERSION: string;
identify(...args: (string | object)[]): void;
track(...args: (string | object)[]): void;
export declare interface SDKInitOptions {
clientToken: string;
forwardErrorsToLogs: boolean;
}
export declare interface WindowWithDatadog extends Window {
DD_LOGS: {
logger: DatadogSDK;
init(options: SDKInitOptions): void;
};
}
export declare interface QueueItem {
method: keyof DatadogSDK;
args: (string | object | LOGGER_LEVEL)[];
}
export declare class DatadogSDK {
private queue;
constructor(queue: QueueItem[]);
private enqueue;
log(message: string, properties: object, status: LOGGER_LEVEL): void;
debug(message: string, properties: object): void;
info(message: string, properties: object): void;
warn(message: string, properties: object): void;
error(message: string, properties: object): void;
init(options: SDKInitOptions): void;
setLevel(level: LOGGER_LEVEL): void;
setContext(context: LoggerContext): void;
setHandler(handler: LOGGER_HANDLER): void;
}
export declare const defaultContext: LoggerContext;
export default class Logger {
constructor();
private identified;
private identification;
private development;
private verbose;
private queue;
private sdk;
private context;
private token;
level: LOGGER_LEVEL;
private getScriptTag;
readonly isVerbose: boolean;
readonly isIdentified: boolean;
enableVerboseMode(): void;
disableVerboseMode(): void;
private loadSDK;
private onLoad;
init(options: LoggerInitOptions): void;
identify(options: LoggerIdentificationOptions): void;
setLevel(level: LOGGER_LEVEL): void;
log(level: LOGGER_LEVEL, message: string, properties?: object): void;

@@ -47,3 +84,2 @@ debug(message: string, properties?: object): void;

error(message: string, properties?: object): void;
track(message: string, properties?: object, integrations?: object): void;
}

@@ -5,92 +5,132 @@ "use strict";

(function (LOGGER_LEVEL) {
LOGGER_LEVEL["DEBUG"] = "DEBUG";
LOGGER_LEVEL["INFO"] = "INFO";
LOGGER_LEVEL["WARN"] = "WARN";
LOGGER_LEVEL["ERROR"] = "ERROR";
LOGGER_LEVEL["DEBUG"] = "debug";
LOGGER_LEVEL["INFO"] = "info";
LOGGER_LEVEL["WARN"] = "warn";
LOGGER_LEVEL["ERROR"] = "error";
})(LOGGER_LEVEL = exports.LOGGER_LEVEL || (exports.LOGGER_LEVEL = {}));
class AnalyticsJSMock extends Array {
constructor() {
super(...arguments);
this.SNIPPET_VERSION = '4.1.0';
var LOGGER_HANDLER;
(function (LOGGER_HANDLER) {
LOGGER_HANDLER["HTTP"] = "http";
LOGGER_HANDLER["CONSOLE"] = "console";
LOGGER_HANDLER["SILENT"] = "silent";
})(LOGGER_HANDLER = exports.LOGGER_HANDLER || (exports.LOGGER_HANDLER = {}));
var LOGGER_ENVIRONMENT;
(function (LOGGER_ENVIRONMENT) {
LOGGER_ENVIRONMENT["DEVELOPMENT"] = "development";
LOGGER_ENVIRONMENT["TEST"] = "test";
LOGGER_ENVIRONMENT["STAGING"] = "staging";
LOGGER_ENVIRONMENT["BETA"] = "beta";
LOGGER_ENVIRONMENT["PRODUCTION"] = "production";
})(LOGGER_ENVIRONMENT = exports.LOGGER_ENVIRONMENT || (exports.LOGGER_ENVIRONMENT = {}));
class DatadogSDK {
constructor(queue) {
this.queue = [];
this.queue = queue;
}
identify(...args) {
args.unshift('identify');
this.push(args);
enqueue(method, args) {
this.queue.push({ method, args });
}
track(...args) {
args.unshift('track');
this.push(args);
log(message, properties, status) {
this.enqueue('log', [message, properties, status]);
}
debug(message, properties) {
this.enqueue(LOGGER_LEVEL.DEBUG, [message, properties]);
}
info(message, properties) {
this.enqueue(LOGGER_LEVEL.INFO, [message, properties]);
}
warn(message, properties) {
this.enqueue(LOGGER_LEVEL.WARN, [message, properties]);
}
error(message, properties) {
this.enqueue(LOGGER_LEVEL.ERROR, [message, properties]);
}
init(options) {
this.enqueue('init', [options]);
}
setLevel(level) {
this.enqueue('setLevel', [level]);
}
setContext(context) {
this.enqueue('setContext', [context]);
}
setHandler(handler) {
this.enqueue('setHandler', [handler]);
}
}
exports.AnalyticsJSMock = AnalyticsJSMock;
exports.DatadogSDK = DatadogSDK;
exports.defaultContext = {
service: 'default',
user_session: {
app_version: 'default',
company_id: 0,
device: 'default',
environment: LOGGER_ENVIRONMENT.DEVELOPMENT,
release: 'default',
user_id: 0
}
};
class Logger {
constructor() {
this.identified = false;
this.identification = null;
this.development = false;
this.verbose = false;
window.analytics = new AnalyticsJSMock();
this.queue = [];
this.sdk = new DatadogSDK(this.queue);
this.context = exports.defaultContext;
this.token = '';
this.level = LOGGER_LEVEL.INFO;
}
getScriptTag(key) {
getScriptTag() {
const script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.src = `https://cdn.segment.com/analytics.js/v1/${key}/analytics.min.js`;
script.src = 'https://www.datadoghq-browser-agent.com/datadog-logs-us.js';
return script;
}
get isVerbose() {
return this.verbose;
loadSDK() {
const script = this.getScriptTag();
script.addEventListener('load', this.onLoad.bind(this));
document.head.appendChild(script);
}
get isIdentified() {
return this.identified;
onLoad() {
const { DD_LOGS } = window;
const isDevelopment = this.context.user_session.environment === LOGGER_ENVIRONMENT.DEVELOPMENT;
DD_LOGS.init({
clientToken: this.token,
forwardErrorsToLogs: !isDevelopment
});
this.sdk = DD_LOGS.logger;
this.sdk.setContext(this.context);
this.sdk.setHandler(isDevelopment ? LOGGER_HANDLER.CONSOLE : LOGGER_HANDLER.HTTP);
this.sdk.setLevel(this.level);
this.queue.forEach(({ method, args }) => {
// @ts-ignore
this.sdk[method](...args);
});
}
enableVerboseMode() {
this.verbose = true;
}
disableVerboseMode() {
this.verbose = false;
}
init(options) {
this.development = options.development || false;
this.verbose = options.verbose || false;
const script = this.getScriptTag(options.key);
document.head.appendChild(script);
this.context = options.context;
this.token = options.token;
this.level = options.level;
this.loadSDK();
}
identify(options) {
window.analytics.identify(options.user_id, options);
this.identification = options;
this.identified = true;
setLevel(level) {
this.level = level;
this.sdk.setLevel(level);
}
log(level, message, properties = {}) {
if (this.development) {
console.log(level, message, properties); // eslint-disable-line no-console
}
else if (level !== LOGGER_LEVEL.DEBUG || this.isVerbose) {
this.track('log', Object.assign({ level, message }, properties), { 'All': false, 'Amazon Kinesis': true });
}
this.sdk.log(message, properties, level);
}
debug(message, properties = {}) {
this.log(LOGGER_LEVEL.DEBUG, message, properties);
this.sdk.debug(message, properties);
}
info(message, properties = {}) {
this.log(LOGGER_LEVEL.INFO, message, properties);
this.sdk.info(message, properties);
}
warn(message, properties = {}) {
this.log(LOGGER_LEVEL.WARN, message, properties);
this.sdk.warn(message, properties);
}
error(message, properties = {}) {
this.log(LOGGER_LEVEL.ERROR, message, properties);
this.sdk.error(message, properties);
}
track(message, properties = {}, integrations = {}) {
if (this.development) {
console.log('TRACK', message, properties); // eslint-disable-line no-console
}
else {
window.analytics.track(message, Object.assign({}, properties, this.identification), {
integrations: Object.assign({ All: false }, integrations)
});
}
}
}
exports.default = Logger;
//# sourceMappingURL=Logger.js.map

@@ -9,3 +9,3 @@ "use strict";

return (next) => (action) => {
if (!_1.default.isVerbose) {
if (_1.default.level !== _1.LOGGER_LEVEL.DEBUG) {
return next(action);

@@ -19,3 +19,3 @@ }

}
if (lastDispatchedActions.length > constants_1.VERBOSE_MODE_LIMIT_STORED_ACTIONS) {
if (lastDispatchedActions.length > constants_1.DEUBG_MODE_LIMIT_STORED_ACTIONS) {
lastDispatchedActions.shift();

@@ -22,0 +22,0 @@ }

{
"name": "@aircall/logger",
"version": "1.2.0",
"version": "2.0.0",
"main": "dist/index.js",

@@ -13,6 +13,3 @@ "types": "dist/index.d.ts",

},
"gitHead": "7945d7290dc9dbfb555ae903ceaba59c43d41e8f",
"devDependencies": {
"@types/segment-analytics": "^0.0.31"
},
"gitHead": "c088bffdce2987ef2a5c4bacf13e822f31ce399a",
"dependencies": {

@@ -19,0 +16,0 @@ "redux": "4.0.1"

global.console = {
log: jest.fn()
};
global.DD_LOGS = {
init: jest.fn(),
logger: {
setContext: jest.fn(),
setLevel: jest.fn(),
setHandler: jest.fn(),
info: jest.fn()
}
};

@@ -1,2 +0,2 @@

import { isLoggerAction, LOGGER, log, debug, info, warn, error, track } from './actions';
import { isLoggerAction, LOGGER, initLogger,log, debug, info, warn, error } from './actions';
import { LOGGER_LEVEL } from './Logger';

@@ -18,3 +18,3 @@

describe('log, debug, info, warn, error, track', (): void => {
describe('initLogger, log, debug, info, warn, error', (): void => {
const message = 'FOO';

@@ -24,2 +24,3 @@ const properties: object = {};

it('should have the right types', (): void => {
expect(initLogger().type).toEqual(LOGGER.INIT);
expect(log(LOGGER_LEVEL.INFO, message, properties).type).toEqual(LOGGER.LOG);

@@ -30,5 +31,4 @@ expect(debug(message, properties).type).toEqual(LOGGER.DEBUG);

expect(error(message, properties).type).toEqual(LOGGER.ERROR);
expect(track(message, properties).type).toEqual(LOGGER.TRACK);
});
});
});

@@ -10,4 +10,3 @@ import { Action } from 'redux';

WARN = 'LOGGER_WARN',
ERROR = 'LOGGER_ERROR',
TRACK = 'LOGGER_TRACK'
ERROR = 'LOGGER_ERROR'
}

@@ -59,7 +58,1 @@

});
export const track = (message: string, properties: object = {}): LoggerLogAction => ({
type: LOGGER.TRACK,
message,
properties
});
// Number of stored actions for the debug mode;
export const VERBOSE_MODE_LIMIT_STORED_ACTIONS = 20;
export const DEUBG_MODE_LIMIT_STORED_ACTIONS = 20;

@@ -1,28 +0,23 @@

import Logger, { LOGGER_LEVEL, WindowWithAnalytics, AnalyticsJSMock } from './Logger';
import Logger, { LOGGER_ENVIRONMENT, LOGGER_LEVEL, WindowWithDatadog } from './Logger';
const initOptions = {
key: 'FOO',
development: false,
verbose: false
};
const properties = { foo: 'BAR' };
const identifyOptions = {
user_id: '1234',
app_version: '1.0.0',
company: 1234,
debugging_session_id: 'qwertyuiop234567890',
platform: 'platform',
environment: 'production'
const [ message, status, token ] = [ 'MESSAGE', 'STATUS', 'TOKEN'];
const user_session = {
app_version: 'APP_VERSION',
company_id: 0,
device: 'DEVICE',
device_details: 'DEVICE_DETAILS',
environment: LOGGER_ENVIRONMENT.TEST,
release: 'RELEASE',
user_id: 0
};
const message = 'MESSAGE';
let logger = new Logger() as any; // eslint-disable-line
const { DD_LOGS } = (window as WindowWithDatadog);
const properties = { foo: 'BAR' };
const integrations = { All: false, 'Amazon Kinesis': true };
let logger = new Logger();
beforeEach((): void => {
logger = new Logger();
logger.info('FOO');
});

@@ -36,53 +31,42 @@

it('should mock the analyticsJS SDK while the script is loading', (): void => {
const analytics = (window as WindowWithAnalytics).analytics as AnalyticsJSMock;
const [trackEvent, trackPayload] = [ 'FOO', { foo: 'BAR' } ];
const [identifyEvent, identifyPayload] = [ 'FOO', { foo: 'BAR' } ];
describe('init', (): void => {
it('should set properties and load Datadog SDK', (): void => {
jest.spyOn(logger, 'loadSDK');
expect(analytics).toBeInstanceOf(AnalyticsJSMock);
logger.init({
context: {
service: 'phone',
user_session
},
token,
level: LOGGER_LEVEL.DEBUG
});
analytics.track(trackEvent, trackPayload);
analytics.identify(identifyEvent, identifyPayload);
expect(analytics[0]).toEqual(['track', trackEvent, trackPayload]);
expect(analytics[1]).toEqual(['identify', identifyEvent, identifyPayload]);
});
describe('init', (): void => {
it('should inject a script tag', (): void => {
const spy: jest.SpyInstance = jest.spyOn(document.head, 'appendChild');
logger.init(initOptions);
expect(spy).toHaveBeenCalledWith(expect.any(HTMLScriptElement));
expect(logger.token).toEqual(token);
expect(logger.level).toEqual(LOGGER_LEVEL.DEBUG);
expect(logger.context).toEqual({ service: 'phone', user_session });
expect(logger.loadSDK).toHaveBeenCalled();
});
});
describe('identify', (): void => {
it('should set identified', (): void => {
logger.init(initOptions);
expect(logger.isIdentified).toBeFalsy();
logger.identify(identifyOptions);
expect(logger.isIdentified).toBeTruthy();
});
describe('loadSDK', (): void => {
it('should call getScriptTag, add event listener and inject it', (): void => {
const script = document.createElement('script');
script.addEventListener = jest.fn();
jest.spyOn(logger, 'getScriptTag').mockReturnValue(script);
jest.spyOn(document.head, 'appendChild');
it('should set verbose', (): void => {
logger.init({ ...initOptions, verbose: true });
expect(logger.isVerbose).toBeTruthy();
});
});
logger.loadSDK();
describe('enableVerboseMode', (): void => {
it('should set verbose', (): void => {
logger.init(initOptions);
expect(logger.isVerbose).toBeFalsy();
logger.enableVerboseMode();
expect(logger.isVerbose).toBeTruthy();
expect(logger.getScriptTag).toHaveBeenCalled();
expect(script.addEventListener).toHaveBeenCalledWith('load', expect.any(Function));
expect(document.head.appendChild).toHaveBeenCalledWith(script);
});
});
describe('disableVerboseMode', (): void => {
it('should set verbose', (): void => {
logger.init({ ...initOptions, verbose: true });
expect(logger.isVerbose).toBeTruthy();
logger.disableVerboseMode();
expect(logger.isVerbose).toBeFalsy();
describe('onLoad', (): void => {
it('should init Datadog SDK', (): void => {
jest.spyOn(DD_LOGS, 'init');
logger.onLoad();
expect(DD_LOGS.init).toHaveBeenCalledWith({clientToken: "", forwardErrorsToLogs: false});
});

@@ -92,48 +76,18 @@ });

describe('log', (): void => {
it('should call track', (): void => {
const spy: jest.SpyInstance = jest.spyOn(logger, 'track');
logger.log(LOGGER_LEVEL.INFO, message, properties);
expect(spy).toHaveBeenCalledWith(
'log',
{ level: LOGGER_LEVEL.INFO, message, ...properties },
{ 'All': false, 'Amazon Kinesis': true }
);
it('should call sdk log and enqueue', (): void => {
jest.spyOn(logger.sdk, 'log');
jest.spyOn(logger.sdk, 'enqueue');
logger.log(status, message, properties);
expect(logger.sdk.log).toHaveBeenCalledWith(message, properties, status);
expect(logger.sdk.enqueue).toHaveBeenCalledWith('log', [ message, properties, status ]);
});
it('should call track for DEBUG level only in verbose mode', (): void => {
logger.init({ ...initOptions, development: false, verbose: false });
const spy: jest.SpyInstance = jest.spyOn(logger, 'track');
logger.log(LOGGER_LEVEL.DEBUG, message, properties);
expect(spy).not.toHaveBeenCalledWith();
logger.enableVerboseMode();
logger.log(LOGGER_LEVEL.DEBUG, message, properties);
expect(spy).toHaveBeenCalledWith(
'log',
{ level: LOGGER_LEVEL.DEBUG, message, ...properties },
{ 'All': false, 'Amazon Kinesis': true }
);
});
it('should call console.log', (): void => {
logger.init({ ...initOptions, development: true });
logger.log(LOGGER_LEVEL.INFO, message, properties);
expect(console.log).toHaveBeenCalledWith(LOGGER_LEVEL.INFO, message, properties); // eslint-disable-line no-console
});
});
describe('debug', (): void => {
it('should call log', (): void => {
const spy: jest.SpyInstance = jest.spyOn(logger, 'log');
it('should call sdk debug and enqueue', (): void => {
jest.spyOn(logger.sdk, 'debug');
jest.spyOn(logger.sdk, 'enqueue');
logger.debug(message, properties);
expect(spy).toHaveBeenCalledWith(LOGGER_LEVEL.DEBUG, message, properties);
expect(logger.sdk.debug).toHaveBeenCalledWith(message, properties);
expect(logger.sdk.enqueue).toHaveBeenCalledWith('debug', [ message, properties ]);
});

@@ -143,8 +97,8 @@ });

describe('info', (): void => {
it('should call log', (): void => {
const spy: jest.SpyInstance = jest.spyOn(logger, 'log');
it('should call sdk info and enqueue', (): void => {
jest.spyOn(logger.sdk, 'info');
jest.spyOn(logger.sdk, 'enqueue');
logger.info(message, properties);
expect(spy).toHaveBeenCalledWith(LOGGER_LEVEL.INFO, message, properties);
expect(logger.sdk.info).toHaveBeenCalledWith(message, properties);
expect(logger.sdk.enqueue).toHaveBeenCalledWith('info', [ message, properties ]);
});

@@ -154,8 +108,8 @@ });

describe('warn', (): void => {
it('should call log', (): void => {
const spy: jest.SpyInstance = jest.spyOn(logger, 'log');
it('should call sdk warn and enqueue', (): void => {
jest.spyOn(logger.sdk, 'warn');
jest.spyOn(logger.sdk, 'enqueue');
logger.warn(message, properties);
expect(spy).toHaveBeenCalledWith(LOGGER_LEVEL.WARN, message, properties);
expect(logger.sdk.warn).toHaveBeenCalledWith(message, properties);
expect(logger.sdk.enqueue).toHaveBeenCalledWith('warn', [ message, properties ]);
});

@@ -165,36 +119,13 @@ });

describe('error', (): void => {
it('should call log', (): void => {
const spy: jest.SpyInstance = jest.spyOn(logger, 'log');
it('should call sdk error and enqueue', (): void => {
jest.spyOn(logger.sdk, 'error');
jest.spyOn(logger.sdk, 'enqueue');
logger.error(message, properties);
expect(spy).toHaveBeenCalledWith(LOGGER_LEVEL.ERROR, message, properties);
expect(logger.sdk.error).toHaveBeenCalledWith(message, properties);
expect(logger.sdk.enqueue).toHaveBeenCalledWith('error', [ message, properties ]);
});
});
describe('track', (): void => {
it('should call analytics.track', (): void => {
const spy: jest.SpyInstance = jest.spyOn((window as WindowWithAnalytics).analytics, 'track');
logger.init(initOptions);
logger.identify(identifyOptions);
logger.track(message, properties, integrations);
expect(spy).toHaveBeenCalledWith(
message,
{ ...properties, ...identifyOptions },
{ integrations }
);
});
it('should call console.log', (): void => {
logger.init({ ...initOptions, development: true });
logger.identify(identifyOptions);
logger.track(message, properties, integrations);
expect(console.log).toHaveBeenCalledWith('TRACK', message, properties); // eslint-disable-line no-console
});
});
});
export enum LOGGER_LEVEL {
DEBUG = 'DEBUG',
INFO = 'INFO',
WARN = 'WARN',
ERROR = 'ERROR'
DEBUG = 'debug',
INFO = 'info',
WARN = 'warn',
ERROR = 'error'
}
export declare interface LoggerIdentificationOptions {
user_id: string;
export enum LOGGER_HANDLER {
HTTP = 'http',
CONSOLE = 'console',
SILENT = 'silent'
}
export enum LOGGER_ENVIRONMENT {
DEVELOPMENT = 'development',
TEST = 'test',
STAGING = 'staging',
BETA = 'beta',
PRODUCTION = 'production'
}
export declare interface UserSession {
app_version: string;
company: number;
debugging_session_id: string;
platform: string;
environment: string;
company_id: number;
cti_name?: string;
device: string;
device_details?: string;
electron_version?: string;
environment: LOGGER_ENVIRONMENT;
release: string;
user_id: number;
}
export declare interface LoggerInitOptions {
development?: boolean;
key: string;
verbose?: boolean;
context: LoggerContext;
token: string;
level: LOGGER_LEVEL;
}
export declare interface WindowWithAnalytics extends Window {
analytics: SegmentAnalytics.AnalyticsJS | AnalyticsJSMock;
export declare interface LoggerContext {
service: string;
user_session: UserSession;
}
export class AnalyticsJSMock extends Array {
public SNIPPET_VERSION: string = '4.1.0';
export declare interface SDKInitOptions {
clientToken: string;
forwardErrorsToLogs: boolean;
}
public identify(...args: (string | object)[]): void {
args.unshift('identify');
this.push(args);
export declare interface WindowWithDatadog extends Window {
DD_LOGS: {
logger: DatadogSDK;
init(options: SDKInitOptions): void;
};
}
export declare interface QueueItem {
method: keyof DatadogSDK;
args: (string | object | LOGGER_LEVEL)[];
}
export class DatadogSDK {
private queue: QueueItem[] = [];
public constructor(queue: QueueItem[]) {
this.queue = queue;
}
public track(...args: (string | object)[]): void {
args.unshift('track');
this.push(args);
private enqueue(method: keyof DatadogSDK, args: (string | object | LOGGER_LEVEL)[]): void {
this.queue.push({ method, args });
}
}
export default class Logger {
public log(message: string, properties: object, status: LOGGER_LEVEL): void {
this.enqueue('log', [ message, properties, status ]);
}
public constructor() {
(window as WindowWithAnalytics).analytics = new AnalyticsJSMock();
public debug(message: string, properties: object): void {
this.enqueue(LOGGER_LEVEL.DEBUG, [ message, properties ]);
}
private identified: boolean = false;
public info(message: string, properties: object): void {
this.enqueue(LOGGER_LEVEL.INFO, [ message, properties ]);
}
private identification: LoggerIdentificationOptions | null = null;
public warn(message: string, properties: object): void {
this.enqueue(LOGGER_LEVEL.WARN, [ message, properties ]);
}
private development: boolean = false;
public error(message: string, properties: object): void {
this.enqueue(LOGGER_LEVEL.ERROR, [ message, properties ]);
}
private verbose: boolean = false;
public init(options: SDKInitOptions): void {
this.enqueue('init', [ options ]);
}
private getScriptTag(key: LoggerInitOptions['key']): HTMLScriptElement {
public setLevel(level: LOGGER_LEVEL): void {
this.enqueue('setLevel', [ level ]);
}
public setContext(context: LoggerContext): void {
this.enqueue('setContext', [ context ]);
}
public setHandler(handler: LOGGER_HANDLER): void {
this.enqueue('setHandler', [ handler ]);
}
}
export const defaultContext: LoggerContext = {
service: 'default',
user_session: {
app_version: 'default',
company_id: 0,
device: 'default',
environment: LOGGER_ENVIRONMENT.DEVELOPMENT,
release: 'default',
user_id: 0
}
};
export default class Logger {
private queue: QueueItem[] = [];
private sdk: DatadogSDK = new DatadogSDK(this.queue);
private context: LoggerContext = defaultContext;
private token: string = '';
public level: LOGGER_LEVEL = LOGGER_LEVEL.INFO;
private getScriptTag(): HTMLScriptElement {
const script: HTMLScriptElement = document.createElement('script');

@@ -60,3 +137,3 @@

script.async = true;
script.src = `https://cdn.segment.com/analytics.js/v1/${key}/analytics.min.js`;
script.src = 'https://www.datadoghq-browser-agent.com/datadog-logs-us.js';

@@ -66,66 +143,62 @@ return script;

public get isVerbose(): boolean {
return this.verbose;
private loadSDK(): void {
const script: HTMLScriptElement = this.getScriptTag();
script.addEventListener('load', this.onLoad.bind(this));
document.head.appendChild(script);
}
public get isIdentified(): boolean {
return this.identified;
}
private onLoad(): void {
const { DD_LOGS } = (window as WindowWithDatadog);
const isDevelopment = this.context.user_session.environment === LOGGER_ENVIRONMENT.DEVELOPMENT;
public enableVerboseMode(): void {
this.verbose = true;
}
DD_LOGS.init({
clientToken: this.token,
forwardErrorsToLogs: !isDevelopment
});
public disableVerboseMode(): void {
this.verbose = false;
this.sdk = DD_LOGS.logger;
this.sdk.setContext(this.context);
this.sdk.setHandler(isDevelopment ? LOGGER_HANDLER.CONSOLE : LOGGER_HANDLER.HTTP);
this.sdk.setLevel(this.level);
this.queue.forEach(({ method, args }: QueueItem): void => {
// @ts-ignore
this.sdk[method](...args);
});
}
public init(options: LoggerInitOptions): void {
this.development = options.development || false;
this.verbose = options.verbose || false;
this.context = options.context;
this.token = options.token;
this.level = options.level;
const script: HTMLScriptElement = this.getScriptTag(options.key);
document.head.appendChild(script);
this.loadSDK();
}
public identify(options: LoggerIdentificationOptions): void {
(window as WindowWithAnalytics).analytics.identify(options.user_id, options);
this.identification = options;
this.identified = true;
public setLevel(level: LOGGER_LEVEL): void {
this.level = level;
this.sdk.setLevel(level);
}
public log(level: LOGGER_LEVEL, message: string, properties: object = {}): void {
if (this.development) {
console.log(level, message, properties); // eslint-disable-line no-console
} else if (level !== LOGGER_LEVEL.DEBUG || this.isVerbose) {
this.track('log', { level, message, ...properties }, { 'All': false, 'Amazon Kinesis': true });
}
this.sdk.log(message, properties, level);
}
public debug(message: string, properties: object = {}): void {
this.log(LOGGER_LEVEL.DEBUG, message, properties);
this.sdk.debug(message, properties);
}
public info(message: string, properties: object = {}): void {
this.log(LOGGER_LEVEL.INFO, message, properties);
this.sdk.info(message, properties);
}
public warn(message: string, properties: object = {}): void {
this.log(LOGGER_LEVEL.WARN, message, properties);
this.sdk.warn(message, properties);
}
public error(message: string, properties: object = {}): void {
this.log(LOGGER_LEVEL.ERROR, message, properties);
this.sdk.error(message, properties);
}
public track(message: string, properties: object = {}, integrations: object = {}): void {
if (this.development) {
console.log('TRACK', message, properties); // eslint-disable-line no-console
} else {
(window as WindowWithAnalytics).analytics.track(message, { ...properties, ...this.identification }, {
integrations: { All: false, ...integrations }
});
}
}
}

@@ -1,3 +0,3 @@

import logger, { LOGGER } from './';
import { VERBOSE_MODE_LIMIT_STORED_ACTIONS } from './constants';
import logger, { LOGGER, LOGGER_LEVEL } from './';
import { DEUBG_MODE_LIMIT_STORED_ACTIONS } from './constants';
import { loggerMiddleware } from './middleware';

@@ -13,4 +13,12 @@

it('should do nothing if level > debug', (): void => {
logger.setLevel(LOGGER_LEVEL.INFO);
store.dispatch(fooAction);
expect(handler).toHaveBeenCalledWith(undefined, fooAction);
store.dispatch(infoAction);
expect(handler).toHaveBeenCalledWith(undefined, { ...infoAction });
});
it('should intercept actions and add lastDispatchedActions property', (): void => {
logger.enableVerboseMode();
logger.setLevel(LOGGER_LEVEL.DEBUG);
store.dispatch(fooAction);

@@ -22,10 +30,10 @@ expect(handler).toHaveBeenCalledWith(undefined, fooAction);

it('should keep only the last VERBOSE_MODE_LIMIT_STORED_ACTIONS actions dispatched', (): void => {
logger.enableVerboseMode();
it('should keep only the last DEUBG_MODE_LIMIT_STORED_ACTIONS actions dispatched', (): void => {
logger.setLevel(LOGGER_LEVEL.DEBUG);
for (let i = 0; i < VERBOSE_MODE_LIMIT_STORED_ACTIONS + 5; i ++) {
for (let i = 0; i < DEUBG_MODE_LIMIT_STORED_ACTIONS + 5; i ++) {
store.dispatch(fooAction);
}
const lastDispatchedActions = (new Array(VERBOSE_MODE_LIMIT_STORED_ACTIONS)).fill(fooAction);
const lastDispatchedActions = (new Array(DEUBG_MODE_LIMIT_STORED_ACTIONS)).fill(fooAction);

@@ -32,0 +40,0 @@ store.dispatch(infoAction);

@@ -1,4 +0,4 @@

import logger from './';
import logger, { LOGGER_LEVEL } from './';
import { Action, Middleware, Dispatch } from 'redux';
import { VERBOSE_MODE_LIMIT_STORED_ACTIONS } from './constants';
import { DEUBG_MODE_LIMIT_STORED_ACTIONS } from './constants';
import { isLoggerAction } from './actions';

@@ -14,3 +14,3 @@

if (!logger.isVerbose) {
if (logger.level !== LOGGER_LEVEL.DEBUG) {
return next(action);

@@ -25,3 +25,3 @@ }

if (lastDispatchedActions.length > VERBOSE_MODE_LIMIT_STORED_ACTIONS) {
if (lastDispatchedActions.length > DEUBG_MODE_LIMIT_STORED_ACTIONS) {
lastDispatchedActions.shift();

@@ -28,0 +28,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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc