mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Add new SentryTransport to winston (#3432)
Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
0258c3476d
commit
b9bb990690
@ -304,6 +304,7 @@
|
|||||||
"@types/tar": "^4.0.5",
|
"@types/tar": "^4.0.5",
|
||||||
"@types/tcp-port-used": "^1.0.0",
|
"@types/tcp-port-used": "^1.0.0",
|
||||||
"@types/tempy": "^0.3.0",
|
"@types/tempy": "^0.3.0",
|
||||||
|
"@types/triple-beam": "^1.3.2",
|
||||||
"@types/url-parse": "^1.4.3",
|
"@types/url-parse": "^1.4.3",
|
||||||
"@types/uuid": "^8.3.0",
|
"@types/uuid": "^8.3.0",
|
||||||
"@types/webdriverio": "^4.13.0",
|
"@types/webdriverio": "^4.13.0",
|
||||||
|
|||||||
@ -23,7 +23,7 @@ import { CaptureConsole, Dedupe, Offline } from "@sentry/integrations";
|
|||||||
import * as Sentry from "@sentry/electron";
|
import * as Sentry from "@sentry/electron";
|
||||||
import { sentryDsn, isProduction } from "./vars";
|
import { sentryDsn, isProduction } from "./vars";
|
||||||
import { UserStore } from "./user-store";
|
import { UserStore } from "./user-store";
|
||||||
import logger from "../main/logger";
|
import { inspect } from "util";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* "Translate" 'browser' to 'main' as Lens developer more familiar with the term 'main'
|
* "Translate" 'browser' to 'main' as Lens developer more familiar with the term 'main'
|
||||||
@ -51,10 +51,13 @@ export function SentryInit() {
|
|||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.info(`🔒 [SENTRY-BEFORE-SEND-HOOK]: allowErrorReporting: ${allowErrorReporting}. Sentry event is caught but not sent to server.`);
|
/**
|
||||||
logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === START OF SENTRY EVENT ===");
|
* Directly write to stdout so that no other integrations capture this and create an infinite loop
|
||||||
logger.info(event);
|
*/
|
||||||
logger.info("🔒 [SENTRY-BEFORE-SEND-HOOK]: === END OF SENTRY EVENT ===");
|
process.stdout.write(`🔒 [SENTRY-BEFORE-SEND-HOOK]: allowErrorReporting: ${allowErrorReporting}. Sentry event is caught but not sent to server.`);
|
||||||
|
process.stdout.write("🔒 [SENTRY-BEFORE-SEND-HOOK]: === START OF SENTRY EVENT ===");
|
||||||
|
process.stdout.write(inspect(event, false, null, true));
|
||||||
|
process.stdout.write("🔒 [SENTRY-BEFORE-SEND-HOOK]: === END OF SENTRY EVENT ===");
|
||||||
|
|
||||||
// if return null, the event won't be sent
|
// if return null, the event won't be sent
|
||||||
// ref https://github.com/getsentry/sentry-javascript/issues/2039
|
// ref https://github.com/getsentry/sentry-javascript/issues/2039
|
||||||
@ -64,12 +67,11 @@ export function SentryInit() {
|
|||||||
integrations: [
|
integrations: [
|
||||||
new CaptureConsole({ levels: ["error"] }),
|
new CaptureConsole({ levels: ["error"] }),
|
||||||
new Dedupe(),
|
new Dedupe(),
|
||||||
new Offline()
|
new Offline(),
|
||||||
],
|
],
|
||||||
initialScope: {
|
initialScope: {
|
||||||
tags: {
|
tags: {
|
||||||
|
"process": processName,
|
||||||
"process": processName
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
environment: isProduction ? "production" : "development",
|
environment: isProduction ? "production" : "development",
|
||||||
|
|||||||
@ -21,31 +21,93 @@
|
|||||||
|
|
||||||
import { app, remote } from "electron";
|
import { app, remote } from "electron";
|
||||||
import winston from "winston";
|
import winston from "winston";
|
||||||
|
import Transport from "winston-transport";
|
||||||
import { isDebugging, isTestEnv } from "../common/vars";
|
import { isDebugging, isTestEnv } from "../common/vars";
|
||||||
|
import { LEVEL } from "triple-beam";
|
||||||
|
import { Severity } from "@sentry/browser";
|
||||||
|
import * as Sentry from "@sentry/electron";
|
||||||
|
|
||||||
const logLevel = process.env.LOG_LEVEL ? process.env.LOG_LEVEL : isDebugging ? "debug" : "info";
|
const SENTRY_LEVELS_MAP = {
|
||||||
const consoleOptions: winston.transports.ConsoleTransportOptions = {
|
silly: Severity.Debug,
|
||||||
handleExceptions: false,
|
verbose: Severity.Debug,
|
||||||
level: logLevel,
|
debug: Severity.Debug,
|
||||||
|
info: Severity.Info,
|
||||||
|
warn: Severity.Warning,
|
||||||
|
error: Severity.Error,
|
||||||
};
|
};
|
||||||
const fileOptions: winston.transports.FileTransportOptions = {
|
const WINSTON_CMP: Record<WinstonLevel, Set<WinstonLevel>> = {
|
||||||
handleExceptions: false,
|
silly: new Set(["silly", "verbose", "debug", "info", "warn", "error"]),
|
||||||
level: logLevel,
|
verbose: new Set(["verbose", "debug", "info", "warn", "error"]),
|
||||||
filename: "lens.log",
|
debug: new Set(["debug", "info", "warn", "error"]),
|
||||||
dirname: (app ?? remote?.app)?.getPath("logs"),
|
info: new Set(["info", "warn", "error"]),
|
||||||
maxsize: 16 * 1024,
|
warn: new Set(["warn", "error"]),
|
||||||
maxFiles: 16,
|
error: new Set(["error"]),
|
||||||
tailable: true,
|
|
||||||
};
|
};
|
||||||
const logger = winston.createLogger({
|
|
||||||
|
type WinstonLevel = keyof typeof SENTRY_LEVELS_MAP;
|
||||||
|
|
||||||
|
class SentryTransport extends Transport {
|
||||||
|
logLevels: Set<WinstonLevel>;
|
||||||
|
|
||||||
|
constructor(minWinstonLevel: WinstonLevel) {
|
||||||
|
super();
|
||||||
|
|
||||||
|
this.logLevels = WINSTON_CMP[minWinstonLevel];
|
||||||
|
}
|
||||||
|
|
||||||
|
log(info: any, next: () => void) {
|
||||||
|
setImmediate(() => {
|
||||||
|
this.emit("logged", info);
|
||||||
|
});
|
||||||
|
|
||||||
|
const { message, level: _, tags, user, ...extra } = info;
|
||||||
|
const winstonLevel: WinstonLevel = info[LEVEL];
|
||||||
|
const level = SENTRY_LEVELS_MAP[winstonLevel];
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (this.logLevels.has(winstonLevel)) {
|
||||||
|
Sentry.captureMessage(message, {
|
||||||
|
level,
|
||||||
|
tags,
|
||||||
|
extra,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CreateLoggerOpts extends winston.LoggerOptions {
|
||||||
|
transports?: Transport[];
|
||||||
|
}
|
||||||
|
|
||||||
|
const logLevel = process.env.LOG_LEVEL || (isDebugging ? "debug" : "info");
|
||||||
|
|
||||||
|
const loggerOpts: CreateLoggerOpts = {
|
||||||
format: winston.format.combine(
|
format: winston.format.combine(
|
||||||
winston.format.colorize(),
|
winston.format.colorize(),
|
||||||
winston.format.simple(),
|
winston.format.simple(),
|
||||||
),
|
),
|
||||||
transports: [
|
transports: [
|
||||||
new winston.transports.Console(consoleOptions),
|
new SentryTransport("error"),
|
||||||
...(isTestEnv ? [] : [new winston.transports.File(fileOptions)]),
|
new winston.transports.Console({
|
||||||
|
handleExceptions: false,
|
||||||
|
level: logLevel,
|
||||||
|
}),
|
||||||
],
|
],
|
||||||
});
|
};
|
||||||
|
|
||||||
export default logger;
|
if (!isTestEnv) {
|
||||||
|
loggerOpts.transports.push(new winston.transports.File({
|
||||||
|
handleExceptions: false,
|
||||||
|
level: logLevel,
|
||||||
|
filename: "lens.log",
|
||||||
|
dirname: (app ?? remote?.app)?.getPath("logs"),
|
||||||
|
maxsize: 16 * 1024,
|
||||||
|
maxFiles: 16,
|
||||||
|
tailable: true,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
export default winston.createLogger(loggerOpts);
|
||||||
|
|||||||
@ -2010,6 +2010,11 @@
|
|||||||
resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-4.0.0.tgz#fef1904e4668b6e5ecee60c52cc6a078ffa6697d"
|
resolved "https://registry.yarnpkg.com/@types/tough-cookie/-/tough-cookie-4.0.0.tgz#fef1904e4668b6e5ecee60c52cc6a078ffa6697d"
|
||||||
integrity sha512-I99sngh224D0M7XgW1s120zxCt3VYQ3IQsuw3P3jbq5GG4yc79+ZjyKznyOGIQrflfylLgcfekeZW/vk0yng6A==
|
integrity sha512-I99sngh224D0M7XgW1s120zxCt3VYQ3IQsuw3P3jbq5GG4yc79+ZjyKznyOGIQrflfylLgcfekeZW/vk0yng6A==
|
||||||
|
|
||||||
|
"@types/triple-beam@^1.3.2":
|
||||||
|
version "1.3.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/triple-beam/-/triple-beam-1.3.2.tgz#38ecb64f01aa0d02b7c8f4222d7c38af6316fef8"
|
||||||
|
integrity sha512-txGIh+0eDFzKGC25zORnswy+br1Ha7hj5cMVwKIU7+s0U2AxxJru/jZSMU6OC9MJWP6+pc/hc6ZjyZShpsyY2g==
|
||||||
|
|
||||||
"@types/trusted-types@*":
|
"@types/trusted-types@*":
|
||||||
version "1.0.4"
|
version "1.0.4"
|
||||||
resolved "https://registry.yarnpkg.com/@types/trusted-types/-/trusted-types-1.0.4.tgz#922d092c84a776a59acb0bd6785fd82b59b9bad5"
|
resolved "https://registry.yarnpkg.com/@types/trusted-types/-/trusted-types-1.0.4.tgz#922d092c84a776a59acb0bd6785fd82b59b9bad5"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user