Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hub-common): add arcgisHubConfig global variable #1820

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/common/src/ArcGISContextManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ export class ArcGISContextManager {

private _currentUser: IUser;

private _logLevel: Level = Level.error;

private _serviceStatus: HubServiceStatus;

private _featureFlags: IFeatureFlags = {};
Expand All @@ -104,10 +102,12 @@ export class ArcGISContextManager {
private constructor(opts: IArcGISContextManagerOptions) {
// Having a unique id makes debugging easier
this.id = new Date().getTime();

// TODO: remove all log level logic at next breaking change
if (opts.logLevel) {
this._logLevel = opts.logLevel;
Logger.setLogLevel(opts.logLevel);
}
Logger.setLogLevel(this._logLevel);

Logger.debug(`ArcGISContextManager:ctor: Creating ${this.id}`);

if (opts.properties) {
Expand Down
3 changes: 1 addition & 2 deletions packages/common/src/types/IArcGISContextManagerOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ export interface IArcGISContextManagerOptions {
properties?: Record<string, any>;

/**
* DEPRECATED: use `globalThis.arcgisHubConfig.logLevel` instead
* Logging level
* off > error > warn > info > debug > all
* defaults to 'error'
*/
logLevel?: Level;

Expand Down
25 changes: 25 additions & 0 deletions packages/common/src/utils/IHubConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { LogLevel } from "./LogLevel";

/**
* Hub.js configuration settings
*
* You can override the default settings by
* initializing the global `arcgisHubConfig` variable
* before using any Hub.js functions, for example:
* ```js
* import { Logger } from '@esri/hub-common';
* // configure Hub.js
* const logLevel = environment === 'production' ? 'error' : 'debug';
* globalThis.arcgisHubConfig = { logLevel };
* // then use Hub.js
* Logger.info('This message will not log on production');
* ```
*/
export interface IHubConfig {
/**
* The global log level for Hub.js
*
* Available logging levels are specified in the LogLevel type. The default is 'info'.
*/
logLevel?: LogLevel;
}
2 changes: 2 additions & 0 deletions packages/common/src/utils/LogLevel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** The level of console logs that will be issued by Hub.js */
export type LogLevel = "all" | "debug" | "info" | "warn" | "error" | "off";
1 change: 1 addition & 0 deletions packages/common/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./LogLevel";
export * from "./asyncForEach";
export * from "./batch";
export * from "./encoding";
Expand Down
43 changes: 43 additions & 0 deletions packages/common/src/utils/internal/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { IHubConfig } from "../IHubConfig";

// the default settings
const DEFAULT_CONFIG: IHubConfig = {
logLevel: "info",
};

// the global variable name that consumers use to override the defaults
const GLOBAL_VARIABLE_NAME = "arcgisHubConfig";

// NOTE: we want to move to a config that is read-only
// once it has been initialized from the above global variable
// see https://devtopia.esri.com/dc/hub/issues/10713#issuecomment-5337749
// but for now we have to support the deprecated Logger.setLogLevel()
// so we need these temporary functions to get/set config settings

export const _getConfigSetting = (key: keyof IHubConfig) => {
const config = {
...DEFAULT_CONFIG,
...(globalThis as any)[GLOBAL_VARIABLE_NAME],
} as IHubConfig;
return config && config[key];
};

export const _setConfigSetting = (key: keyof IHubConfig, value: any) => {
const config = (globalThis as any)[GLOBAL_VARIABLE_NAME] as IHubConfig;
if (!config) {
(globalThis as any)[GLOBAL_VARIABLE_NAME] = {};
}
(globalThis as any)[GLOBAL_VARIABLE_NAME][key] = value;
};

// TODO: once we remove Logger.setLogLevel() we should
// replace the above functions with the following:

// merge consumer supplied config (if any) with the defaults
// const config = {
// ...DEFAULT_CONFIG,
// ...(globalThis as any)[GLOBAL_VARIABLE_NAME]
// } as IHubConfig;

// export the settings as immutable consts
// export const logLevel = config.logLevel;
24 changes: 13 additions & 11 deletions packages/common/src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* tslint:disable no-console */
import { _setConfigSetting, _getConfigSetting } from "./internal/config";

// TODO: stop exporting this at the next breaking change
// only exporting for backward compatibility
/**
* Enum for Logger Levels
*/
Expand All @@ -9,20 +12,20 @@ export enum Level {
info,
warn,
error,
off
off,
}

/**
* ```js
* import { Logger, Level } from '@esri/hub-common'
* import { Logger } from '@esri/hub-common'
* ```
* Functions share the console interface
* ```js
* Logger.log('My Message');
* Logger.warn('Watch out!', { threat: 'Charizard' });
* // etc, etc
* ```
* Available logging levels are specified in the Level enum. The hierarchy is as follows:
* Available logging levels are specified in the LogLevel type. The hierarchy is as follows:
* ```
* off > error > warn > info > debug > all
* ```
Expand All @@ -32,20 +35,17 @@ export enum Level {
* Logger.info('This message won't log');
* Logger.error('But this one will!');
* ```
* Logger's default level is 'off', so set desired level before use
* ```js
* Logger.setLogLevel(Level.all);
* ```
*/
export class Logger {
private static logLevel = Level.off;

// TODO: remove this at next breaking change
/**
* DEPRECATED: configure log level with `globalThis.arcgisHubConfig.logLevel` instead
* Sets the global log level
* @param {Level} level
*/
public static setLogLevel(level: Level) {
this.logLevel = level;
const levelName = Level[level];
_setConfigSetting("logLevel", levelName);
}

/**
Expand Down Expand Up @@ -104,6 +104,8 @@ export class Logger {
}

private static isLevelPermitted(level: Level) {
return this.logLevel <= level;
const configuredLevel =
Level[_getConfigSetting("logLevel") as keyof typeof Level];
return configuredLevel <= level;
}
}
Loading