Skip to content

Commit

Permalink
refactor: Integrate logging package (#755)
Browse files Browse the repository at this point in the history
## Summary
- Integrate logging package in `optimizely-sdk` package.

## Test plan
- All unit tests and Full stack compatibility suite tests pass.

## Jira
[OASIS-8204](https://optimizely.atlassian.net/browse/OASIS-8204)
  • Loading branch information
ozayr-zaviar committed Aug 2, 2022
1 parent 71fcef4 commit fff21e0
Show file tree
Hide file tree
Showing 34 changed files with 898 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
import sinon from 'sinon';
import { assert } from 'chai';
import { getLogger } from '@optimizely/js-sdk-logging';
import { sprintf } from '../../utils/fns';
import { getLogger } from '../../modules/logging';

import { createAudienceEvaluator } from './index';
import * as conditionTreeEvaluator from '../condition_tree_evaluator';
Expand Down
4 changes: 2 additions & 2 deletions packages/optimizely-sdk/lib/core/audience_evaluator/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016, 2018-2021, Optimizely
* Copyright 2016, 2018-2022, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { getLogger } from '@optimizely/js-sdk-logging';
import { getLogger } from '../../modules/logging';

import fns from '../../utils/fns';
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/optimizely-sdk/lib/core/bucketer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
import { sprintf } from '../../utils/fns';
import murmurhash from 'murmurhash';
import { LogHandler } from '@optimizely/js-sdk-logging';
import { LogHandler } from '../../modules/logging';
import {
DecisionResponse,
BucketerParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
LOG_LEVEL,
LOG_MESSAGES,
} from '../../utils/enums';
import * as logging from '@optimizely/js-sdk-logging';
import * as logging from '../../modules/logging';
import * as customAttributeEvaluator from './';

var browserConditionSafari = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2018-2019, 2020 Optimizely, Inc. and contributors *
* Copyright 2018-2019, 2020, 2022, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand All @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/
import { getLogger } from '@optimizely/js-sdk-logging';
import { getLogger } from '../../modules/logging';
import { UserAttributes, Condition } from '../../shared_types';

import fns from '../../utils/fns';
Expand Down
2 changes: 1 addition & 1 deletion packages/optimizely-sdk/lib/core/decision_service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and *
* limitations under the License. *
***************************************************************************/
import { LogHandler } from '@optimizely/js-sdk-logging';
import { LogHandler } from '../../modules/logging';
import { sprintf } from '../../utils/fns';

import fns from '../../utils/fns';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2019-2021, Optimizely
* Copyright 2019-2022, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { getLogger } from '@optimizely/js-sdk-logging';
import { getLogger } from '../../modules/logging';

import fns from '../../utils/fns';
import * as eventTagUtils from '../../utils/event_tag_utils';
Expand Down
4 changes: 2 additions & 2 deletions packages/optimizely-sdk/lib/core/event_builder/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2016-2021, Optimizely
* Copyright 2016-2022, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { LoggerFacade } from '@optimizely/js-sdk-logging';
import { LoggerFacade } from '../../modules/logging';
import { EventV1 as CommonEventParams } from '@optimizely/js-sdk-event-processor';

import fns from '../../utils/fns';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { LogHandler, ErrorHandler } from '@optimizely/js-sdk-logging';
import { LogHandler, ErrorHandler } from '../../modules/logging';
import { objectValues } from '../../utils/fns';
import { NotificationListener, ListenerPayload } from '../../shared_types';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import sinon from 'sinon';
import { assert } from 'chai';
import { forEach, cloneDeep } from 'lodash';
import { getLogger } from '@optimizely/js-sdk-logging';
import { sprintf } from '../../utils/fns';
import { getLogger } from '../../modules/logging';

import fns from '../../utils/fns';
import projectConfig from './';
Expand Down
2 changes: 1 addition & 1 deletion packages/optimizely-sdk/lib/core/project_config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
} from '../../utils/enums';
import configValidator from '../../utils/config_validator';

import { LogHandler } from '@optimizely/js-sdk-logging';
import { LogHandler } from '../../modules/logging';
import {
Audience,
Experiment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import sinon from 'sinon';
import { assert } from 'chai';
import { cloneDeep } from 'lodash';

import * as logging from '@optimizely/js-sdk-logging';
import { sprintf } from '../../utils/fns';
import * as logging from '../../modules/logging';
import * as datafileManager from '@optimizely/js-sdk-datafile-manager';

import * as projectConfig from './index';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { getLogger } from '@optimizely/js-sdk-logging';
import { getLogger } from '../../modules/logging';
import { sprintf } from '../../utils/fns';

import { ERROR_MESSAGES } from '../../utils/enums';
Expand Down
2 changes: 1 addition & 1 deletion packages/optimizely-sdk/lib/index.browser.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as logging from '@optimizely/js-sdk-logging';
import logging from './modules/logging/logger';

import { assert } from 'chai';
import sinon from 'sinon';
Expand Down
18 changes: 10 additions & 8 deletions packages/optimizely-sdk/lib/index.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import logHelper from './modules/logging/logger';
import {
getLogger,
setLogHandler,
setLogLevel,
setErrorHandler,
getErrorHandler,
LogLevel
} from '@optimizely/js-sdk-logging';
} from './modules/logging';
import { LocalStoragePendingEventsDispatcher } from '@optimizely/js-sdk-event-processor';
import configValidator from './utils/config_validator';
import defaultErrorHandler from './plugins/error_handler';
Expand All @@ -35,8 +34,8 @@ import { OptimizelyDecideOption, Client, Config } from './shared_types';
import { createHttpPollingDatafileManager } from './plugins/datafile_manager/http_polling_datafile_manager';

const logger = getLogger();
setLogHandler(loggerPlugin.createLogger());
setLogLevel(LogLevel.INFO);
logHelper.setLogHandler(loggerPlugin.createLogger());
logHelper.setLogLevel(LogLevel.INFO);

const MODULE_NAME = 'INDEX_BROWSER';
const DEFAULT_EVENT_BATCH_SIZE = 10;
Expand All @@ -60,12 +59,12 @@ const createInstance = function(config: Config): Client | null {
setErrorHandler(config.errorHandler);
}
if (config.logger) {
setLogHandler(config.logger);
logHelper.setLogHandler(config.logger);
// respect the logger's shouldLog functionality
setLogLevel(LogLevel.NOTSET);
logHelper.setLogLevel(LogLevel.NOTSET);
}
if (config.logLevel !== undefined) {
setLogLevel(config.logLevel);
logHelper.setLogLevel(config.logLevel);
}

try {
Expand Down Expand Up @@ -160,6 +159,9 @@ const __internalResetRetryState = function(): void {
/**
* Entry point into the Optimizely Browser SDK
*/

const setLogHandler = logHelper.setLogHandler
const setLogLevel = logHelper.setLogLevel
export {
loggerPlugin as logging,
defaultErrorHandler as errorHandler,
Expand Down
8 changes: 4 additions & 4 deletions packages/optimizely-sdk/lib/index.lite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/
import {
getLogger,
setLogHandler,
setLogLevel,
setErrorHandler,
getErrorHandler,
LogLevel
} from '@optimizely/js-sdk-logging';
LogLevel,
setLogHandler,
setLogLevel
} from './modules/logging';
import configValidator from './utils/config_validator';
import defaultErrorHandler from './plugins/error_handler';
import noOpEventDispatcher from './plugins/event_dispatcher/no_op';
Expand Down
8 changes: 4 additions & 4 deletions packages/optimizely-sdk/lib/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
***************************************************************************/
import {
getLogger,
setLogHandler,
setLogLevel,
setErrorHandler,
getErrorHandler,
LogLevel
} from '@optimizely/js-sdk-logging';
LogLevel,
setLogHandler,
setLogLevel
} from './modules/logging';
import Optimizely from './optimizely';
import * as enums from './utils/enums';
import * as loggerPlugin from './plugins/logger';
Expand Down
2 changes: 1 addition & 1 deletion packages/optimizely-sdk/lib/index.react_native.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
import { assert } from 'chai';
import sinon from 'sinon';
import * as logging from '@optimizely/js-sdk-logging';
import * as logging from './modules/logging/logger';
import * as eventProcessor from './plugins/event_processor';

import Optimizely from './optimizely';
Expand Down
8 changes: 4 additions & 4 deletions packages/optimizely-sdk/lib/index.react_native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/
import {
getLogger,
setLogHandler,
setLogLevel,
setErrorHandler,
getErrorHandler,
LogLevel
} from '@optimizely/js-sdk-logging';
LogLevel,
setLogHandler,
setLogLevel
} from './modules/logging';
import * as enums from './utils/enums';
import Optimizely from './optimizely';
import configValidator from './utils/config_validator';
Expand Down
67 changes: 67 additions & 0 deletions packages/optimizely-sdk/lib/modules/logging/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright 2019, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @export
* @interface ErrorHandler
*/
export interface ErrorHandler {
/**
* @param {Error} exception
* @memberof ErrorHandler
*/
handleError(exception: Error): void
}

/**
* @export
* @class NoopErrorHandler
* @implements {ErrorHandler}
*/
export class NoopErrorHandler implements ErrorHandler {
/**
* @param {Error} exception
* @memberof NoopErrorHandler
*/
handleError(exception: Error): void {
// no-op
return
}
}

let globalErrorHandler: ErrorHandler = new NoopErrorHandler()

/**
* @export
* @param {ErrorHandler} handler
*/
export function setErrorHandler(handler: ErrorHandler): void {
globalErrorHandler = handler
}

/**
* @export
* @returns {ErrorHandler}
*/
export function getErrorHandler(): ErrorHandler {
return globalErrorHandler
}

/**
* @export
*/
export function resetErrorHandler(): void {
globalErrorHandler = new NoopErrorHandler()
}
18 changes: 18 additions & 0 deletions packages/optimizely-sdk/lib/modules/logging/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright 2019, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './errorHandler'
export * from './models'
export * from './logger'

0 comments on commit fff21e0

Please sign in to comment.