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

Refactor logger #2

Merged
merged 3 commits into from
May 21, 2024
Merged
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
**/node_modules
**/dist
**/build
.DS_Store
.DS_Store
**coverage
86 changes: 48 additions & 38 deletions packages/dmf-logging/package.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,50 @@
{
"name": "@dream.mf/logging",
"version": "1.2.3",
"main": "dist/index.js",
"module": "dist/index.mjs",
"typings": "dist/index.d.ts",
"license": "MIT",
"engines": {
"node": ">=16",
"pnpm": ">=9",
"npm": ">=8"
},
"scripts": {
"build": "rollup -c && npm run copy",
"copy": "cp package.json dist/ && cp readme.md dist/ && cp LICENSE dist/",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"react": "18.1.0",
"react-dom": "18.1.0",
"@dream.mf/core": "1.2.2"
},
"devDependencies": {
"rollup": "^2.78.0",
"@rollup/plugin-json": "6.1.0",
"rollup-plugin-dts": "^4.2.2",
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-sass": "^1.2.13",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.32.1",
"rollup-plugin-esbuild": "^6.1.1",
"@rollup/plugin-commonjs": "^22.0.2",
"@rollup/plugin-node-resolve": "^13.3.0",
"@rollup/plugin-typescript": "^8.3.4",
"typescript": "^4.7.4",
"tslib": "2.6.2",
"ts-jest": "^28.0.7",
"ts-loader": "^9.3.1"
}
"name": "@dream.mf/logging",
"version": "1.3.0",
"main": "dist/index.js",
"module": "dist/index.mjs",
"typings": "dist/index.d.ts",
"license": "MIT",
"engines": {
"node": ">=16",
"pnpm": ">=9",
"npm": ">=8"
},
"scripts": {
"build": "rollup -c && npm run copy && npm run clean-dev-deps",
"copy": "cp package.json dist/ && cp readme.md dist/ && cp LICENSE dist/",
"test": "vitest",
"clean-dev-deps": "json -f ./dist/package.json -I -e \"delete this.devDependencies;\""
},
"dependencies": {
"@dream.mf/core": "1.2.2"
},
"peerDependencies": {
"react": "^18.1.0",
"react-dom": "^18.1.0"
},
"devDependencies": {
"json": "11.0.0",
"@rollup/plugin-commonjs": "^22.0.2",
"@rollup/plugin-json": "6.1.0",
"@rollup/plugin-node-resolve": "^13.3.0",
"@rollup/plugin-typescript": "^8.3.4",
"@testing-library/jest-dom": "6.4.5",
"@vitest/coverage-v8": "^1.6.0",
"@vitejs/plugin-react": "4.2.1",
"jsdom": "^24.0.0",
"rollup": "^2.78.0",
"rollup-plugin-dts": "^4.2.2",
"rollup-plugin-esbuild": "^6.1.1",
"rollup-plugin-peer-deps-external": "^2.2.4",
"rollup-plugin-sass": "^1.2.13",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.32.1",
"ts-jest": "^28.0.7",
"ts-loader": "^9.3.1",
"tslib": "2.6.2",
"typescript": "^4.7.4",
"vite": "5.2.11",
"vitest": "1.6.0"
}
}
136 changes: 48 additions & 88 deletions packages/dmf-logging/src/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,120 +2,80 @@ import { useEffect } from "react";
import { LogConfig, LogType, debugPrefix } from "./types";
import core from "./core";

/** Log listener configuration settings and function callbacks */
export const logConfig = {
/** If set to true, all logs will also be piped to console.log */
debug: false,
logGeneral: undefined,
logAuthentication: undefined,
logException: undefined,
logPageView: undefined,
logFetch: undefined,
logFederation: undefined,
logEvent: undefined,
};

/** Interface for the log listener config */
export interface LogListenerProps {
config: LogConfig;
config: LogConfig;
}

/** Empty function to swallow logs, can be used later to log to console generically */
const _noop = () => null;

/** Handle the event and append any extra config based logic */
const _handleEvent = (config, event, type, func) => {
if (config.debug) {
console.log(debugPrefix, "[DEBUG]", type, event.detail);
}
func(event);
if (config.debug) {
console.log(debugPrefix, "[DEBUG]", type, event.detail);
}
func(event);
};

