@aircall/logger
Advanced tools
Comparing version 0.0.13 to 0.0.14
@@ -1,2 +0,2 @@ | ||
import { LoggerLevel } from './Logger'; | ||
import { LOGGER_LEVEL } from './Logger'; | ||
export declare enum LOGGER { | ||
@@ -18,4 +18,4 @@ INIT = "LOGGER_INIT", | ||
}; | ||
export declare const log: (level: LoggerLevel, message: string, properties?: object) => LoggerLog & { | ||
level: LoggerLevel; | ||
export declare const log: (level: LOGGER_LEVEL, message: string, properties?: object) => LoggerLog & { | ||
level: LOGGER_LEVEL; | ||
}; | ||
@@ -22,0 +22,0 @@ export declare const info: (message: string, properties?: object) => LoggerLog; |
@@ -5,3 +5,7 @@ /// <reference types="segment-analytics" /> | ||
} | ||
export declare type LoggerLevel = 'INFO' | 'WARN' | 'ERROR'; | ||
export declare enum LOGGER_LEVEL { | ||
INFO = "INFO", | ||
WARN = "WARN", | ||
ERROR = "ERROR" | ||
} | ||
export declare interface LoggerIdentificationOptions { | ||
@@ -25,9 +29,11 @@ user_id: string; | ||
private getScriptTag; | ||
readonly isInitialized: boolean; | ||
readonly isIdentified: boolean; | ||
init(options: LoggerInitOptions): void; | ||
identify(options: LoggerIdentificationOptions): void; | ||
log(level: LoggerLevel, message: string, properties?: object): void; | ||
log(level: LOGGER_LEVEL, message: string, properties?: object): void; | ||
info(message: string, properties?: object): void; | ||
warn(message: string, properties?: object): void; | ||
error(message: string, properties?: object): void; | ||
track(event: string, properties?: object, integrations?: object): void; | ||
track(message: string, properties?: object, integrations?: object): void; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var LOGGER_LEVEL; | ||
(function (LOGGER_LEVEL) { | ||
LOGGER_LEVEL["INFO"] = "INFO"; | ||
LOGGER_LEVEL["WARN"] = "WARN"; | ||
LOGGER_LEVEL["ERROR"] = "ERROR"; | ||
})(LOGGER_LEVEL = exports.LOGGER_LEVEL || (exports.LOGGER_LEVEL = {})); | ||
class Logger { | ||
@@ -18,8 +24,14 @@ constructor() { | ||
script.src = `https://cdn.segment.com/analytics.js/v1/${key}/analytics.min.js`; | ||
script.addEventListener('load', this.onInitialized.bind(this)); | ||
return script; | ||
} | ||
get isInitialized() { | ||
return this.initialized; | ||
} | ||
get isIdentified() { | ||
return this.identified; | ||
} | ||
init(options) { | ||
this.debugMode = options.debugMode; | ||
const script = this.getScriptTag(options.key); | ||
this.debugMode = options.debugMode; | ||
script.addEventListener('load', this.onInitialized.bind(this)); | ||
document.head.appendChild(script); | ||
@@ -41,16 +53,16 @@ } | ||
info(message, properties = {}) { | ||
this.log('INFO', message, properties); | ||
this.log(LOGGER_LEVEL.INFO, message, properties); | ||
} | ||
warn(message, properties = {}) { | ||
this.log('WARN', message, properties); | ||
this.log(LOGGER_LEVEL.WARN, message, properties); | ||
} | ||
error(message, properties = {}) { | ||
this.log('ERROR', message, properties); | ||
this.log(LOGGER_LEVEL.ERROR, message, properties); | ||
} | ||
track(event, properties = {}, integrations = {}) { | ||
track(message, properties = {}, integrations = {}) { | ||
if (this.debugMode) { | ||
console.log('TRACK', event, properties); // eslint-disable-line no-console | ||
console.log('TRACK', message, properties); // eslint-disable-line no-console | ||
} | ||
else { | ||
window.analytics.track(event, Object.assign({}, properties, this.identification), { | ||
window.analytics.track(message, Object.assign({}, properties, this.identification), { | ||
integrations: Object.assign({ All: false }, integrations) | ||
@@ -57,0 +69,0 @@ }); |
{ | ||
"name": "@aircall/logger", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"main": "dist/index.js", | ||
@@ -13,3 +13,3 @@ "types": "dist/index.d.ts", | ||
}, | ||
"gitHead": "20fd9b31d086e850aff5c4d461eb4cc3cc2a8695", | ||
"gitHead": "39ddd135130b8ca0f7526d61ffe7b752087b11ba", | ||
"devDependencies": { | ||
@@ -16,0 +16,0 @@ "@types/segment-analytics": "^0.0.31" |
@@ -1,2 +0,2 @@ | ||
import { LoggerLevel } from './Logger'; | ||
import { LOGGER_LEVEL } from './Logger'; | ||
@@ -22,3 +22,3 @@ export enum LOGGER { | ||
export const log = (level: LoggerLevel, message: string, properties: object = {}): LoggerLog & { level: LoggerLevel } => ({ | ||
export const log = (level: LOGGER_LEVEL, message: string, properties: object = {}): LoggerLog & { level: LOGGER_LEVEL } => ({ | ||
type: LOGGER.LOG, | ||
@@ -25,0 +25,0 @@ level, |
@@ -1,2 +0,2 @@ | ||
import Logger from './Logger'; | ||
import Logger, { LOGGER_LEVEL, WindowWithAnalytics } from './Logger'; | ||
@@ -8,2 +8,16 @@ const initOptions = { | ||
const identifyOptions = { | ||
user_id: '1234', | ||
app_version: '1.0.0', | ||
company: 1234, | ||
platform: 'platform', | ||
environment: 'production' | ||
}; | ||
const message = 'MESSAGE'; | ||
const properties = { foo: 'BAR' }; | ||
const integrations = { All: false, 'Amazon Kinesis': true }; | ||
let logger = new Logger(); | ||
@@ -22,3 +36,2 @@ | ||
describe('init', (): void => { | ||
it('should inject a script tag', (): void => { | ||
@@ -29,7 +42,86 @@ const spy: jest.SpyInstance = jest.spyOn(document.head, 'appendChild'); | ||
}); | ||
}); | ||
describe('identify', (): void => { | ||
it('should set identified', (): void => { | ||
logger.init(initOptions); | ||
expect(logger.isIdentified).toBeFalsy(); | ||
logger.identify(identifyOptions); | ||
expect(logger.isIdentified).toBeTruthy(); | ||
}); | ||
}); | ||
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 console.log', (): void => { | ||
logger.init({ ...initOptions, debugMode: true }); | ||
logger.log(LOGGER_LEVEL.INFO, message, properties); | ||
expect(console.log).toHaveBeenCalledWith('LOG', LOGGER_LEVEL.INFO, message, properties); // eslint-disable-line no-console | ||
}); | ||
}); | ||
describe('info', (): void => { | ||
it('should call log', (): void => { | ||
const spy: jest.SpyInstance = jest.spyOn(logger, 'log'); | ||
logger.info(message, properties); | ||
expect(spy).toHaveBeenCalledWith(LOGGER_LEVEL.INFO, message, properties); | ||
}); | ||
}); | ||
describe('warn', (): void => { | ||
it('should call log', (): void => { | ||
const spy: jest.SpyInstance = jest.spyOn(logger, 'log'); | ||
logger.warn(message, properties); | ||
expect(spy).toHaveBeenCalledWith(LOGGER_LEVEL.WARN, message, properties); | ||
}); | ||
}); | ||
describe('error', (): void => { | ||
it('should call log', (): void => { | ||
const spy: jest.SpyInstance = jest.spyOn(logger, 'log'); | ||
logger.error(message, properties); | ||
expect(spy).toHaveBeenCalledWith(LOGGER_LEVEL.ERROR, message, properties); | ||
}); | ||
}); | ||
describe('track', (): void => { | ||
it('should call analytics.track', (): void => { | ||
logger.init(initOptions); | ||
logger.identify(identifyOptions); | ||
logger.track(message, properties, integrations); | ||
expect((window as WindowWithAnalytics).analytics.track).toHaveBeenCalledWith( | ||
message, | ||
{ ...properties, ...identifyOptions }, | ||
{ integrations } | ||
); | ||
}); | ||
it('should call console.log', (): void => { | ||
logger.init({ ...initOptions, debugMode: true }); | ||
logger.identify(identifyOptions); | ||
logger.track(message, properties, integrations); | ||
expect(console.log).toHaveBeenCalledWith('TRACK', message, properties); // eslint-disable-line no-console | ||
}); | ||
}); | ||
}); | ||
@@ -5,3 +5,7 @@ export declare interface WindowWithAnalytics extends Window { | ||
export declare type LoggerLevel = 'INFO' | 'WARN' | 'ERROR'; | ||
export enum LOGGER_LEVEL { | ||
INFO = 'INFO', | ||
WARN = 'WARN', | ||
ERROR = 'ERROR' | ||
} | ||
@@ -41,3 +45,2 @@ export declare interface LoggerIdentificationOptions { | ||
script.src = `https://cdn.segment.com/analytics.js/v1/${key}/analytics.min.js`; | ||
script.addEventListener('load', this.onInitialized.bind(this)); | ||
@@ -47,7 +50,15 @@ return script; | ||
public get isInitialized(): boolean { | ||
return this.initialized; | ||
} | ||
public get isIdentified(): boolean { | ||
return this.identified; | ||
} | ||
public init(options: LoggerInitOptions): void { | ||
const script: HTMLScriptElement = this.getScriptTag(options.key); | ||
this.debugMode = options.debugMode; | ||
const script: HTMLScriptElement = this.getScriptTag(options.key); | ||
script.addEventListener('load', this.onInitialized.bind(this)); | ||
document.head.appendChild(script); | ||
@@ -62,3 +73,3 @@ } | ||
public log(level: LoggerLevel, message: string, properties: object = {}): void { | ||
public log(level: LOGGER_LEVEL, message: string, properties: object = {}): void { | ||
if (this.debugMode) { | ||
@@ -72,18 +83,18 @@ console.log('LOG', level, message, properties); // eslint-disable-line no-console | ||
public info(message: string, properties: object = {}): void { | ||
this.log('INFO', message, properties); | ||
this.log(LOGGER_LEVEL.INFO, message, properties); | ||
} | ||
public warn(message: string, properties: object = {}): void { | ||
this.log('WARN', message, properties); | ||
this.log(LOGGER_LEVEL.WARN, message, properties); | ||
} | ||
public error(message: string, properties: object = {}): void { | ||
this.log('ERROR', message, properties); | ||
this.log(LOGGER_LEVEL.ERROR, message, properties); | ||
} | ||
public track(event: string, properties: object = {}, integrations: object = {}): void { | ||
public track(message: string, properties: object = {}, integrations: object = {}): void { | ||
if (this.debugMode) { | ||
console.log('TRACK', event, properties); // eslint-disable-line no-console | ||
console.log('TRACK', message, properties); // eslint-disable-line no-console | ||
} else { | ||
(window as WindowWithAnalytics).analytics.track(event, { ...properties, ...this.identification }, { | ||
(window as WindowWithAnalytics).analytics.track(message, { ...properties, ...this.identification }, { | ||
integrations: { All: false, ...integrations } | ||
@@ -90,0 +101,0 @@ }); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
17799
17
434