Skip to content

Commit b3a4036

Browse files
committed
fix(logger): log level, use single class mode
1 parent 8bd3ca6 commit b3a4036

File tree

9 files changed

+32
-37
lines changed

9 files changed

+32
-37
lines changed

src/cli.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,9 @@ import process, { env, exit } from "node:process";
44
import { Command } from "@commander-js/extra-typings";
55
import pkg from "../package.json" with { type: "json" };
66
import { Build, Config, Release, Serve, Test } from "./index.js";
7-
import { Log } from "./utils/log.js";
7+
import { logger } from "./utils/log.js";
88
import { updateNotifier } from "./utils/updater.js";
99

10-
const logger = new Log();
11-
// let globalOpts: {
12-
// configCwd?: string;
13-
// } = {
14-
// configCwd: cwd(),
15-
// };
16-
1710
export default async function main() {
1811
const { name, version } = pkg;
1912
updateNotifier(name, version);

src/config.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import type { Config, Context, Hooks, OverrideConfig, UserConfig } from "./types/index.js";
2-
import { env } from "node:process";
32
import { loadConfig as c12 } from "c12";
43
import { kebabCase, mapValues } from "es-toolkit";
54
import { readJsonSync } from "fs-extra/esm";
65
import { createHooks } from "hookable";
7-
import { Log } from "./utils/log.js";
6+
import { logger } from "./utils/log.js";
87
import { dateFormat, parseRepoUrl, template } from "./utils/string.js";
98

109
/**
@@ -32,9 +31,7 @@ export async function loadConfig(overrides?: OverrideConfig): Promise<Context> {
3231
}
3332

3433
function resolveConfig(config: Config): Context {
35-
// Sync log level to env so that logs in utils are also output as expected by log level
36-
env.ZOTERO_PLUGIN_LOG_LEVEL = config.logLevel;
37-
const logger = new Log(config);
34+
logger.setLogLevel(config.logLevel);
3835

3936
// Load user's package.json
4037
const pkgUser = readJsonSync("package.json", {

src/creator.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// npx 创建模板
22
// import { input } from "@inquirer/prompts";
3-
import { Log } from "./utils/log.js";
4-
5-
const logger = new Log();
3+
import { logger } from "./utils/log.js";
64

75
export default class Create {
86
async run() {

src/types/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { BuildOptions } from "esbuild";
2+
import type { LogLevelType } from "../utils/log.js";
23
import type { Context } from "./index.js";
34
import type { Manifest } from "./manifest.js";
45
import type { UpdateJSON } from "./update-json.js";
@@ -153,7 +154,7 @@ export interface Config {
153154
*
154155
* @default "info"
155156
*/
156-
logLevel: "trace" | "debug" | "info" | "warn" | "error";
157+
logLevel: LogLevelType;
157158
}
158159

159160
export interface BuildConfig {

src/utils/headless.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { execSync } from "node:child_process";
22
import { exit } from "node:process";
33
import { isLinux } from "std-env";
4-
import { Log } from "./log.js";
5-
6-
const logger = new Log();
4+
import { logger } from "./log.js";
75

86
export async function installXvfb() {
97
logger.debug("Installing xvfb...");

src/utils/log.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { Config } from "../types/index.js";
21
import process, { env } from "node:process";
32
import readline from "node:readline";
43
import chalk from "chalk";
@@ -8,28 +7,42 @@ import { isDebug } from "std-env";
87
/**
98
* Log level
109
*/
11-
enum LOG_LEVEL {
10+
export enum LOG_LEVEL {
1211
trace = 0,
1312
debug = 1,
1413
info = 2,
1514
warn = 3,
1615
error = 4,
1716
}
1817

18+
export type LogLevelType = keyof typeof LOG_LEVEL;
19+
1920
/**
2021
* Logger
2122
*/
2223
export class Log {
24+
private static instance: Log;
2325
private logLevel: number;
24-
constructor(config?: Config) {
26+
constructor(level?: LOG_LEVEL) {
2527
if (isDebug)
2628
this.logLevel = LOG_LEVEL.trace;
27-
else if (config)
28-
this.logLevel = LOG_LEVEL[config.logLevel];
2929
else if (env.ZOTERO_PLUGIN_LOG_LEVEL)
30-
this.logLevel = LOG_LEVEL[env.ZOTERO_PLUGIN_LOG_LEVEL as Config["logLevel"]];
30+
this.logLevel = LOG_LEVEL[env.ZOTERO_PLUGIN_LOG_LEVEL as LogLevelType];
31+
else if (level)
32+
this.logLevel = level;
3133
else
32-
this.logLevel = LOG_LEVEL.trace;
34+
this.logLevel = LOG_LEVEL.info;
35+
}
36+
37+
static getInstance(): Log {
38+
if (!Log.instance) {
39+
Log.instance = new Log();
40+
}
41+
return Log.instance;
42+
}
43+
44+
setLogLevel(level: LogLevelType) {
45+
this.logLevel = LOG_LEVEL[level];
3346
}
3447

3548
private formatArgs(arg: any): string {
@@ -117,3 +130,5 @@ export class Log {
117130
console.log("");
118131
}
119132
}
133+
134+
export const logger = Log.getInstance();

src/utils/updater.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import process from "node:process";
22
import tinyUpdateNotifier from "tiny-update-notifier";
3-
import { Log } from "./log.js";
3+
import { logger } from "./log.js";
44
import { ExitSignals } from "./process.js";
55

6-
const logger = new Log();
7-
86
export function updateNotifier(name: string, version: string) {
97
tinyUpdateNotifier({ pkg: { name, version } }).then((update) => {
108
if (update) {

src/utils/zotero-runner.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ import { env } from "node:process";
66
import { delay } from "es-toolkit";
77
import { outputFile, outputJSON, pathExists, readJSON, remove } from "fs-extra/esm";
88
import { isLinux, isMacOS, isWindows } from "std-env";
9-
import { Log } from "./log.js";
9+
import { logger } from "./log.js";
1010
import { isRunning } from "./process.js";
1111
import { prefs } from "./zotero/preference.js";
1212
import { findFreeTcpPort, RemoteFirefox } from "./zotero/remote-zotero.js";
1313

14-
const logger = new Log();
15-
1614
export interface ZoteroRunnerOptions {
1715
binaryPath: string;
1816
profilePath: string;
@@ -286,8 +284,6 @@ export class ZoteroRunner {
286284
}
287285

288286
export function killZotero() {
289-
const logger = new Log();
290-
291287
function kill() {
292288
try {
293289
if (env.ZOTERO_PLUGIN_KILL_COMMAND) {

src/utils/zotero/remote-zotero.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@
2222

2323
import net from "node:net";
2424

25-
import { Log } from "../log.js";
25+
import { logger } from "../log.js";
2626
import { MessagingClient } from "./rdp-client.js";
2727

28-
const logger = new Log();
2928
const MAX_RETRIES = 150;
3029
const RETRY_INTERVAL = 1000;
3130

0 commit comments

Comments
 (0)