Skip to content

Commit

Permalink
feat(FEC-9023): More logger options (#90)
Browse files Browse the repository at this point in the history
add configuration logger options.
handler to output logs with customize function.
option to remove playerVersion from log.
  • Loading branch information
Yuvalke authored Jun 20, 2019
1 parent 07d7fa8 commit 8b62656
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 21 deletions.
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ PlayKit JS Providers is written in [ECMAScript6], analyzed statically using [Flo

## Table of Contents

* [Getting Started](#getting-started)
* [Installing](#installing)
* [Building](#building)
* [Embed the Library In Your Test Page](#embed-the-library-in-your-test-page)
* [Documentation](#documentation)
* [Running the Tests](#running-the-tests)
* [Compatibility](#compatibility)
* [Contributing](#contributing)
* [Versioning](#versioning)
* [Licensing](#licensing)
- [Getting Started](#getting-started)
- [Installing](#installing)
- [Building](#building)
- [Embed the Library In Your Test Page](#embed-the-library-in-your-test-page)
- [Documentation](#documentation)
- [Running the Tests](#running-the-tests)
- [Compatibility](#compatibility)
- [Contributing](#contributing)
- [Versioning](#versioning)
- [Licensing](#licensing)

## Getting Started

Expand Down Expand Up @@ -61,7 +61,9 @@ Finally, add the bundle as a script tag in your page, and initialize the provide
var options = {
partnerId: "YOUR_PARTNER_ID", // Mandatory
ks: "YOUR_KS", // Optional
logLevel: "LOG_LEVEL", // Optional
log:{
level: "LOG_LEVEL", // Optional
}
uiConfId: UI_CONF_ID, // Optional
env: { // Optional
serviceUrl: "YOUR_SERVICE_URL",
Expand Down Expand Up @@ -92,7 +94,9 @@ provider.getMediaConfig(mediaInfo).then(function(mediaConfig) {
var options = {
partnerId: "YOUR_PARTNER_ID", // Mandatory
ks: "YOUR_KS", // Optional
logLevel: "LOG_LEVEL", // Optional
log:{
level:"LOG_LEVEL", // Optional
}
uiConfId: UI_CONF_ID, // Optional
env: { // Optional
serviceUrl: "YOUR_SERVICE_URL",
Expand All @@ -119,8 +123,8 @@ provider.getMediaConfig(mediaInfo).then(function(mediaConfig) {

## Documentation

* **[Configuration](docs/configuration.md)**
* **API**
- **[Configuration](docs/configuration.md)**
- **API**

## Running the Tests

Expand Down
14 changes: 12 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var provider = new playkit.providers.ott.Provider(config);
```js
{
partnerId: number,
logLevel: string, // optional
log: ProviderLogConfigObject, // optional
ks: string, // optional
uiConfId: number, // optional
env: ProviderEnvConfigObject, // optional
Expand All @@ -36,7 +36,11 @@ var provider = new playkit.providers.ott.Provider(config);

##

> ### config.logLevel
> ### config.log
>
> ##### Type: `Object`
>
> ### config.log.level
>
> ##### Type: `string`
>
Expand All @@ -45,6 +49,12 @@ var provider = new playkit.providers.ott.Provider(config);
> ##### Description: Defines the provider log level.
>
> Possible values: `"DEBUG", "INFO", "TIME", "WARN", "ERROR", "OFF"`
>
> ### config.log.handler
>
> ##### Type: `function`
>
> ##### Description: Defines the log handler function by default will write to console.
##

Expand Down
5 changes: 5 additions & 0 deletions flow-typed/types/log-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare type LogHandlerType = (messages: any[], context: Object) => void;
declare type ProviderLogConfigObject = {
level: string,
handler: ?LogHandlerType
};
2 changes: 1 addition & 1 deletion flow-typed/types/provider-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
declare type ProviderOptionsObject = {
partnerId: number,
widgetId?: string,
logLevel?: string,
log?: ProviderLogConfigObject,
ks?: string,
uiConfId?: number,
env?: ProviderEnvConfigObject,
Expand Down
9 changes: 6 additions & 3 deletions src/k-provider/common/base-provider.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import {setLogLevel, getLogLevel, LogLevel} from '../../util/logger';
import {setLogLevel, getLogLevel, LogLevel, setLogHandler} from '../../util/logger';
import DataLoaderManager from './data-loader-manager';
import Error from '../../util/error/error';

Expand Down Expand Up @@ -53,8 +53,11 @@ export default class BaseProvider<MI> {
this._isAnonymous = !options.ks;
this._ks = options.ks || '';
this._playerVersion = playerVersion;
if (options.logLevel && this.LogLevel[options.logLevel]) {
setLogLevel(this.LogLevel[options.logLevel]);
if (options.log && options.log.level && this.LogLevel[options.log.level]) {
setLogLevel(this.LogLevel[options.log.level]);
}
if (options.log && typeof options.log.handler === 'function') {
setLogHandler(options.log.handler);
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/util/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ const LogLevel: LogLevelType = {

JsLogger.useDefaults({defaultLevel: JsLogger.ERROR});

/**
* sets the logger handler
* @param {LogHandlerType} handler - the log level
* @returns {void}
*/
function setLogHandler(handler: LogHandlerType): void {
JsLogger.setHandler((messages, context) => handler(messages, context));
}
/**
* get a logger
* @param {?string} name - the logger name
Expand Down Expand Up @@ -47,4 +55,4 @@ function setLogLevel(level: LogLevelObject, name?: string): void {
}

export default getLogger;
export {LogLevel, getLogLevel, setLogLevel};
export {LogLevel, getLogLevel, setLogLevel, setLogHandler};

0 comments on commit 8b62656

Please sign in to comment.