Skip to content

Commit a1ee915

Browse files
authored
fix(cli): support --log-level for start and restart cmds (#1623)
Resolve #1614 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Start and Restart commands accept a validated --log-level option (now includes "fatal"); chosen level is applied to the running/restarted service and defaults to the LOG_LEVEL environment value when set. * **Documentation** * CLI docs updated to list the --log-level option and allowed levels (including fatal), show LOG_LEVEL as an environment-variable alternative, and include usage examples. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent c147a6b commit a1ee915

File tree

3 files changed

+55
-21
lines changed

3 files changed

+55
-21
lines changed

api/docs/public/cli.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ unraid-api start [--log-level <level>]
2121
Starts the Unraid API service.
2222

2323
Options:
24-
- `--log-level`: Set logging level (trace|debug|info|warn|error)
24+
25+
- `--log-level`: Set logging level (trace|debug|info|warn|error|fatal)
26+
27+
Alternative: You can also set the log level using the `LOG_LEVEL` environment variable:
28+
29+
```bash
30+
LOG_LEVEL=trace unraid-api start
31+
```
2532

2633
### Stop
2734

@@ -36,11 +43,21 @@ Stops the Unraid API service.
3643
### Restart
3744

3845
```bash
39-
unraid-api restart
46+
unraid-api restart [--log-level <level>]
4047
```
4148

4249
Restarts the Unraid API service.
4350

51+
Options:
52+
53+
- `--log-level`: Set logging level (trace|debug|info|warn|error|fatal)
54+
55+
Alternative: You can also set the log level using the `LOG_LEVEL` environment variable:
56+
57+
```bash
58+
LOG_LEVEL=trace unraid-api restart
59+
```
60+
4461
### Logs
4562

4663
```bash

api/src/unraid-api/cli/restart.command.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1-
import { Command, CommandRunner } from 'nest-commander';
1+
import { Command, CommandRunner, Option } from 'nest-commander';
22

3-
import { ECOSYSTEM_PATH } from '@app/environment.js';
3+
import type { LogLevel } from '@app/core/log.js';
4+
import { levels } from '@app/core/log.js';
5+
import { ECOSYSTEM_PATH, LOG_LEVEL } from '@app/environment.js';
46
import { LogService } from '@app/unraid-api/cli/log.service.js';
57
import { PM2Service } from '@app/unraid-api/cli/pm2.service.js';
68

9+
export interface LogLevelOptions {
10+
logLevel?: LogLevel;
11+
}
12+
13+
export function parseLogLevelOption(val: string, allowedLevels: string[] = [...levels]): LogLevel {
14+
const normalized = val.toLowerCase() as LogLevel;
15+
if (allowedLevels.includes(normalized)) {
16+
return normalized;
17+
}
18+
throw new Error(`Invalid --log-level "${val}". Allowed: ${allowedLevels.join(', ')}`);
19+
}
20+
721
@Command({ name: 'restart', description: 'Restart the Unraid API' })
822
export class RestartCommand extends CommandRunner {
923
constructor(
@@ -13,11 +27,12 @@ export class RestartCommand extends CommandRunner {
1327
super();
1428
}
1529

16-
async run(): Promise<void> {
30+
async run(_?: string[], options: LogLevelOptions = {}): Promise<void> {
1731
try {
1832
this.logger.info('Restarting the Unraid API...');
33+
const env = { LOG_LEVEL: options.logLevel };
1934
const { stderr, stdout } = await this.pm2.run(
20-
{ tag: 'PM2 Restart', raw: true },
35+
{ tag: 'PM2 Restart', raw: true, extendEnv: true, env },
2136
'restart',
2237
ECOSYSTEM_PATH,
2338
'--update-env'
@@ -40,4 +55,13 @@ export class RestartCommand extends CommandRunner {
4055
process.exit(1);
4156
}
4257
}
58+
59+
@Option({
60+
flags: `--log-level <${levels.join('|')}>`,
61+
description: 'log level to use',
62+
defaultValue: LOG_LEVEL.toLowerCase(),
63+
})
64+
parseLogLevel(val: string): LogLevel {
65+
return parseLogLevelOption(val);
66+
}
4367
}

api/src/unraid-api/cli/start.command.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { Command, CommandRunner, Option } from 'nest-commander';
22

33
import type { LogLevel } from '@app/core/log.js';
4+
import type { LogLevelOptions } from '@app/unraid-api/cli/restart.command.js';
45
import { levels } from '@app/core/log.js';
5-
import { ECOSYSTEM_PATH } from '@app/environment.js';
6+
import { ECOSYSTEM_PATH, LOG_LEVEL } from '@app/environment.js';
67
import { LogService } from '@app/unraid-api/cli/log.service.js';
78
import { PM2Service } from '@app/unraid-api/cli/pm2.service.js';
8-
9-
interface StartCommandOptions {
10-
'log-level'?: string;
11-
}
9+
import { parseLogLevelOption } from '@app/unraid-api/cli/restart.command.js';
1210

1311
@Command({ name: 'start', description: 'Start the Unraid API' })
1412
export class StartCommand extends CommandRunner {
@@ -27,17 +25,12 @@ export class StartCommand extends CommandRunner {
2725
await this.pm2.run({ tag: 'PM2 Delete' }, 'delete', ECOSYSTEM_PATH);
2826
}
2927

30-
async run(_: string[], options: StartCommandOptions): Promise<void> {
28+
async run(_: string[], options: LogLevelOptions): Promise<void> {
3129
this.logger.info('Starting the Unraid API');
3230
await this.cleanupPM2State();
33-
34-
const env: Record<string, string> = {};
35-
if (options['log-level']) {
36-
env.LOG_LEVEL = options['log-level'];
37-
}
38-
31+
const env = { LOG_LEVEL: options.logLevel };
3932
const { stderr, stdout } = await this.pm2.run(
40-
{ tag: 'PM2 Start', env, raw: true },
33+
{ tag: 'PM2 Start', raw: true, extendEnv: true, env },
4134
'start',
4235
ECOSYSTEM_PATH,
4336
'--update-env'
@@ -54,9 +47,9 @@ export class StartCommand extends CommandRunner {
5447
@Option({
5548
flags: `--log-level <${levels.join('|')}>`,
5649
description: 'log level to use',
57-
defaultValue: 'info',
50+
defaultValue: LOG_LEVEL.toLowerCase(),
5851
})
5952
parseLogLevel(val: string): LogLevel {
60-
return levels.includes(val as LogLevel) ? (val as LogLevel) : 'info';
53+
return parseLogLevelOption(val);
6154
}
6255
}

0 commit comments

Comments
 (0)