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.

refactor: add info level to logservice (#946)

Wenzhao Huf88025159aef
diff --git a/packages/core/src/services/command/command.service.ts b/packages/core/src/services/command/command.service.ts
index 8293364567..28f47d5a41 100644
--- a/packages/core/src/services/command/command.service.ts
+++ b/packages/core/src/services/command/command.service.ts
@@ -203,7 +203,7 @@ export class CommandService implements ICommandService {
constructor(
@Inject(Injector) private readonly _injector: Injector,
- @ILogService private readonly _log: ILogService
+ @ILogService private readonly _logService: ILogService
) {
this._commandRegistry = new CommandRegistry();
}
@@ -354,7 +354,7 @@ export class CommandService implements ICommandService {
}
private async _execute<P extends object, R = boolean>(command: ICommand<P, R>, params?: P): Promise<R> {
- this._log.log(
+ this._logService.debug(
'[CommandService]',
`${'|-'.repeat(Math.max(this._commandExecutingLevel, 0))}executing command "${command.id}"`
);
@@ -374,7 +374,7 @@ export class CommandService implements ICommandService {
}
private _syncExecute<P extends object, R = boolean>(command: ICommand<P, R>, params?: P): R {
- this._log.log(
+ this._logService.debug(
'[CommandService]',
`${'|-'.repeat(Math.max(0, this._commandExecutingLevel))}executing command "${command.id}".`
);
@@ -442,7 +442,7 @@ class MultiCommand implements IMultiCommand {
for (const item of this._implementations) {
const preconditions = item.command.preconditions;
if (!preconditions || (preconditions && preconditions(contextService))) {
- logService.log(`[MultiCommand]`, `executing implementation "${item.command.name}".`);
+ logService.debug(`[MultiCommand]`, `executing implementation "${item.command.name}".`);
const result = await injector.invoke(item.command.handler, params);
if (result) {
return true;
diff --git a/packages/core/src/services/lifecycle/lifecycle.service.ts b/packages/core/src/services/lifecycle/lifecycle.service.ts
index 984ef0956d..98899cb3b6 100644
--- a/packages/core/src/services/lifecycle/lifecycle.service.ts
+++ b/packages/core/src/services/lifecycle/lifecycle.service.ts
@@ -33,10 +33,7 @@ export class LifecycleService extends Disposable {
constructor(@ILogService private readonly _logService: ILogService) {
super();
- this._logService.log(
- `[LifecycleService]`,
- `lifecycle progressed to "${LifecycleNameMap[LifecycleStages.Starting]}".`
- );
+ this._reportProgress(LifecycleStages.Starting);
}
get stage(): LifecycleStages {
@@ -52,7 +49,7 @@ export class LifecycleService extends Disposable {
return;
}
- this._logService.log(`[LifecycleService]`, `lifecycle progressed to "${LifecycleNameMap[stage]}".`);
+ this._reportProgress(stage);
this._lifecycle$.next(stage);
}
@@ -88,6 +85,10 @@ export class LifecycleService extends Disposable {
return this._lifecycle$.subscribe(subscriber);
});
}
+
+ private _reportProgress(stage: LifecycleStages): void {
+ this._logService.debug(`[LifecycleService]`, `lifecycle progressed to "${LifecycleNameMap[stage]}".`);
+ }
}
/**
diff --git a/packages/core/src/services/log/log.service.ts b/packages/core/src/services/log/log.service.ts
index a75e0e5acc..b91c5e6c68 100644
--- a/packages/core/src/services/log/log.service.ts
+++ b/packages/core/src/services/log/log.service.ts
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+/* eslint-disable no-console */
+
import { createIdentifier } from '@wendellhu/redi';
import { Disposable } from '../../shared/lifecycle';
@@ -22,13 +24,15 @@ export enum LogLevel /* eslint-disable no-magic-numbers */ {
SILENT = 0,
ERROR = 1,
WARN = 2,
- VERBOSE = 3,
+ INFO = 3,
+ VERBOSE = 4,
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ArgsType = any[];
export interface ILogService {
+ debug(...args: ArgsType): void;
log(...args: ArgsType): void;
warn(...args: ArgsType): void;
error(...args: ArgsType): void;
@@ -39,39 +43,46 @@ export interface ILogService {
export const ILogService = createIdentifier<ILogService>('univer.log');
export class DesktopLogService extends Disposable implements ILogService {
- private _logLevel: LogLevel = LogLevel.SILENT;
+ private _logLevel: LogLevel = LogLevel.INFO;
- log(...args: ArgsType): void {
- if (this._logLevel < LogLevel.VERBOSE || !args.length) {
- return;
+ debug(...args: ArgsType): void {
+ if (this._logLevel >= LogLevel.VERBOSE) {
+ this._log(console.debug, ...args);
}
+ }
- const firstArg = args[0];
- const withTag = /^\[(.*?)\]/g.test(firstArg);
- if (withTag) {
- /* eslint-disable-next-line no-console */
- console.log(`\x1B[97;104m${firstArg}\x1B[0m:`, ...args.slice(1));
- } else {
- /* eslint-disable-next-line no-console */
- console.log(...args);
+ log(...args: ArgsType): void {
+ if (this._logLevel >= LogLevel.INFO) {
+ this._log(console.log, ...args);
}
}
warn(...args: ArgsType): void {
if (this._logLevel >= LogLevel.WARN) {
- /* eslint-disable-next-line no-console */
- console.warn(...args);
+ this._log(console.warn, ...args);
}
}
error(...args: ArgsType): void {
if (this._logLevel >= LogLevel.ERROR) {
- /* eslint-disable-next-line no-console */
- console.error(...args);
+ this._log(console.error, ...args);
}
}
setLogLevel(logLevel: LogLevel): void {
this._logLevel = logLevel;
}
+
+ private _log(
+ method: typeof console.log | typeof console.error | typeof console.warn | typeof console.debug,
+ ...args: ArgsType
+ ): void {
+ const firstArg = args[0];
+ const withTag = /^\[(.*?)\]/g.test(firstArg);
+ if (withTag) {
+ method(`\x1B[97;104m${firstArg}\x1B[0m:`, ...args.slice(1));
+ } else {
+ method(...args);
+ }
+ }
}