Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/kernel-cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- The daemon log filters entries below a minimum severity, defaulting to `info`, so high-volume `debug` output (refcount churn and similar) no longer dominates `daemon.log`; set `$OCAP_DAEMON_LOG_LEVEL` to `debug` to record everything again ([#1008](https://github.com/MetaMask/ocap-kernel/pull/1008))
- Relay state files (`relay.pid`, `relay.addr`) now live in their own directory (default `~/.libp2p-relay`, overridable via `$LIBP2P_RELAY_HOME`) instead of under `$OCAP_HOME`, so one libp2p relay can serve daemons with different OCAP_HOMEs ([#952](https://github.com/MetaMask/ocap-kernel/pull/952))

### Fixed
Expand Down
43 changes: 40 additions & 3 deletions packages/kernel-cli/src/commands/daemon-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,41 @@ import { join } from 'node:path';
import { getOcapHome } from '../ocap-home.ts';
import { isProcessAlive } from '../utils.ts';

// Mirror of @metamask/logger's level ordering (`logLevels` is not part
// of the package's public surface). Higher numbers are more severe.
// Declared above the file-scope logger construction so the transport
// factory doesn't hit a temporal-dead-zone reference when it's called
// during module init.
const LOG_LEVELS = {
debug: 1,
info: 2,
log: 3,
warn: 4,
error: 5,
} as const;

type LogLevelName = keyof typeof LOG_LEVELS;

/**
* Resolve the daemon's minimum log level from `OCAP_DAEMON_LOG_LEVEL`.
* Defaults to `info` so noisy `debug` entries (refcount churn etc.)
* are dropped; set the env var to `debug` to re-enable everything.
*
* @returns The minimum log level to record.
*/
function resolveMinLogLevel(): LogLevelName {
const raw = process.env.OCAP_DAEMON_LOG_LEVEL;
if (raw !== undefined && raw in LOG_LEVELS) {
return raw as LogLevelName;
}
return 'info';
}

const ocapDir = getOcapHome();
const logPath = join(ocapDir, 'daemon.log');
const logger = new Logger({
tags: ['daemon'],
transports: [makeFileTransport(logPath)],
transports: [makeFileTransport(logPath, resolveMinLogLevel())],
});

// Install exit-cause handlers at module load, before main() runs, so
Expand Down Expand Up @@ -139,13 +169,20 @@ async function readDaemonPid(pidPath: string): Promise<number | undefined> {
}

/**
* Create a file transport that writes logs to a file.
* Create a file transport that writes logs to a file, filtering out
* entries below `minLevel`.
*
* @param logFilePath - The log file path.
* @param minLevel - Minimum severity to write; entries below this are
* dropped silently.
* @returns A log transport function.
*/
function makeFileTransport(logFilePath: string) {
function makeFileTransport(logFilePath: string, minLevel: LogLevelName) {
const minIdx = LOG_LEVELS[minLevel];
return (entry: LogEntry): void => {
if (LOG_LEVELS[entry.level] < minIdx) {
return;
}
const line = `[${new Date().toISOString()}] [${entry.level}] ${entry.message ?? ''} ${(entry.data ?? []).map(String).join(' ')}\n`;
// eslint-disable-next-line n/no-sync -- synchronous write needed for log transport reliability
appendFileSync(logFilePath, line);
Expand Down
Loading