diff --git a/.eslintrc.json b/.eslintrc.json index d857e499e..5b620a1ca 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -20,8 +20,7 @@ "should": true, "sinon": true, "__VERSION__": true, - "__NAME__": true, - "__PACKAGE_URL__": true + "__NAME__": true }, "rules": { "mocha-no-only/mocha-no-only": "off", diff --git a/src/drm/base-drm-protocol.js b/src/drm/base-drm-protocol.js index dc77094e7..fe6b821c0 100644 --- a/src/drm/base-drm-protocol.js +++ b/src/drm/base-drm-protocol.js @@ -1,12 +1,12 @@ // @flow /* eslint-disable no-unused-vars */ -import LoggerFactory from '../utils/logger' +import getLogger from '../utils/logger' import PlayerError from '../utils/player-error' import DrmSupport from './drm-support' import {DrmScheme} from './drm-scheme' export default class BaseDrmProtocol implements IDrmProtocol { - static getLogger = LoggerFactory.getLogger; + static getLogger = getLogger; static DrmSupport = DrmSupport; static DrmScheme = DrmScheme; diff --git a/src/drm/drm-support.js b/src/drm/drm-support.js index bfc73bb56..a8a6a8445 100644 --- a/src/drm/drm-support.js +++ b/src/drm/drm-support.js @@ -1,12 +1,12 @@ // @flow import Env from '../utils/env' -import LoggerFactory from '../utils/logger' +import getLogger from '../utils/logger' import {DrmScheme} from './drm-scheme' const NOT_SUPPORTED: string = 'not_supported_drm_playback'; export default class DrmSupport { - static _logger = LoggerFactory.getLogger('DrmSupport'); + static _logger = getLogger('DrmSupport'); static _Browsers: Object = { Safari: () => { let device = Env.device.type; diff --git a/src/engines/html5/media-source/base-media-source-adapter.js b/src/engines/html5/media-source/base-media-source-adapter.js index 2753f8b7e..b78094c52 100644 --- a/src/engines/html5/media-source/base-media-source-adapter.js +++ b/src/engines/html5/media-source/base-media-source-adapter.js @@ -4,7 +4,7 @@ import FakeEvent from '../../../event/fake-event' import FakeEventTarget from '../../../event/fake-event-target' import PlayerError from '../../../utils/player-error' import {CUSTOM_EVENTS} from '../../../event/events' -import LoggerFactory from '../../../utils/logger' +import getLogger from '../../../utils/logger' import Track from '../../../track/track' import VideoTrack from '../../../track/video-track' import AudioTrack from '../../../track/audio-track' @@ -22,7 +22,7 @@ export default class BaseMediaSourceAdapter extends FakeEventTarget implements I * @type {Function} * @static */ - static getLogger: Function = LoggerFactory.getLogger; + static getLogger: Function = getLogger; /** * The adapter config. diff --git a/src/engines/html5/media-source/media-source-provider.js b/src/engines/html5/media-source/media-source-provider.js index edddb7eba..c3a90fa0b 100644 --- a/src/engines/html5/media-source/media-source-provider.js +++ b/src/engines/html5/media-source/media-source-provider.js @@ -1,6 +1,6 @@ //@flow import NativeAdapter from './adapters/native-adapter' -import LoggerFactory from '../../../utils/logger' +import getLogger from '../../../utils/logger' /** * Media source provider @@ -13,7 +13,7 @@ export default class MediaSourceProvider { * @static * @private */ - static _logger: any = LoggerFactory.getLogger('MediaSourceProvider'); + static _logger: any = getLogger('MediaSourceProvider'); /** * The media source adapter registry. * @member {Array} _mediaSourceAdapters diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js index 3c6628984..c9b35190b 100644 --- a/src/middleware/middleware.js +++ b/src/middleware/middleware.js @@ -1,7 +1,7 @@ // @flow import MultiMap from '../utils/multi-map' import BaseMiddleware from './base-middleware' -import LoggerFactory from '../utils/logger' +import getLogger from '../utils/logger' /** * Generic middleware implementation. @@ -33,7 +33,7 @@ export default class Middleware { constructor(actions: { [action: string]: string }) { this._actions = actions; this._middlewares = new MultiMap(); - this._logger = LoggerFactory.getLogger("Middleware"); + this._logger = getLogger("Middleware"); } /** diff --git a/src/player.js b/src/player.js index b8ff83dff..a540264b2 100644 --- a/src/player.js +++ b/src/player.js @@ -8,7 +8,7 @@ import {PLAYER_EVENTS as PlayerEvents, HTML5_EVENTS as Html5Events, CUSTOM_EVENT import PlayerStates from './state/state-types' import * as Utils from './utils/util' import Locale from './utils/locale' -import LoggerFactory from './utils/logger' +import getLogger, {LogLevel, getLogLevel, setLogLevel} from './utils/logger' import Html5 from './engines/html5/html5' import PluginManager from './plugin/plugin-manager' import BasePlugin from './plugin/base-plugin' @@ -107,7 +107,7 @@ export default class Player extends FakeEventTarget { * @static * @private */ - static _logger: any = LoggerFactory.getLogger('Player'); + static _logger: any = getLogger('Player'); /** * The available engines of the player. * @type {Array} @@ -335,6 +335,9 @@ export default class Player extends FakeEventTarget { * @returns {void} */ configure(config: Object): void { + if (config.logLevel && LogLevel[config.logLevel]) { + setLogLevel(LogLevel[config.logLevel]); + } Utils.Object.mergeDeep(this._config, config); this._configureOrLoadPlugins(config.plugins); if (!Utils.Object.isEmptyObject(config.sources)) { @@ -1590,5 +1593,32 @@ export default class Player extends FakeEventTarget { return TrackTypes; } + /** + * Get the player log level. + * @returns {Object} - The log levels of the player. + * @public + */ + get LogLevel(): { [level: string]: Object } { + return LogLevel; + } + // + /** + * get the log level + * @param {?string} name - the logger name + * @returns {Object} - the log level + */ + getLogLevel(name?: string): Object { + return getLogLevel(name); + } + + /** + * sets the logger level + * @param {Object} level - the log level + * @param {?string} name - the logger name + * @returns {void} + */ + setLogLevel(level: Object, name?: string) { + setLogLevel(level, name); + } } diff --git a/src/playkit.js b/src/playkit.js index d57df5708..e6b44df23 100644 --- a/src/playkit.js +++ b/src/playkit.js @@ -1,6 +1,5 @@ //@flow import Player from './player' -import LoggerFactory from './utils/logger' import BaseMediaSourceAdapter from './engines/html5/media-source/base-media-source-adapter' import {registerMediaSourceAdapter} from './engines/html5/media-source/media-source-provider' import {registerPlugin} from './plugin/plugin-manager' @@ -19,10 +18,6 @@ Player.runCapabilities(); declare var __VERSION__: string; declare var __NAME__: string; -declare var __PACKAGE_URL__: string; - -LoggerFactory.getLogger().log(`%c ${__NAME__} ${__VERSION__}`, "color: #98ff98; font-size: large"); -LoggerFactory.getLogger().log(`%c For more details see ${__PACKAGE_URL__}`, "color: #98ff98;"); /** * @param {Object} config - The configuration of the player diff --git a/src/plugin/base-plugin.js b/src/plugin/base-plugin.js index 797c89077..3fff59139 100644 --- a/src/plugin/base-plugin.js +++ b/src/plugin/base-plugin.js @@ -1,6 +1,6 @@ //@flow import Player from '../player' -import LoggerFactory from '../utils/logger' +import getLogger from '../utils/logger' import * as Utils from '../utils/util' import EventManager from '../event/event-manager' import PlayerError from '../utils/player-error' @@ -83,7 +83,7 @@ export default class BasePlugin implements IPlugin { this.name = name; this.player = player; this.eventManager = new EventManager(); - this.logger = LoggerFactory.getLogger(Utils.String.capitlize(this.name)); + this.logger = getLogger(Utils.String.capitlize(this.name)); this.config = {}; Utils.Object.mergeDeep(this.config, this.constructor.defaultConfig, config); } diff --git a/src/plugin/plugin-manager.js b/src/plugin/plugin-manager.js index ffa9f93dd..2a28809bc 100644 --- a/src/plugin/plugin-manager.js +++ b/src/plugin/plugin-manager.js @@ -2,14 +2,14 @@ import BasePlugin from './base-plugin' import PlayerError from '../utils/player-error' import Player from '../player' -import LoggerFactory from '../utils/logger' +import getLogger from '../utils/logger' /** * The logger of the PluginManager class. * @private * @const */ -const logger = LoggerFactory.getLogger("PluginManager"); +const logger = getLogger("PluginManager"); /** The PluginManager responsible for register plugins definitions and store plugins instances. * @classdesc diff --git a/src/state/state-manager.js b/src/state/state-manager.js index 0d278f6e9..44ef3b637 100644 --- a/src/state/state-manager.js +++ b/src/state/state-manager.js @@ -5,7 +5,7 @@ import State from './state' import PlayerStates from './state-types' import {HTML5_EVENTS as Html5Events, CUSTOM_EVENTS as CustomEvents} from '../event/events' import FakeEvent from '../event/fake-event' -import LoggerFactory from '../utils/logger' +import getLogger from '../utils/logger' /** * This class responsible to manage all the state machine of the player. @@ -136,7 +136,7 @@ export default class StateManager { */ constructor(player: Player) { this._player = player; - this._logger = LoggerFactory.getLogger("StateManager"); + this._logger = getLogger("StateManager"); this._eventManager = new EventManager(); this._history = []; this._prevState = null; diff --git a/src/utils/logger.js b/src/utils/logger.js index 5c9a01c4b..13f6f9064 100644 --- a/src/utils/logger.js +++ b/src/utils/logger.js @@ -1,7 +1,7 @@ //@flow import * as JsLogger from 'js-logger'; -const LOG_LEVEL: { [level: string]: Object } = { +const LogLevel: { [level: string]: Object } = { "DEBUG": JsLogger.DEBUG, "INFO": JsLogger.INFO, "TIME": JsLogger.TIME, @@ -10,20 +10,38 @@ const LOG_LEVEL: { [level: string]: Object } = { "OFF": JsLogger.OFF }; -class LoggerFactory { - constructor(options?: Object) { - JsLogger.useDefaults(options || {}); - } +JsLogger.useDefaults({defaultLevel: JsLogger.ERROR}); - getLogger(name?: string) { - if (!name) { - return JsLogger; - } - return JsLogger.get(name); +/** + * get a logger + * @param {?string} name - the logger name + * @returns {Object} - the logger class + */ +function getLogger(name?: string): Object { + if (!name) { + return JsLogger; } + return JsLogger.get(name); +} + +/** + * get the log level + * @param {?string} name - the logger name + * @returns {Object} - the log level + */ +function getLogLevel(name?: string): Object { + return getLogger(name).getLevel(); } -const lf = new LoggerFactory({defaultLevel: JsLogger.DEBUG}); +/** + * sets the logger level + * @param {Object} level - the log level + * @param {?string} name - the logger name + * @returns {void} + */ +function setLogLevel(level: Object, name?: string): void { + getLogger(name).setLevel(level); +} -export default lf; -export {LOG_LEVEL}; +export default getLogger; +export {LogLevel, getLogLevel, setLogLevel}; diff --git a/test/src/middleware/middleware.spec.js b/test/src/middleware/middleware.spec.js index b3b8c3ce5..0e68d8328 100644 --- a/test/src/middleware/middleware.spec.js +++ b/test/src/middleware/middleware.spec.js @@ -1,10 +1,10 @@ import BaseMiddleware from '../../../src/middleware/base-middleware' import Middleware from '../../../src/middleware/middleware' -import LoggerFactory from '../../../src/utils/logger' +import getLogger from '../../../src/utils/logger' class M1 extends BaseMiddleware { id = 'M1'; - logger = LoggerFactory.getLogger(this.id); + logger = getLogger(this.id); drink(next) { this.logger.debug('drink'); @@ -19,7 +19,7 @@ class M1 extends BaseMiddleware { class M2 extends BaseMiddleware { id = 'M2'; - logger = LoggerFactory.getLogger(this.id); + logger = getLogger(this.id); drink(next) { this.logger.debug('drink'); @@ -34,7 +34,7 @@ class M2 extends BaseMiddleware { class M3 extends BaseMiddleware { id = 'M3'; - logger = LoggerFactory.getLogger(this.id); + logger = getLogger(this.id); drink(next) { this.logger.debug('drink'); diff --git a/test/src/middleware/playback-middleware.spec.js b/test/src/middleware/playback-middleware.spec.js index d07e84f9f..f62c8adca 100644 --- a/test/src/middleware/playback-middleware.spec.js +++ b/test/src/middleware/playback-middleware.spec.js @@ -1,10 +1,10 @@ import BaseMiddleware from '../../../src/middleware/base-middleware' import PlaybackMiddleware from '../../../src/middleware/playback-middleware' -import LoggerFactory from '../../../src/utils/logger' +import getLogger from '../../../src/utils/logger' class PM1 extends BaseMiddleware { id = 'PM1'; - logger = LoggerFactory.getLogger(this.id); + logger = getLogger(this.id); play(next) { this.logger.debug('play'); @@ -19,7 +19,7 @@ class PM1 extends BaseMiddleware { class PM2 extends BaseMiddleware { id = 'PM2'; - logger = LoggerFactory.getLogger(this.id); + logger = getLogger(this.id); play(next) { this.logger.debug('play'); @@ -29,7 +29,7 @@ class PM2 extends BaseMiddleware { class PM3 extends BaseMiddleware { id = 'PM3'; - logger = LoggerFactory.getLogger(this.id); + logger = getLogger(this.id); pause(next) { this.logger.debug('pause'); diff --git a/test/src/player.spec.js b/test/src/player.spec.js index 05bba1648..b3892a02e 100644 --- a/test/src/player.spec.js +++ b/test/src/player.spec.js @@ -2649,7 +2649,6 @@ describe('_getLanguage', function () { }); }); - describe('_resetTextCuesAndReposition', function () { let config, player, sandbox; @@ -2665,10 +2664,32 @@ describe('_resetTextCuesAndReposition', function () { }); it('should reset the active text cues', () => { - player._activeTextCues[0]={}; + player._activeTextCues[0] = {}; player._resetTextCuesAndReposition(); let cue = player._activeTextCues[0]; cue.hasBeenReset.should.equals(true); }); }); +describe('logger', () => { + it('should return the current log level', () => { + const player = new Player(); + const currentLogLevel = player.getLogLevel(); + currentLogLevel.should.equal(player.LogLevel.ERROR); + }); + + it('should enable setting the current log level from API', () => { + const player = new Player(); + let currentLogLevel = player.getLogLevel(); + currentLogLevel.should.equal(player.LogLevel.ERROR); + player.setLogLevel(player.LogLevel.WARN); + currentLogLevel = player.getLogLevel(); + currentLogLevel.should.equal(player.LogLevel.WARN); + }); + + it('should enable setting the current log level from config', () => { + const player = new Player({logLevel: "DEBUG"}); + let currentLogLevel = player.getLogLevel(); + currentLogLevel.should.equal(player.LogLevel.DEBUG); + }); +}); diff --git a/test/src/utils/logger.spec.js b/test/src/utils/logger.spec.js index b89b2284c..69f90c3e8 100644 --- a/test/src/utils/logger.spec.js +++ b/test/src/utils/logger.spec.js @@ -1,5 +1,5 @@ -import LoggerFactory from '../../../src/utils/logger' -import {LOG_LEVEL} from '../../../src/utils/logger' +import getLogger from '../../../src/utils/logger' +import {LogLevel} from '../../../src/utils/logger' describe('LoggerFactory', () => { @@ -7,8 +7,8 @@ describe('LoggerFactory', () => { let loggerB = null; beforeEach(() => { - loggerA = LoggerFactory.getLogger('loggerA'); - loggerB = LoggerFactory.getLogger('loggerB'); + loggerA = getLogger('loggerA'); + loggerB = getLogger('loggerB'); }); it('should create two separate loggers', () => { @@ -17,8 +17,8 @@ describe('LoggerFactory', () => { }); it('should change loggerA\'s log level', () => { - loggerA.setLevel(LOG_LEVEL.WARN); - (loggerA.context.filterLevel).should.deep.equal(LOG_LEVEL.WARN); - (loggerB.context.filterLevel).should.deep.equal(LOG_LEVEL.DEBUG); + loggerA.setLevel(LogLevel.WARN); + (loggerA.context.filterLevel).should.deep.equal(LogLevel.WARN); + (loggerB.context.filterLevel).should.deep.equal(LogLevel.DEBUG); }); }); diff --git a/webpack.config.js b/webpack.config.js index dfe1bdcd0..f7594fee9 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -8,12 +8,11 @@ const packageData = require("./package.json"); let plugins = [ new webpack.DefinePlugin({ __VERSION__: JSON.stringify(packageData.version), - __NAME__: JSON.stringify(packageData.name), - __PACKAGE_URL__: JSON.stringify(packageData.repository.url) + __NAME__: JSON.stringify(packageData.name) }) ]; -if (PROD){ +if (PROD) { plugins.push(new webpack.optimize.UglifyJsPlugin({sourceMap: true})); } diff --git a/yarn.lock b/yarn.lock index 2f7749515..695eb796e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3023,8 +3023,8 @@ js-base64@^2.1.9: resolved "https://registry.yarnpkg.com/js-base64/-/js-base64-2.1.9.tgz#f0e80ae039a4bd654b5f281fc93f04a914a7fcce" js-logger@^1.3.0: - version "1.3.0" - resolved "https://registry.yarnpkg.com/js-logger/-/js-logger-1.3.0.tgz#de1a1143242710f7ce4da20408348a2a93ebf34d" + version "1.4.1" + resolved "https://registry.yarnpkg.com/js-logger/-/js-logger-1.4.1.tgz#f0230dc5e84e120f213d6e5a6b767a913d290335" js-tokens@^3.0.0: version "3.0.1"