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

Add winston formatting support for error causes #6576

Merged
merged 4 commits into from
Nov 17, 2022
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@
"uuid": "^8.3.2",
"win-ca": "^3.5.0",
"winston": "^3.8.2",
"winston-console-format": "^1.0.8",
"winston-transport-browserconsole": "^1.0.5",
"ws": "^8.11.0",
"xterm-link-provider": "^1.3.1"
Expand Down Expand Up @@ -366,6 +365,7 @@
"@typescript-eslint/parser": "^5.42.1",
"adr": "^1.4.3",
"ansi_up": "^5.1.0",
"chalk": "^4.1.2",
"chart.js": "^2.9.4",
"circular-dependency-plugin": "^5.2.2",
"cli-progress": "^3.11.2",
Expand Down
13 changes: 13 additions & 0 deletions src/common/app-paths/directory-for-logs.injectable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import appPathsInjectable from "./app-paths.injectable";

const directoryForLogsInjectable = getInjectable({
id: "directory-for-logs",
instantiate: (di) => di.inject(appPathsInjectable).logs,
});

export default directoryForLogsInjectable;
131 changes: 0 additions & 131 deletions src/common/ipc/__tests__/type-enforced-ipc.test.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/common/ipc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@

export * from "./ipc";
export * from "./invalid-kubeconfig";
export * from "./type-enforced-ipc";
107 changes: 0 additions & 107 deletions src/common/ipc/type-enforced-ipc.ts

This file was deleted.

11 changes: 9 additions & 2 deletions src/common/logger.injectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { createLogger, format } from "winston";
import type { Logger } from "./logger";
import logger from "./logger";
import { loggerTransportInjectionToken } from "./logger/transports";

const loggerInjectable = getInjectable({
id: "logger",
instantiate: (): Logger => logger,
instantiate: (di): Logger => createLogger({
format: format.combine(
format.splat(),
format.simple(),
),
transports: di.injectMany(loggerTransportInjectionToken),
}),
});

export default loggerInjectable;
73 changes: 6 additions & 67 deletions src/common/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
* Licensed under MIT License. See LICENSE in root directory for more information.
*/

import { app, ipcMain } from "electron";
import winston, { format } from "winston";
import type Transport from "winston-transport";
import { consoleFormat } from "winston-console-format";
import { isDebugging, isTestEnv } from "./vars";
import BrowserConsole from "winston-transport-browserconsole";
import { asLegacyGlobalForExtensionApi } from "../extensions/as-legacy-globals-for-extension-api/as-legacy-global-object-for-extension-api";
import loggerInjectable from "./logger.injectable";

export interface Logger {
info: (message: string, ...args: any) => void;
Expand All @@ -18,64 +14,7 @@ export interface Logger {
silly: (message: string, ...args: any) => void;
}

const logLevel = process.env.LOG_LEVEL
? process.env.LOG_LEVEL
: isDebugging
? "debug"
: isTestEnv
? "error"
: "info";

const transports: Transport[] = [];

if (ipcMain) {
transports.push(
new winston.transports.Console({
handleExceptions: false,
level: logLevel,
format: format.combine(
format.colorize({ level: true, message: false }),
format.padLevels(),
format.ms(),
consoleFormat({
showMeta: true,
inspectOptions: {
depth: 4,
colors: true,
maxArrayLength: 10,
breakLength: 120,
compact: Infinity,
},
}),
),
}),
);

if (!isTestEnv) {
transports.push(
new winston.transports.File({
handleExceptions: false,
level: "debug",
filename: "lens.log",
/**
* SAFTEY: the `ipcMain` check above should mean that this is only
* called in the main process
*/
dirname: app.getPath("logs"),
maxsize: 1024 * 1024,
maxFiles: 16,
tailable: true,
}),
);
}
} else {
transports.push(new BrowserConsole());
}

export default winston.createLogger({
format: format.combine(
format.splat(),
format.simple(),
),
transports,
}) as Logger;
/**
* @deprecated use `di.inject(loggerInjectable)` instead
*/
export default asLegacyGlobalForExtensionApi(loggerInjectable);
11 changes: 11 additions & 0 deletions src/common/logger/transports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/

import { getInjectionToken } from "@ogre-tools/injectable";
import type TransportStream from "winston-transport";

export const loggerTransportInjectionToken = getInjectionToken<TransportStream>({
id: "logger-transport",
});
Loading