1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Add proxy

Signed-off-by: Hung-Han (Henry) Chen <chenhungh@gmail.com>
This commit is contained in:
Hung-Han (Henry) Chen 2021-07-08 22:36:16 +03:00
parent 05b1a2fc5d
commit 21347ff89c
No known key found for this signature in database
GPG Key ID: 54B44603D251B788

View File

@ -48,4 +48,44 @@ const logger = winston.createLogger({
],
});
export default logger;
/**
* Type guard to ensure unknown is logger.error function
*/
const isLoggerError = (unknown: unknown, key: keyof typeof logger): unknown is (typeof logger)["error"] =>
typeof unknown === "function" && key === "error";
const captureErrorMessage = (message: string) => {
let Sentry;
// if in main process
if (process.type === "browser") {
Sentry = require("@sentry/electron/dist/main");
}
// if in renderer process
if (process.type === "renderer") {
Sentry = require("@sentry/electron/dist/renderer");
}
if (Sentry && typeof Sentry.captureException === "function" && message) {
console.info("capturing.....");
Sentry.captureException(message);
}
};
const proxiedLogger = new Proxy(logger, {
get(target: typeof logger, key: keyof typeof logger) {
const property = target[key];
if (isLoggerError(property, key)) {
return (message: string, callback: winston.LogCallback) => {
captureErrorMessage(message);
property(message, callback);
};
}
return property;
}
});
export default proxiedLogger;