Skip to content

Commit

Permalink
fix: Can only enable one debugger at a time, which isn't useful.
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-coster committed Oct 6, 2021
1 parent 83303c0 commit b91a187
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
12 changes: 9 additions & 3 deletions src/lib/BravoClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
stringsMatch,
generateRandomString,
stringToNumber,
wrapIfNotArray,
} from './utility.js';
import { BravoCollection } from './entities/BravoCollection';
import { BravoUser } from '$/lib/entities/BravoUser';
Expand All @@ -20,6 +21,7 @@ import { BravoWidget } from '$entities/BravoWidget.js';
import { BravoColumn } from './entities/BravoColumn.js';
import type {
ArrayMatchFunction,
OneOrMore,
PartialBy,
RequiredBy,
} from '$/types/Utility.js';
Expand All @@ -33,7 +35,7 @@ import type { BravoEntity } from './BravoEntity.js';
import type { FavroApi } from '$types/FavroApi.js';
import { BravoWebhookDefinition } from './entities/BravoWebhook.js';
import { BravoGroup } from '$/types/Bravo.js';
import { DebugPath, Logger } from './Logger.js';
import { DebugPath, DebugPathSetting, Logger } from './Logger.js';

export { FavroClientAuth as BravoClientAuth } from './clientLib/FavroClient.js';

Expand Down Expand Up @@ -81,8 +83,12 @@ export class BravoClient extends FavroClient {
return new BravoResponseEntities(this, entityClass, res);
}

enableDebugLogging(debugNamespace: DebugPath) {
Logger.enableDebug(debugNamespace);
/**
* Override any previous debugLogging settings
* with a new set.
*/
enableDebugLogging(debugNamespaces: OneOrMore<DebugPathSetting>) {
Logger.enableDebug(wrapIfNotArray(debugNamespaces));
}

disableDebugLogging() {
Expand Down
21 changes: 8 additions & 13 deletions src/lib/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { DebugPaths } from '$/types/ObjectPaths.js';
import type { AnyFunction } from '$/types/Utility.js';
import debug from 'debug';
import { sortPaths } from './utility.js';

const requiredLogLevels = ['log', 'error'] as const;
const optionalLogLevels = ['info', 'trace', 'warn', 'dir'] as const;
Expand All @@ -17,6 +16,7 @@ export type LoggerUtility = {
};

export type DebugPath = DebugPaths<typeof Logger['debugHeirarchy']>;
export type DebugPathSetting = DebugPath | `-${DebugPath}`;

export class Logger {
private static _debuggers: { [namePath: string]: debug.Debugger } = {};
Expand All @@ -37,8 +37,8 @@ export class Logger {
} as const;
}

static enableDebug(debugNamespace: DebugPath) {
debug.enable(debugNamespace);
static enableDebug(debugNamespaces: DebugPathSetting[]) {
debug.enable(debugNamespaces.join(','));
}

static disableDebug() {
Expand Down Expand Up @@ -71,19 +71,14 @@ export class Logger {
Logger._utility.error.bind(Logger._utility)(...args);
}

static getDebugLogger(path: string, parentPath?: string) {
const fullPath = parentPath ? `${parentPath}:${path}` : path;
this._debuggers[fullPath] ||= debug(fullPath);
this._debuggers[fullPath].log = Logger.log;
this._debuggerPaths.add(fullPath);
return this._debuggers[fullPath];
static getDebugLogger(path: DebugPath) {
this._debuggers[path] ||= debug(path);
this._debuggers[path].log = Logger.log;
this._debuggerPaths.add(path);
return this._debuggers[path];
}

static set loggingUtility(utility: LoggerUtility) {
Logger._utility = utility;
}

static get debugPaths() {
return sortPaths([...this._debuggerPaths], ':');
}
}
2 changes: 1 addition & 1 deletion src/types/Utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export type ParametersExcludingIndices<
export type ParametersExcludingFirst<AnyFunc extends AnyFunction> =
ParametersExcludingIndices<AnyFunc, 0>;

export type ValueOrValueArray<T> = T[] | T;
export type OneOrMore<T> = T[] | T;

export type ExtractKeysByValue<Container, ValueTypeFilter> = {
[Key in keyof Container]-?: Container[Key] extends Function
Expand Down

0 comments on commit b91a187

Please sign in to comment.