docmirror/dev-sidecar · error

未指定日志类型,无法配置并获取日志对象!!!

Error message

未指定日志类型,无法配置并获取日志对象!!!

What it means

getLogger(category) configures and returns a log4js logger per category ('core', 'gui', 'server', etc.). A category name is mandatory — it selects the log file and appenders. When called with a falsy category (undefined, null, ''), the function logs and throws this error since it cannot configure a logger without knowing which one is requested.

Source

Thrown at packages/core/src/utils/util.logger.js:76

    config.categories[category] = { appenders: logToConsole ? [category, 'std'] : [category], level }
  }

  log4js.configure(config)

  // 拿第一个日志类型来logger并设置到log变量中
  log = log4js.getLogger(categories[0])
  logOrConsole.setLogger(log)

  log.info(`设置日志配置完成,进程ID: ${process.pid},categories:[${categories}],config:`, JSON.stringify(config))
}

module.exports = {
  getLogger (category) {
    if (!category) {
      if (log) {
        log.error('未指定日志类型,无法配置并获取日志对象!!!')
      }
      throw new Error('未指定日志类型,无法配置并获取日志对象!!!')
    }

    if (category === 'core' || category === 'gui') {
      // core 和 gui 的日志配置,因为它们在同一进程中,所以一起配置,且只能配置一次
      if (log == null) {
        log4jsConfigure(['core', 'gui'])
      }

      return log4js.getLogger(category)
    } else {
      if (log == null) {
        log4jsConfigure([category])
      } else if (category !== log.category) {
        log.error(`当前进程已经设置过日志配置,无法再设置 "${category}" 的配置,先临时返回 "${log.category}" 的 log 进行日志记录。如果与其他类型的日志在同一进程中写入,请参照 core 和 gui 一起配置`)
      }

      return log
    }

View on GitHub (pinned to 7710cd56cc)

Solutions

  1. Pass an explicit category string: getLogger('core'), getLogger('server'), etc.
  2. Check the variable supplying the category — initialize or default it (e.g. `category || 'server'`)
  3. If the category comes from config/options, validate it is a non-empty string before calling getLogger
  4. Grep your call sites for getLogger( with missing/empty arguments

Example fix

// before
const log = getLogger(options.category) // undefined when options.category missing
// after
const log = getLogger(options.category || 'server')
Defensive patterns

Strategy: validation

Validate before calling

function getLoggerSafe(category) {
  if (typeof category !== 'string' || category.trim() === '') {
    category = 'server' // or throw your own descriptive error
  }
  return getLogger(category)
}

Type guard

function isValidCategory(c) { return typeof c === 'string' && c.trim() !== '' }

Try / catch

try {
  const log = getLogger(category)
} catch (e) {
  if (String(e.message).includes('未指定日志类型')) {
    console.error('Logger category missing; defaulting to console')
    return console
  }
  throw e
}

Prevention

When it happens

Trigger: Calling util.logger.getLogger() with no argument; passing an empty string or null category, typically from a variable that was not initialized (e.g. `getLogger(category)` where category comes from config or constructor that omitted it).

Common situations: Refactoring code and forgetting to pass the module name; constructing a class with an optional name field left undefined; copying a getLogger call and deleting the literal; config-driven category lookup returning empty.

Related errors


AI-assisted analysis of docmirror/dev-sidecar@7710cd56cc (2026-08-31). Data as JSON: /api/errors/1d18cd7565c2ed6f. Report an issue: GitHub.