openclaw - 💡(How to fix) Fix [Bug] Logger _meta.hostname always "unknown" on macOS — os.hostname() returns empty string at module-load time [1 pull requests]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Root Cause

The hostname is set as a module-level constant in src/logging/logger.ts:

const HOSTNAME = os.hostname() || "unknown";

This is evaluated once when the logger module is first imported during Gateway startup. The value is then cached for the entire lifetime of the Gateway process.

On macOS, os.hostname() returns an empty string ("") intermittently during the module load phase, likely due to a timing/race condition in Node.js ESM module resolution. After the Gateway has fully started, os.hostname() works correctly — but the module-level constant is never re-evaluated.

Fix Action

Fixed

Code Example

const HOSTNAME = os.hostname() || "unknown";

---

// Current — evaluated once at module load
const HOSTNAME = os.hostname() || "unknown";

// Fix — evaluated each time a log entry is written
function getHostname(): string {
  const h = os.hostname();
  return h || "unknown";
}
RAW_BUFFERClick to expand / collapse

Bug Description

The structured JSON log output (/tmp/openclaw/openclaw-YYYY-MM-DD.log) always shows _meta.hostname: "unknown" regardless of the actual system hostname.

Root Cause

The hostname is set as a module-level constant in src/logging/logger.ts:

const HOSTNAME = os.hostname() || "unknown";

This is evaluated once when the logger module is first imported during Gateway startup. The value is then cached for the entire lifetime of the Gateway process.

On macOS, os.hostname() returns an empty string ("") intermittently during the module load phase, likely due to a timing/race condition in Node.js ESM module resolution. After the Gateway has fully started, os.hostname() works correctly — but the module-level constant is never re-evaluated.

Evidence

  • The log file contains entries from three different hostname values across restart cycles:
    • "unknown" — 7017 entries (core subsystems: gateway/ws, diagnostic, plugins)
    • "192.168.1.5" — 97 entries (old hostname)
    • "lr-macbook" — 8 entries (new hostname from the weixin plugin, which uses a DIFFERENT logger instance)
  • The weixin plugin uses its own logger instance, not the HOSTNAME constant from logger.ts, so it correctly picks up the hostname.
  • Running node -e "require(\"os\").hostname()" always returns the correct hostname.

Suggested Fix

Replace the module-level constant with a lazy function so the hostname is resolved at log-write time, not at module-load time:

// Current — evaluated once at module load
const HOSTNAME = os.hostname() || "unknown";

// Fix — evaluated each time a log entry is written
function getHostname(): string {
  const h = os.hostname();
  return h || "unknown";
}

Environment

  • OpenClaw v2026.5.12 (f066dd2)
  • macOS 12.6 (Darwin 21.6.0, x64)
  • Node.js v22.22.0

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

openclaw - 💡(How to fix) Fix [Bug] Logger _meta.hostname always "unknown" on macOS — os.hostname() returns empty string at module-load time [1 pull requests]