diff --git a/api/docs/public/cli.md b/api/docs/public/cli.md index 4bfa26e879..27d33c4322 100644 --- a/api/docs/public/cli.md +++ b/api/docs/public/cli.md @@ -21,7 +21,14 @@ unraid-api start [--log-level ] Starts the Unraid API service. Options: -- `--log-level`: Set logging level (trace|debug|info|warn|error) + +- `--log-level`: Set logging level (trace|debug|info|warn|error|fatal) + +Alternative: You can also set the log level using the `LOG_LEVEL` environment variable: + +```bash +LOG_LEVEL=trace unraid-api start +``` ### Stop @@ -36,11 +43,21 @@ Stops the Unraid API service. ### Restart ```bash -unraid-api restart +unraid-api restart [--log-level ] ``` Restarts the Unraid API service. +Options: + +- `--log-level`: Set logging level (trace|debug|info|warn|error|fatal) + +Alternative: You can also set the log level using the `LOG_LEVEL` environment variable: + +```bash +LOG_LEVEL=trace unraid-api restart +``` + ### Logs ```bash diff --git a/api/src/unraid-api/cli/restart.command.ts b/api/src/unraid-api/cli/restart.command.ts index c3668f44ff..da6005a68c 100644 --- a/api/src/unraid-api/cli/restart.command.ts +++ b/api/src/unraid-api/cli/restart.command.ts @@ -1,9 +1,23 @@ -import { Command, CommandRunner } from 'nest-commander'; +import { Command, CommandRunner, Option } from 'nest-commander'; -import { ECOSYSTEM_PATH } from '@app/environment.js'; +import type { LogLevel } from '@app/core/log.js'; +import { levels } from '@app/core/log.js'; +import { ECOSYSTEM_PATH, LOG_LEVEL } from '@app/environment.js'; import { LogService } from '@app/unraid-api/cli/log.service.js'; import { PM2Service } from '@app/unraid-api/cli/pm2.service.js'; +export interface LogLevelOptions { + logLevel?: LogLevel; +} + +export function parseLogLevelOption(val: string, allowedLevels: string[] = [...levels]): LogLevel { + const normalized = val.toLowerCase() as LogLevel; + if (allowedLevels.includes(normalized)) { + return normalized; + } + throw new Error(`Invalid --log-level "${val}". Allowed: ${allowedLevels.join(', ')}`); +} + @Command({ name: 'restart', description: 'Restart the Unraid API' }) export class RestartCommand extends CommandRunner { constructor( @@ -13,11 +27,12 @@ export class RestartCommand extends CommandRunner { super(); } - async run(): Promise { + async run(_?: string[], options: LogLevelOptions = {}): Promise { try { this.logger.info('Restarting the Unraid API...'); + const env = { LOG_LEVEL: options.logLevel }; const { stderr, stdout } = await this.pm2.run( - { tag: 'PM2 Restart', raw: true }, + { tag: 'PM2 Restart', raw: true, extendEnv: true, env }, 'restart', ECOSYSTEM_PATH, '--update-env' @@ -40,4 +55,13 @@ export class RestartCommand extends CommandRunner { process.exit(1); } } + + @Option({ + flags: `--log-level <${levels.join('|')}>`, + description: 'log level to use', + defaultValue: LOG_LEVEL.toLowerCase(), + }) + parseLogLevel(val: string): LogLevel { + return parseLogLevelOption(val); + } } diff --git a/api/src/unraid-api/cli/start.command.ts b/api/src/unraid-api/cli/start.command.ts index 33e2c4845d..74d512574b 100644 --- a/api/src/unraid-api/cli/start.command.ts +++ b/api/src/unraid-api/cli/start.command.ts @@ -1,14 +1,12 @@ import { Command, CommandRunner, Option } from 'nest-commander'; import type { LogLevel } from '@app/core/log.js'; +import type { LogLevelOptions } from '@app/unraid-api/cli/restart.command.js'; import { levels } from '@app/core/log.js'; -import { ECOSYSTEM_PATH } from '@app/environment.js'; +import { ECOSYSTEM_PATH, LOG_LEVEL } from '@app/environment.js'; import { LogService } from '@app/unraid-api/cli/log.service.js'; import { PM2Service } from '@app/unraid-api/cli/pm2.service.js'; - -interface StartCommandOptions { - 'log-level'?: string; -} +import { parseLogLevelOption } from '@app/unraid-api/cli/restart.command.js'; @Command({ name: 'start', description: 'Start the Unraid API' }) export class StartCommand extends CommandRunner { @@ -27,17 +25,12 @@ export class StartCommand extends CommandRunner { await this.pm2.run({ tag: 'PM2 Delete' }, 'delete', ECOSYSTEM_PATH); } - async run(_: string[], options: StartCommandOptions): Promise { + async run(_: string[], options: LogLevelOptions): Promise { this.logger.info('Starting the Unraid API'); await this.cleanupPM2State(); - - const env: Record = {}; - if (options['log-level']) { - env.LOG_LEVEL = options['log-level']; - } - + const env = { LOG_LEVEL: options.logLevel }; const { stderr, stdout } = await this.pm2.run( - { tag: 'PM2 Start', env, raw: true }, + { tag: 'PM2 Start', raw: true, extendEnv: true, env }, 'start', ECOSYSTEM_PATH, '--update-env' @@ -54,9 +47,9 @@ export class StartCommand extends CommandRunner { @Option({ flags: `--log-level <${levels.join('|')}>`, description: 'log level to use', - defaultValue: 'info', + defaultValue: LOG_LEVEL.toLowerCase(), }) parseLogLevel(val: string): LogLevel { - return levels.includes(val as LogLevel) ? (val as LogLevel) : 'info'; + return parseLogLevelOption(val); } }