Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat(core): add `logLevel` flag to toggle the switch of log service (#662)

白熱b290da7f9745
diff --git a/examples/sheets/src/main.ts b/examples/sheets/src/main.ts
index 23ed1bb70e..281ac65e14 100644
--- a/examples/sheets/src/main.ts
+++ b/examples/sheets/src/main.ts
@@ -1,4 +1,4 @@
-import { LocaleType, Univer } from '@univerjs/core';
+import { LocaleType, LogLevel, Univer } from '@univerjs/core';
import { greenTheme } from '@univerjs/design';
import { DocsPlugin, RichTextEditingMutation } from '@univerjs/docs';
import { BaseFormulaEnginePlugin } from '@univerjs/engine-formula';
@@ -30,6 +30,7 @@ const univer = new Univer({
theme: greenTheme,
locale: LocaleType.EN_US,
locales,
+ logLevel: LogLevel.VERBOSE,
});
// core plugins
diff --git a/packages/core/src/basics/univer.ts b/packages/core/src/basics/univer.ts
index 1420a533b3..1b8f2fc367 100644
--- a/packages/core/src/basics/univer.ts
+++ b/packages/core/src/basics/univer.ts
@@ -53,10 +53,12 @@ export class Univer {
constructor(univerData: Partial<IUniverData> = {}) {
this._rootInjector = this._initDependencies();
- const { theme, locale, locales } = univerData;
+ const { theme, locale, locales, logLevel } = univerData;
+
theme && this._rootInjector.get(ThemeService).setTheme(theme);
locales && this._rootInjector.get(LocaleService).load(locales);
locale && this._rootInjector.get(LocaleService).setLocale(locale);
+ logLevel && this._rootInjector.get(ILogService).setLogLevel(logLevel);
}
__getInjector(): Injector {
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index ac5e28837c..3b2282bcf9 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -51,7 +51,7 @@ export { LifecycleStages, OnLifecycle, runOnLifecycle } from './services/lifecyc
export { LifecycleService } from './services/lifecycle/lifecycle.service';
export { ILocalStorageService } from './services/local-storage/local-storage.service';
export { LocaleService } from './services/locale/locale.service';
-export { DesktopLogService, ILogService } from './services/log/log.service';
+export { DesktopLogService, ILogService, LogLevel } from './services/log/log.service';
export {
IPermissionService,
PermissionService,
diff --git a/packages/core/src/services/log/log.service.ts b/packages/core/src/services/log/log.service.ts
index f19eeddcce..1c46535ada 100644
--- a/packages/core/src/services/log/log.service.ts
+++ b/packages/core/src/services/log/log.service.ts
@@ -1,25 +1,32 @@
+/* eslint-disable no-magic-numbers */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { createIdentifier } from '@wendellhu/redi';
import { Disposable } from '../../shared/lifecycle';
+export enum LogLevel {
+ SILENT = 0,
+ ERROR = 1,
+ WARN = 2,
+ VERBOSE = 3,
+}
+
export interface ILogService {
log(...args: any[]): void;
warn(...args: any[]): void;
error(...args: any[]): void;
- toggleLogEnabled(enabled: boolean): void;
- getLogEnabled(): boolean;
+ setLogLevel(enabled: LogLevel): void;
}
export const ILogService = createIdentifier<ILogService>('univer.log');
export class DesktopLogService extends Disposable implements ILogService {
- private _logEnabled = true;
+ private _logLevel: LogLevel = LogLevel.SILENT;
log(...args: any[]): void {
- if (!this._logEnabled || !args.length) {
+ if (this._logLevel < LogLevel.VERBOSE || !args.length) {
return;
}
@@ -33,22 +40,18 @@ export class DesktopLogService extends Disposable implements ILogService {
}
warn(...args: any[]): void {
- if (this._logEnabled) {
+ if (this._logLevel >= LogLevel.WARN) {
console.warn(...args);
}
}
error(...args: any[]): void {
- if (this._logEnabled) {
+ if (this._logLevel >= LogLevel.ERROR) {
console.error(...args);
}
}
- toggleLogEnabled(enabled: boolean): void {
- this._logEnabled = enabled;
- }
-
- getLogEnabled(): boolean {
- return this._logEnabled;
+ setLogLevel(logLevel: LogLevel): void {
+ this._logLevel = logLevel;
}
}
diff --git a/packages/core/src/sheets/__tests__/create-core-test-bed.ts b/packages/core/src/sheets/__tests__/create-core-test-bed.ts
index 55f1e29a43..a284ee20a2 100644
--- a/packages/core/src/sheets/__tests__/create-core-test-bed.ts
+++ b/packages/core/src/sheets/__tests__/create-core-test-bed.ts
@@ -4,7 +4,7 @@ import { Inject, Injector } from '@wendellhu/redi';
import { Univer } from '../../basics/univer';
import { Plugin, PluginType } from '../../plugin/plugin';
import { IUniverInstanceService } from '../../services/instance/instance.service';
-import { ILogService } from '../../services/log/log.service';
+import { ILogService, LogLevel } from '../../services/log/log.service';
import { LocaleType } from '../../types/enum/locale-type';
import type { IWorkbookData } from '../../types/interfaces/i-workbook-data';
@@ -66,7 +66,7 @@ export function createCoreTestBed(workbookConfig?: IWorkbookData, dependencies?:
univerInstanceService.focusUniverInstance('test');
const logService = get(ILogService);
- logService.toggleLogEnabled(false);
+ logService.setLogLevel(LogLevel.SILENT);
return {
univer,
diff --git a/packages/core/src/types/interfaces/i-univer-data.ts b/packages/core/src/types/interfaces/i-univer-data.ts
index 1750cb2c1e..e5eeed2cc0 100644
--- a/packages/core/src/types/interfaces/i-univer-data.ts
+++ b/packages/core/src/types/interfaces/i-univer-data.ts
@@ -1,3 +1,4 @@
+import type { LogLevel } from '../../services/log/log.service';
import type { IStyleSheet } from '../../services/theme/theme.service';
import type { ILocales } from '../../shared/locale';
import type { LocaleType } from '../enum';
@@ -6,6 +7,7 @@ export interface IUniverData {
theme: IStyleSheet;
locale: LocaleType;
locales: ILocales;
+ logLevel: LogLevel;
id: string;
}
diff --git a/packages/docs/src/commands/commands/__tests__/create-command-test-bed.ts b/packages/docs/src/commands/commands/__tests__/create-command-test-bed.ts
index 745d2c3607..765a58d28e 100644
--- a/packages/docs/src/commands/commands/__tests__/create-command-test-bed.ts
+++ b/packages/docs/src/commands/commands/__tests__/create-command-test-bed.ts
@@ -1,5 +1,13 @@
import type { IDocumentData } from '@univerjs/core';
-import { BooleanNumber, ILogService, IUniverInstanceService, Plugin, PluginType, Univer } from '@univerjs/core';
+import {
+ BooleanNumber,
+ ILogService,
+ IUniverInstanceService,
+ LogLevel,
+ Plugin,
+ PluginType,
+ Univer,
+} from '@univerjs/core';
import type { Dependency } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';
@@ -109,7 +117,7 @@ export function createCommandTestBed(workbookConfig?: IDocumentData, dependencie
univerInstanceService.focusUniverInstance('test-doc');
const logService = get(ILogService);
- logService.toggleLogEnabled(false); // change this to `true` to debug tests via logs
+ logService.setLogLevel(LogLevel.SILENT); // change this to `LogLevel.VERBOSE` to debug tests via logs
return {
univer,
diff --git a/packages/engine-formula/src/engine/analysis/__tests__/create-command-test-bed.ts b/packages/engine-formula/src/engine/analysis/__tests__/create-command-test-bed.ts
index e201437959..2615d4e231 100644
--- a/packages/engine-formula/src/engine/analysis/__tests__/create-command-test-bed.ts
+++ b/packages/engine-formula/src/engine/analysis/__tests__/create-command-test-bed.ts
@@ -1,5 +1,5 @@
import type { IWorkbookData } from '@univerjs/core';
-import { ILogService, IUniverInstanceService, LocaleType, Plugin, PluginType, Univer } from '@univerjs/core';
+import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core';
import type { Dependency } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';
@@ -91,7 +91,7 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencie
univerInstanceService.focusUniverInstance('test');
const logService = get(ILogService);
- logService.toggleLogEnabled(false); // change this to `true` to debug tests via logs
+ logService.setLogLevel(LogLevel.SILENT); // change this to `LogLevel.VERBOSE` to debug tests via logs
return {
univer,
diff --git a/packages/sheets-formula/src/commands/operations/__tests__/create-command-test-bed.ts b/packages/sheets-formula/src/commands/operations/__tests__/create-command-test-bed.ts
index a91bcebfca..3c6f99a484 100644
--- a/packages/sheets-formula/src/commands/operations/__tests__/create-command-test-bed.ts
+++ b/packages/sheets-formula/src/commands/operations/__tests__/create-command-test-bed.ts
@@ -1,5 +1,5 @@
import type { IWorkbookData } from '@univerjs/core';
-import { ILogService, IUniverInstanceService, LocaleType, Plugin, PluginType, Univer } from '@univerjs/core';
+import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core';
import { FormulaEngineService } from '@univerjs/engine-formula';
import { SelectionManagerService } from '@univerjs/sheets';
import type { Dependency } from '@wendellhu/redi';
@@ -73,7 +73,7 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencie
univerInstanceService.focusUniverInstance('test');
const logService = get(ILogService);
- logService.toggleLogEnabled(false); // change this to `true` to debug tests via logs
+ logService.setLogLevel(LogLevel.SILENT); // change this to `LogLevel.VERBOSE` to debug tests via logs
return {
univer,
diff --git a/packages/sheets-formula/src/controllers/__tests__/create-command-test-bed.ts b/packages/sheets-formula/src/controllers/__tests__/create-command-test-bed.ts
index 9b87f7edce..6799ca1048 100644
--- a/packages/sheets-formula/src/controllers/__tests__/create-command-test-bed.ts
+++ b/packages/sheets-formula/src/controllers/__tests__/create-command-test-bed.ts
@@ -1,5 +1,5 @@
import type { IWorkbookData, Workbook } from '@univerjs/core';
-import { ILogService, IUniverInstanceService, LocaleType, Plugin, PluginType, Univer } from '@univerjs/core';
+import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core';
import { FormulaEngineService } from '@univerjs/engine-formula';
import { SelectionManagerService } from '@univerjs/sheets';
import type { Dependency } from '@wendellhu/redi';
@@ -82,7 +82,7 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencie
univerInstanceService.focusUniverInstance('test');
const logService = injector.get(ILogService);
- logService.toggleLogEnabled(false); // change this to `true` to debug tests via logs
+ logService.setLogLevel(LogLevel.SILENT); // change this to `LogLevel.VERBOSE` to debug tests via logs
return {
univer,
diff --git a/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-test-bed.ts b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-test-bed.ts
index 0b089787b7..22e93808fc 100644
--- a/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-test-bed.ts
+++ b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-test-bed.ts
@@ -1,5 +1,5 @@
import type { IWorkbookData } from '@univerjs/core';
-import { ILogService, IUniverInstanceService, LocaleType, Plugin, PluginType, Univer } from '@univerjs/core';
+import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core';
import { IRenderManagerService, RenderManagerService } from '@univerjs/engine-render';
import { SelectionManagerService } from '@univerjs/sheets';
import {
@@ -557,7 +557,7 @@ export function clipboardTestBed(workbookConfig?: IWorkbookData, dependencies?:
univerInstanceService.focusUniverInstance('test');
const logService = get(ILogService);
- logService.toggleLogEnabled(false); // change this to `true` to debug tests via logs
+ logService.setLogLevel(LogLevel.SILENT); // change this to `LogLevel.VERBOSE` to debug tests via logs
return {
univer,
diff --git a/packages/sheets/src/commands/commands/__tests__/create-command-test-bed.ts b/packages/sheets/src/commands/commands/__tests__/create-command-test-bed.ts
index 569cec0ed1..77f738ae54 100644
--- a/packages/sheets/src/commands/commands/__tests__/create-command-test-bed.ts
+++ b/packages/sheets/src/commands/commands/__tests__/create-command-test-bed.ts
@@ -1,5 +1,5 @@
import type { IWorkbookData, Workbook } from '@univerjs/core';
-import { ILogService, IUniverInstanceService, LocaleType, Plugin, PluginType, Univer } from '@univerjs/core';
+import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core';
import type { Dependency } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';
@@ -69,7 +69,7 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencie
univerInstanceService.focusUniverInstance('test');
const logService = injector.get(ILogService);
- logService.toggleLogEnabled(false); // change this to `true` to debug tests via logs
+ logService.setLogLevel(LogLevel.SILENT); // change this to `LogLevel.VERBOSE` to debug tests via logs
return {
univer,
diff --git a/packages/sheets/src/controllers/__tests__/util.ts b/packages/sheets/src/controllers/__tests__/util.ts
index 63f521079f..bd86e25d83 100644
--- a/packages/sheets/src/controllers/__tests__/util.ts
+++ b/packages/sheets/src/controllers/__tests__/util.ts
@@ -1,5 +1,5 @@
import type { IWorkbookData } from '@univerjs/core';
-import { ILogService, IUniverInstanceService, LocaleType, Plugin, PluginType, Univer } from '@univerjs/core';
+import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core';
import type { Dependency } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';
@@ -68,7 +68,7 @@ export function createTestBase(workbookConfig?: IWorkbookData, dependencies?: De
univerInstanceService.focusUniverInstance('test');
const logService = get(ILogService);
- logService.toggleLogEnabled(false); // change this to `true` to debug tests via logs
+ logService.setLogLevel(LogLevel.SILENT); // change this to `LogLevel.VERBOSE` to debug tests via logs
return {
univer,
diff --git a/packages/sheets/src/services/__tests__/util.ts b/packages/sheets/src/services/__tests__/util.ts
index 09989079ba..bb5d214f8b 100644
--- a/packages/sheets/src/services/__tests__/util.ts
+++ b/packages/sheets/src/services/__tests__/util.ts
@@ -1,5 +1,5 @@
import type { IWorkbookData } from '@univerjs/core';
-import { ILogService, IUniverInstanceService, LocaleType, Plugin, PluginType, Univer } from '@univerjs/core';
+import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core';
import type { Dependency } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';
@@ -81,7 +81,7 @@ export function createTestBase(workbookConfig?: IWorkbookData, dependencies?: De
univerInstanceService.focusUniverInstance('test');
const logService = get(ILogService);
- logService.toggleLogEnabled(false); // change this to `true` to debug tests via logs
+ logService.setLogLevel(LogLevel.SILENT); // change this to `LogLevel.VERBOSE` to debug tests via logs
return {
univer,
diff --git a/packages/sheets/src/services/ref-range/__tests__/util.ts b/packages/sheets/src/services/ref-range/__tests__/util.ts
index 09989079ba..bb5d214f8b 100644
--- a/packages/sheets/src/services/ref-range/__tests__/util.ts
+++ b/packages/sheets/src/services/ref-range/__tests__/util.ts
@@ -1,5 +1,5 @@
import type { IWorkbookData } from '@univerjs/core';
-import { ILogService, IUniverInstanceService, LocaleType, Plugin, PluginType, Univer } from '@univerjs/core';
+import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core';
import type { Dependency } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';
@@ -81,7 +81,7 @@ export function createTestBase(workbookConfig?: IWorkbookData, dependencies?: De
univerInstanceService.focusUniverInstance('test');
const logService = get(ILogService);
- logService.toggleLogEnabled(false); // change this to `true` to debug tests via logs
+ logService.setLogLevel(LogLevel.SILENT); // change this to `LogLevel.VERBOSE` to debug tests via logs
return {
univer,
diff --git a/packages/sheets/src/services/sheet-interceptor/__tests__/create-core-test-bed.ts b/packages/sheets/src/services/sheet-interceptor/__tests__/create-core-test-bed.ts
index 62d985be17..78041880bc 100644
--- a/packages/sheets/src/services/sheet-interceptor/__tests__/create-core-test-bed.ts
+++ b/packages/sheets/src/services/sheet-interceptor/__tests__/create-core-test-bed.ts
@@ -1,5 +1,5 @@
import type { IWorkbookData } from '@univerjs/core';
-import { ILogService, IUniverInstanceService, LocaleType, Plugin, PluginType, Univer } from '@univerjs/core';
+import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core';
import type { Dependency } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';
@@ -65,7 +65,7 @@ export function createCoreTestBed(workbookConfig?: IWorkbookData, dependencies?:
univerInstanceService.focusUniverInstance('test');
const logService = get(ILogService);
- logService.toggleLogEnabled(false);
+ logService.setLogLevel(LogLevel.SILENT);
return {
univer,