/** Log Listener, usually found int he host, which requires config and debug flags.
* This listener allows you to subscribe to global log events and apply your own log aggregator.
* @param config: The log configuration flags and callback functions.
*/
export const DreamMFLogListener = ({ config }: LogListenerProps) => {
/** On mount register and unregister event listeners if the config function is */
useEffect(() => {
core.setupRuntime(config);
RegisterListeners(config);
return () => {
DeregisterListeners(config);
};
}, []);
/** On mount register and unregister event listeners if the config function is */
useEffect(() => {
core.setupRuntime(config);
RegisterListeners(config);
return () => {
DeregisterListeners(config);
};
}, [config]);

/** Return nothing, in case we are using this in JSX */
return null;
/** Return nothing, in case we are using this in JSX */
return null;
};

/** Registers event listeners and appens a _noop for events not listened for */
export const RegisterListeners = (config: LogConfig) => {
config.logGeneral &&
window.addEventListener(LogType.General, (event) => {
_handleEvent(config, event, LogType.General, config.logGeneral);
});
config.logAuthentication &&
window.addEventListener(LogType.Authentication, (event) => {
_handleEvent(
config,
event,
LogType.Authentication,
config.logAuthentication,
);
});
config.logException &&
window.addEventListener(LogType.Exception, (event) => {
_handleEvent(config, event, LogType.Exception, config.logException);
});
config.logPageView &&
window.addEventListener(LogType.PageView, (event) => {
_handleEvent(config, event, LogType.PageView, config.logPageView);
});
config.logFederation &&
window.addEventListener(LogType.Federation, (event) => {
_handleEvent(config, event, LogType.Federation, config.logFederation);
});
config.logFetch &&
window.addEventListener(LogType.Fetch, (event) => {
_handleEvent(config, event, LogType.Fetch, config.logFetch);
});
config.logEvent &&
window.addEventListener(LogType.Event, (event) => {
_handleEvent(config, event, LogType.Event, config.logEvent);
});
config.logInfo &&
window.addEventListener(LogType.Info, (event) => {
_handleEvent(config, event, LogType.Info, config.logInfo);
});
config.logException &&
window.addEventListener(LogType.Exception, (event) => {
_handleEvent(config, event, LogType.Exception, config.logException);
});
config.logPageView &&
window.addEventListener(LogType.PageView, (event) => {
_handleEvent(config, event, LogType.PageView, config.logPageView);
});
config.logEvent &&
window.addEventListener(LogType.Event, (event) => {
_handleEvent(config, event, LogType.Event, config.logEvent);
});
};

/** Removes event listeners and appends the _noop */
export const DeregisterListeners = (config: LogConfig) => {
config.logEvent &&
window.removeEventListener(LogType.General, config.logEvent || _noop());
config.logAuthentication &&
window.removeEventListener(
LogType.Authentication,
config.logAuthentication || _noop(),
);
config.logException &&
window.removeEventListener(
LogType.Exception,
config.logException || _noop(),
);
config.logPageView &&
window.removeEventListener(LogType.PageView, config.logPageView || _noop());
config.logFetch &&
window.removeEventListener(LogType.Fetch, config.logFetch || _noop());
config.logFederation &&
window.removeEventListener(
LogType.Federation,
config.logFederation || _noop(),
);
config.logEvent &&
window.removeEventListener(LogType.Event, config.logEvent || _noop());
config.logEvent &&
window.removeEventListener(LogType.Event, config.logEvent || _noop());
config.logException &&
window.removeEventListener(
LogType.Exception,
config.logException || _noop()
);
config.logPageView &&
window.removeEventListener(
LogType.PageView,
config.logPageView || _noop()
);
config.logInfo &&
window.removeEventListener(LogType.Info, config.logInfo || _noop());
};

export default {
logConfig,
DreamMFLogListener,
RegisterListeners,
DeregisterListeners,
DreamMFLogListener,
RegisterListeners,
DeregisterListeners,
};
5 changes: 5 additions & 0 deletions packages/dmf-logging/src/logger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe("it runs", () => {
it("should run", () => {
expect(true).toBe(true);
});
});
Loading
Loading