Преглед на файлове

remove log level from config

Dax Raad преди 7 месеца
родител
ревизия
c1d87c32a2
променени са 3 файла, в които са добавени 19 реда и са изтрити 31 реда
  1. 0 1
      packages/opencode/src/config/config.ts
  2. 15 19
      packages/opencode/src/index.ts
  3. 4 11
      packages/opencode/src/util/log.ts

+ 0 - 1
packages/opencode/src/config/config.ts

@@ -167,7 +167,6 @@ export namespace Config {
         .catchall(Mode)
         .catchall(Mode)
         .optional()
         .optional()
         .describe("Modes configuration, see https://opencode.ai/docs/modes"),
         .describe("Modes configuration, see https://opencode.ai/docs/modes"),
-      log_level: Log.Level.optional().describe("Minimum log level to write to log files"),
       provider: z
       provider: z
         .record(
         .record(
           ModelsDev.Provider.partial().extend({
           ModelsDev.Provider.partial().extend({

+ 15 - 19
packages/opencode/src/index.ts

@@ -44,25 +44,21 @@ const cli = yargs(hideBin(process.argv))
     describe: "print logs to stderr",
     describe: "print logs to stderr",
     type: "boolean",
     type: "boolean",
   })
   })
-  .middleware(async () => {
-    await Log.init({ print: process.argv.includes("--print-logs"), dev: Installation.isDev() })
-
-    try {
-      const { Config } = await import("./config/config")
-      const { App } = await import("./app/app")
-
-      App.provide({ cwd: process.cwd() }, async () => {
-        const cfg = await Config.get()
-        if (cfg.log_level) {
-          Log.setLevel(cfg.log_level as Log.Level)
-        } else {
-          const defaultLevel = Installation.isDev() ? "DEBUG" : "INFO"
-          Log.setLevel(defaultLevel)
-        }
-      })
-    } catch (e) {
-      Log.Default.error("failed to load config", { error: e })
-    }
+  .option("log-level", {
+    describe: "log level",
+    type: "string",
+    choices: ["DEBUG", "INFO", "WARN", "ERROR"],
+  })
+  .middleware(async (opts) => {
+    await Log.init({
+      print: process.argv.includes("--print-logs"),
+      dev: Installation.isDev(),
+      level: (() => {
+        if (opts.logLevel) return opts.logLevel as Log.Level
+        if (Installation.isDev()) return "DEBUG"
+        return "INFO"
+      })(),
+    })
 
 
     Log.Default.info("opencode", {
     Log.Default.info("opencode", {
       version: Installation.VERSION,
       version: Installation.VERSION,

+ 4 - 11
packages/opencode/src/util/log.ts

@@ -14,18 +14,10 @@ export namespace Log {
     ERROR: 3,
     ERROR: 3,
   }
   }
 
 
-  let currentLevel: Level = "INFO"
+  let level: Level = "INFO"
 
 
-  export function setLevel(level: Level) {
-    currentLevel = level
-  }
-
-  export function getLevel(): Level {
-    return currentLevel
-  }
-
-  function shouldLog(level: Level): boolean {
-    return levelPriority[level] >= levelPriority[currentLevel]
+  function shouldLog(input: Level): boolean {
+    return levelPriority[input] >= levelPriority[level]
   }
   }
 
 
   export type Logger = {
   export type Logger = {
@@ -60,6 +52,7 @@ export namespace Log {
   }
   }
 
 
   export async function init(options: Options) {
   export async function init(options: Options) {
+    if (options.level) level = options.level
     const dir = path.join(Global.Path.data, "log")
     const dir = path.join(Global.Path.data, "log")
     await fs.mkdir(dir, { recursive: true })
     await fs.mkdir(dir, { recursive: true })
     cleanup(dir)
     cleanup(dir)