mirror of
https://github.com/bitwarden/browser
synced 2025-12-11 13:53:34 +00:00
Platform logging lib (#15338)
* Add Platform Logging Lib * Move console log spec and test util back into libs/common * Fix ConsoleLogServer re-export * Fix types error
This commit is contained in:
1
.github/CODEOWNERS
vendored
1
.github/CODEOWNERS
vendored
@@ -91,6 +91,7 @@ libs/common/spec @bitwarden/team-platform-dev
|
|||||||
libs/common/src/state-migrations @bitwarden/team-platform-dev
|
libs/common/src/state-migrations @bitwarden/team-platform-dev
|
||||||
libs/platform @bitwarden/team-platform-dev
|
libs/platform @bitwarden/team-platform-dev
|
||||||
libs/storage-core @bitwarden/team-platform-dev
|
libs/storage-core @bitwarden/team-platform-dev
|
||||||
|
libs/logging @bitwarden/team-platform-dev
|
||||||
libs/storage-test-utils @bitwarden/team-platform-dev
|
libs/storage-test-utils @bitwarden/team-platform-dev
|
||||||
# Web utils used across app and connectors
|
# Web utils used across app and connectors
|
||||||
apps/web/src/utils/ @bitwarden/team-platform-dev
|
apps/web/src/utils/ @bitwarden/team-platform-dev
|
||||||
|
|||||||
@@ -1,9 +1 @@
|
|||||||
import { LogLevelType } from "../enums/log-level-type.enum";
|
export { LogService } from "@bitwarden/logging";
|
||||||
|
|
||||||
export abstract class LogService {
|
|
||||||
abstract debug(message?: any, ...optionalParams: any[]): void;
|
|
||||||
abstract info(message?: any, ...optionalParams: any[]): void;
|
|
||||||
abstract warning(message?: any, ...optionalParams: any[]): void;
|
|
||||||
abstract error(message?: any, ...optionalParams: any[]): void;
|
|
||||||
abstract write(level: LogLevelType, message?: any, ...optionalParams: any[]): void;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,8 +1 @@
|
|||||||
// FIXME: update to use a const object instead of a typescript enum
|
export { LogLevel as LogLevelType } from "@bitwarden/logging";
|
||||||
// eslint-disable-next-line @bitwarden/platform/no-enums
|
|
||||||
export enum LogLevelType {
|
|
||||||
Debug,
|
|
||||||
Info,
|
|
||||||
Warning,
|
|
||||||
Error,
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { interceptConsole, restoreConsole } from "../../../spec";
|
import { ConsoleLogService } from "@bitwarden/logging";
|
||||||
|
|
||||||
import { ConsoleLogService } from "./console-log.service";
|
import { interceptConsole, restoreConsole } from "../../../spec";
|
||||||
|
|
||||||
describe("ConsoleLogService", () => {
|
describe("ConsoleLogService", () => {
|
||||||
const error = new Error("this is an error");
|
const error = new Error("this is an error");
|
||||||
|
|||||||
@@ -1,59 +1 @@
|
|||||||
// FIXME: Update this file to be type safe and remove this and next line
|
export { ConsoleLogService } from "@bitwarden/logging";
|
||||||
// @ts-strict-ignore
|
|
||||||
import { LogService as LogServiceAbstraction } from "../abstractions/log.service";
|
|
||||||
import { LogLevelType } from "../enums/log-level-type.enum";
|
|
||||||
|
|
||||||
export class ConsoleLogService implements LogServiceAbstraction {
|
|
||||||
protected timersMap: Map<string, [number, number]> = new Map();
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
protected isDev: boolean,
|
|
||||||
protected filter: (level: LogLevelType) => boolean = null,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
debug(message?: any, ...optionalParams: any[]) {
|
|
||||||
if (!this.isDev) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.write(LogLevelType.Debug, message, ...optionalParams);
|
|
||||||
}
|
|
||||||
|
|
||||||
info(message?: any, ...optionalParams: any[]) {
|
|
||||||
this.write(LogLevelType.Info, message, ...optionalParams);
|
|
||||||
}
|
|
||||||
|
|
||||||
warning(message?: any, ...optionalParams: any[]) {
|
|
||||||
this.write(LogLevelType.Warning, message, ...optionalParams);
|
|
||||||
}
|
|
||||||
|
|
||||||
error(message?: any, ...optionalParams: any[]) {
|
|
||||||
this.write(LogLevelType.Error, message, ...optionalParams);
|
|
||||||
}
|
|
||||||
|
|
||||||
write(level: LogLevelType, message?: any, ...optionalParams: any[]) {
|
|
||||||
if (this.filter != null && this.filter(level)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (level) {
|
|
||||||
case LogLevelType.Debug:
|
|
||||||
// eslint-disable-next-line
|
|
||||||
console.log(message, ...optionalParams);
|
|
||||||
break;
|
|
||||||
case LogLevelType.Info:
|
|
||||||
// eslint-disable-next-line
|
|
||||||
console.log(message, ...optionalParams);
|
|
||||||
break;
|
|
||||||
case LogLevelType.Warning:
|
|
||||||
// eslint-disable-next-line
|
|
||||||
console.warn(message, ...optionalParams);
|
|
||||||
break;
|
|
||||||
case LogLevelType.Error:
|
|
||||||
// eslint-disable-next-line
|
|
||||||
console.error(message, ...optionalParams);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
5
libs/logging/README.md
Normal file
5
libs/logging/README.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# logging
|
||||||
|
|
||||||
|
Owned by: platform
|
||||||
|
|
||||||
|
Logging primitives
|
||||||
3
libs/logging/eslint.config.mjs
Normal file
3
libs/logging/eslint.config.mjs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
import baseConfig from "../../eslint.config.mjs";
|
||||||
|
|
||||||
|
export default [...baseConfig];
|
||||||
10
libs/logging/jest.config.js
Normal file
10
libs/logging/jest.config.js
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
module.exports = {
|
||||||
|
displayName: "logging",
|
||||||
|
preset: "../../jest.preset.js",
|
||||||
|
testEnvironment: "node",
|
||||||
|
transform: {
|
||||||
|
"^.+\\.[tj]s$": ["ts-jest", { tsconfig: "<rootDir>/tsconfig.spec.json" }],
|
||||||
|
},
|
||||||
|
moduleFileExtensions: ["ts", "js", "html"],
|
||||||
|
coverageDirectory: "../../coverage/libs/logging",
|
||||||
|
};
|
||||||
11
libs/logging/package.json
Normal file
11
libs/logging/package.json
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"name": "@bitwarden/logging",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"description": "Logging primitives",
|
||||||
|
"private": true,
|
||||||
|
"type": "commonjs",
|
||||||
|
"main": "dist/index.js",
|
||||||
|
"types": "dist/index.d.ts",
|
||||||
|
"license": "GPL-3.0",
|
||||||
|
"author": "platform"
|
||||||
|
}
|
||||||
33
libs/logging/project.json
Normal file
33
libs/logging/project.json
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
{
|
||||||
|
"name": "logging",
|
||||||
|
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
||||||
|
"sourceRoot": "libs/logging/src",
|
||||||
|
"projectType": "library",
|
||||||
|
"tags": [],
|
||||||
|
"targets": {
|
||||||
|
"build": {
|
||||||
|
"executor": "@nx/js:tsc",
|
||||||
|
"outputs": ["{options.outputPath}"],
|
||||||
|
"options": {
|
||||||
|
"outputPath": "dist/libs/logging",
|
||||||
|
"main": "libs/logging/src/index.ts",
|
||||||
|
"tsConfig": "libs/logging/tsconfig.lib.json",
|
||||||
|
"assets": ["libs/logging/*.md"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"lint": {
|
||||||
|
"executor": "@nx/eslint:lint",
|
||||||
|
"outputs": ["{options.outputFile}"],
|
||||||
|
"options": {
|
||||||
|
"lintFilePatterns": ["libs/logging/**/*.ts"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"test": {
|
||||||
|
"executor": "@nx/jest:jest",
|
||||||
|
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
|
||||||
|
"options": {
|
||||||
|
"jestConfig": "libs/logging/jest.config.js"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
57
libs/logging/src/console-log.service.ts
Normal file
57
libs/logging/src/console-log.service.ts
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
import { LogLevel } from "./log-level";
|
||||||
|
import { LogService } from "./log.service";
|
||||||
|
|
||||||
|
export class ConsoleLogService implements LogService {
|
||||||
|
protected timersMap: Map<string, [number, number]> = new Map();
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
protected isDev: boolean,
|
||||||
|
protected filter: ((level: LogLevel) => boolean) | null = null,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
debug(message?: any, ...optionalParams: any[]) {
|
||||||
|
if (!this.isDev) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.write(LogLevel.Debug, message, ...optionalParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
info(message?: any, ...optionalParams: any[]) {
|
||||||
|
this.write(LogLevel.Info, message, ...optionalParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
warning(message?: any, ...optionalParams: any[]) {
|
||||||
|
this.write(LogLevel.Warning, message, ...optionalParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
error(message?: any, ...optionalParams: any[]) {
|
||||||
|
this.write(LogLevel.Error, message, ...optionalParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
write(level: LogLevel, message?: any, ...optionalParams: any[]) {
|
||||||
|
if (this.filter != null && this.filter(level)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (level) {
|
||||||
|
case LogLevel.Debug:
|
||||||
|
// eslint-disable-next-line
|
||||||
|
console.log(message, ...optionalParams);
|
||||||
|
break;
|
||||||
|
case LogLevel.Info:
|
||||||
|
// eslint-disable-next-line
|
||||||
|
console.log(message, ...optionalParams);
|
||||||
|
break;
|
||||||
|
case LogLevel.Warning:
|
||||||
|
// eslint-disable-next-line
|
||||||
|
console.warn(message, ...optionalParams);
|
||||||
|
break;
|
||||||
|
case LogLevel.Error:
|
||||||
|
// eslint-disable-next-line
|
||||||
|
console.error(message, ...optionalParams);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
3
libs/logging/src/index.ts
Normal file
3
libs/logging/src/index.ts
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
export { LogService } from "./log.service";
|
||||||
|
export { LogLevel } from "./log-level";
|
||||||
|
export { ConsoleLogService } from "./console-log.service";
|
||||||
8
libs/logging/src/log-level.ts
Normal file
8
libs/logging/src/log-level.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
// FIXME: update to use a const object instead of a typescript enum
|
||||||
|
// eslint-disable-next-line @bitwarden/platform/no-enums
|
||||||
|
export enum LogLevel {
|
||||||
|
Debug,
|
||||||
|
Info,
|
||||||
|
Warning,
|
||||||
|
Error,
|
||||||
|
}
|
||||||
9
libs/logging/src/log.service.ts
Normal file
9
libs/logging/src/log.service.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { LogLevel } from "./log-level";
|
||||||
|
|
||||||
|
export abstract class LogService {
|
||||||
|
abstract debug(message?: any, ...optionalParams: any[]): void;
|
||||||
|
abstract info(message?: any, ...optionalParams: any[]): void;
|
||||||
|
abstract warning(message?: any, ...optionalParams: any[]): void;
|
||||||
|
abstract error(message?: any, ...optionalParams: any[]): void;
|
||||||
|
abstract write(level: LogLevel, message?: any, ...optionalParams: any[]): void;
|
||||||
|
}
|
||||||
8
libs/logging/src/logging.spec.ts
Normal file
8
libs/logging/src/logging.spec.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import * as lib from "./index";
|
||||||
|
|
||||||
|
describe("logging", () => {
|
||||||
|
// This test will fail until something is exported from index.ts
|
||||||
|
it("should work", () => {
|
||||||
|
expect(lib).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
13
libs/logging/tsconfig.json
Normal file
13
libs/logging/tsconfig.json
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"extends": "../../tsconfig.base.json",
|
||||||
|
"files": [],
|
||||||
|
"include": [],
|
||||||
|
"references": [
|
||||||
|
{
|
||||||
|
"path": "./tsconfig.lib.json"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "./tsconfig.spec.json"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
10
libs/logging/tsconfig.lib.json
Normal file
10
libs/logging/tsconfig.lib.json
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"extends": "./tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"outDir": "../../dist/out-tsc",
|
||||||
|
"declaration": true,
|
||||||
|
"types": ["node"]
|
||||||
|
},
|
||||||
|
"include": ["src/**/*.ts"],
|
||||||
|
"exclude": ["jest.config.js", "src/**/*.spec.ts"]
|
||||||
|
}
|
||||||
16
libs/logging/tsconfig.spec.json
Normal file
16
libs/logging/tsconfig.spec.json
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"extends": "./tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"outDir": "../..//dist/out-tsc",
|
||||||
|
"module": "commonjs",
|
||||||
|
"moduleResolution": "node10",
|
||||||
|
"types": ["jest", "node"]
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"jest.config.ts",
|
||||||
|
"src/**/*.test.ts",
|
||||||
|
"src/**/*.spec.ts",
|
||||||
|
"src/**/*.d.ts",
|
||||||
|
"src/intercept-console.ts"
|
||||||
|
]
|
||||||
|
}
|
||||||
8
package-lock.json
generated
8
package-lock.json
generated
@@ -353,6 +353,10 @@
|
|||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
"license": "GPL-3.0"
|
"license": "GPL-3.0"
|
||||||
},
|
},
|
||||||
|
"libs/logging": {
|
||||||
|
"version": "0.0.1",
|
||||||
|
"license": "GPL-3.0"
|
||||||
|
},
|
||||||
"libs/node": {
|
"libs/node": {
|
||||||
"name": "@bitwarden/node",
|
"name": "@bitwarden/node",
|
||||||
"version": "0.0.0",
|
"version": "0.0.0",
|
||||||
@@ -4583,6 +4587,10 @@
|
|||||||
"resolved": "libs/key-management-ui",
|
"resolved": "libs/key-management-ui",
|
||||||
"link": true
|
"link": true
|
||||||
},
|
},
|
||||||
|
"node_modules/@bitwarden/logging": {
|
||||||
|
"resolved": "libs/logging",
|
||||||
|
"link": true
|
||||||
|
},
|
||||||
"node_modules/@bitwarden/node": {
|
"node_modules/@bitwarden/node": {
|
||||||
"resolved": "libs/node",
|
"resolved": "libs/node",
|
||||||
"link": true
|
"link": true
|
||||||
|
|||||||
@@ -37,6 +37,7 @@
|
|||||||
"@bitwarden/importer-ui": ["./libs/importer/src/components"],
|
"@bitwarden/importer-ui": ["./libs/importer/src/components"],
|
||||||
"@bitwarden/key-management": ["./libs/key-management/src"],
|
"@bitwarden/key-management": ["./libs/key-management/src"],
|
||||||
"@bitwarden/key-management-ui": ["./libs/key-management-ui/src"],
|
"@bitwarden/key-management-ui": ["./libs/key-management-ui/src"],
|
||||||
|
"@bitwarden/logging": ["libs/logging/src"],
|
||||||
"@bitwarden/node/*": ["./libs/node/src/*"],
|
"@bitwarden/node/*": ["./libs/node/src/*"],
|
||||||
"@bitwarden/nx-plugin": ["libs/nx-plugin/src/index.ts"],
|
"@bitwarden/nx-plugin": ["libs/nx-plugin/src/index.ts"],
|
||||||
"@bitwarden/platform": ["./libs/platform/src"],
|
"@bitwarden/platform": ["./libs/platform/src"],
|
||||||
|
|||||||
Reference in New Issue
Block a user