Skip to content

Commit 66801e8

Browse files
committed
fix: Improper Handling of Optional Log Config and set log level issue
1 parent b5b7a87 commit 66801e8

File tree

13 files changed

+519
-187
lines changed

13 files changed

+519
-187
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/contentstack-config/README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $ npm install -g @contentstack/cli-config
1818
$ csdx COMMAND
1919
running command...
2020
$ csdx (--version)
21-
@contentstack/cli-config/1.15.1 darwin-arm64 node-v22.14.0
21+
@contentstack/cli-config/1.15.2 darwin-arm64 node-v22.14.0
2222
$ csdx --help [COMMAND]
2323
USAGE
2424
$ csdx COMMAND
@@ -329,9 +329,10 @@ USAGE
329329
$ csdx config:set:log [--level debug|info|warn|error] [--path <value>] [--show-console-logs]
330330
331331
FLAGS
332-
--level=<option> Set the log level for the CLI.
332+
--level=<option> Set the log level for the CLI. Defaults to "info" if not specified.
333333
<options: debug|info|warn|error>
334-
--path=<value> Specify the file path where logs should be saved.
334+
--path=<value> Specify the directory path where logs should be saved. Supports both relative and absolute
335+
paths. Defaults to ~/.contentstack/logs if not specified.
335336
--[no-]show-console-logs Enable console logging.
336337
337338
DESCRIPTION
@@ -340,9 +341,19 @@ DESCRIPTION
340341
EXAMPLES
341342
$ csdx config:set:log
342343
343-
$ csdx config:set:log --level debug --path ./logs/app.log --show-console-logs
344+
$ csdx config:set:log --level debug
345+
346+
$ csdx config:set:log --path ./logs
347+
348+
$ csdx config:set:log --level debug --path ./logs --show-console-logs
344349
345350
$ csdx config:set:log --no-show-console-logs
351+
352+
$ csdx config:set:log --level warn --show-console-logs
353+
354+
$ csdx config:set:log --path ~/custom/logs
355+
356+
$ csdx config:set:log --path /var/log/contentstack
346357
```
347358

348359
_See code: [src/commands/config/set/log.ts](https://github.com/contentstack/cli/blob/main/packages/contentstack-config/src/commands/config/set/log.ts)_

packages/contentstack-config/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@contentstack/cli-config",
33
"description": "Contentstack CLI plugin for configuration",
4-
"version": "1.15.1",
4+
"version": "1.15.2",
55
"author": "Contentstack",
66
"scripts": {
77
"build": "npm run clean && npm run compile",

packages/contentstack-config/src/commands/config/get/log.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Command } from '@contentstack/cli-command';
2-
import { cliux, configHandler, messageHandler, TableHeader } from '@contentstack/cli-utilities';
2+
import { cliux, configHandler, TableHeader } from '@contentstack/cli-utilities';
3+
import { getEffectiveLogConfig } from '../../../utils/log-config-defaults';
34

45
export default class LogGetCommand extends Command {
56
static description = 'Get logging configuration for CLI';
@@ -9,26 +10,29 @@ export default class LogGetCommand extends Command {
910
async run() {
1011
try {
1112
const currentLoggingConfig = configHandler.get('log') || {};
12-
const logLevel = currentLoggingConfig?.level;
13-
const logPath = currentLoggingConfig?.path;
13+
const effectiveConfig = getEffectiveLogConfig(currentLoggingConfig);
1414

15-
if (logLevel || logPath) {
16-
const logConfigList = [
17-
{
18-
'Log Level': logLevel || 'Not set',
19-
'Log Path': logPath || 'Not set',
20-
},
21-
];
15+
const logConfigList = [
16+
{
17+
Setting: 'Log Level',
18+
Value: effectiveConfig.level,
19+
},
20+
{
21+
Setting: 'Log Path',
22+
Value: effectiveConfig.path,
23+
},
24+
{
25+
Setting: 'Show Console Logs',
26+
Value: effectiveConfig.showConsoleLogs.toString(),
27+
},
28+
];
2229

23-
const headers: TableHeader[] = [
24-
{ value: 'Log Level' },
25-
{ value: 'Log Path' },
26-
];
30+
const headers: TableHeader[] = [{ value: 'Setting' }, { value: 'Value' }];
2731

28-
cliux.table(headers, logConfigList);
29-
} else {
30-
cliux.print(`error: ${messageHandler.parse('CLI_CONFIG_LOG_NO_CONFIG')}`, { color: 'red' });
31-
}
32+
cliux.table(headers, logConfigList);
33+
cliux.print('\nNote: Absolute paths are displayed. Relative paths are resolved from current working directory.', {
34+
color: 'dim',
35+
});
3236
} catch (error) {
3337
cliux.error('error', error);
3438
}

packages/contentstack-config/src/commands/config/set/log.ts

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,75 @@
11
import { Command } from '@contentstack/cli-command';
22
import { cliux, flags, configHandler, FlagInput, messageHandler } from '@contentstack/cli-utilities';
3-
import { interactive } from '../../../utils';
3+
import { resolveLogPath } from '../../../utils/log-config-defaults';
4+
import * as path from 'path';
45

56
export default class LogSetCommand extends Command {
67
static description = 'Set logging configuration for CLI';
78

89
static flags: FlagInput = {
9-
'level': flags.string({
10-
description: 'Set the log level for the CLI.',
10+
level: flags.string({
11+
description: 'Set the log level for the CLI. Defaults to "info" if not specified.',
1112
options: ['debug', 'info', 'warn', 'error'],
1213
}),
13-
'path': flags.string({
14-
description: 'Specify the file path where logs should be saved.',
14+
path: flags.string({
15+
description:
16+
'Specify the directory path where logs should be saved. Supports both relative and absolute paths. Defaults to ~/.contentstack/logs if not specified.',
1517
}),
1618
'show-console-logs': flags.boolean({
1719
description: 'Enable console logging.',
1820
allowNo: true, // no-show-console-logs
1921
default: false,
20-
})
22+
}),
2123
};
2224

23-
2425
static examples = [
2526
'csdx config:set:log',
26-
'csdx config:set:log --level debug --path ./logs/app.log --show-console-logs',
27+
'csdx config:set:log --level debug',
28+
'csdx config:set:log --path ./logs',
29+
'csdx config:set:log --level debug --path ./logs --show-console-logs',
2730
'csdx config:set:log --no-show-console-logs',
31+
'csdx config:set:log --level warn --show-console-logs',
32+
'csdx config:set:log --path ~/custom/logs',
33+
'csdx config:set:log --path /var/log/contentstack',
2834
];
2935

3036
async run() {
3137
try {
3238
const { flags } = await this.parse(LogSetCommand);
39+
const currentLoggingConfig = configHandler.get('log') || {};
40+
if (flags['level'] !== undefined) {
41+
currentLoggingConfig.level = flags['level'];
42+
}
3343

34-
let logLevel: string = flags['level'];
35-
let logPath: string = flags['path'];
36-
const showConsoleLogs: boolean = flags['show-console-logs'];
37-
38-
// Interactive prompts if not passed via flags
39-
if (!logLevel) {
40-
logLevel = await interactive.askLogLevel();
44+
if (flags['path'] !== undefined) {
45+
// Convert to absolute path and ensure it's a directory
46+
let resolvedPath = resolveLogPath(flags['path']);
47+
const pathExt = path.extname(resolvedPath);
48+
if (pathExt && pathExt.length > 0) {
49+
resolvedPath = path.dirname(resolvedPath);
50+
}
51+
52+
currentLoggingConfig.path = resolvedPath;
4153
}
4254

43-
if (!logPath) {
44-
logPath = await interactive.askLogPath();
55+
if (flags['show-console-logs'] !== undefined) {
56+
currentLoggingConfig['show-console-logs'] = flags['show-console-logs'];
4557
}
58+
configHandler.set('log', currentLoggingConfig);
4659

47-
const currentLoggingConfig = configHandler.get('log') || {};
48-
if (logLevel) currentLoggingConfig.level = logLevel;
49-
if (logPath) currentLoggingConfig.path = logPath;
50-
currentLoggingConfig['show-console-logs'] = showConsoleLogs;
60+
if (flags['level'] !== undefined) {
61+
cliux.success(messageHandler.parse('CLI_CONFIG_LOG_LEVEL_SET', currentLoggingConfig.level));
62+
}
5163

52-
configHandler.set('log', currentLoggingConfig);
64+
if (flags['path'] !== undefined) {
65+
cliux.success(messageHandler.parse('CLI_CONFIG_LOG_PATH_SET', currentLoggingConfig.path));
66+
}
5367

54-
cliux.success(messageHandler.parse('CLI_CONFIG_LOG_LEVEL_SET', logLevel));
55-
cliux.success(messageHandler.parse('CLI_CONFIG_LOG_PATH_SET', logPath));
56-
cliux.success(messageHandler.parse('CLI_CONFIG_LOG_CONSOLE_SET', String(showConsoleLogs)));
57-
cliux.print(messageHandler.parse('CLI_CONFIG_LOG_SET_SUCCESS'), { color: 'green' });
68+
if (flags['show-console-logs'] !== undefined) {
69+
cliux.success(
70+
messageHandler.parse('CLI_CONFIG_LOG_CONSOLE_SET', String(currentLoggingConfig['show-console-logs'])),
71+
);
72+
}
5873
} catch (error) {
5974
cliux.error('error', error);
6075
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as path from 'path';
2+
import * as os from 'os';
3+
4+
export const LOG_CONFIG_DEFAULTS = {
5+
LEVEL: 'info',
6+
PATH: path.join(os.homedir(), '.contentstack', 'logs'),
7+
SHOW_CONSOLE_LOGS: false,
8+
} as const;
9+
10+
/**
11+
* Resolves a log path to an absolute path
12+
* @param logPath - The path to resolve
13+
* @returns Absolute path
14+
*/
15+
export function resolveLogPath(logPath: string): string {
16+
if (!logPath) return LOG_CONFIG_DEFAULTS.PATH;
17+
18+
return path.isAbsolute(logPath) ? logPath : path.resolve(process.cwd(), logPath);
19+
}
20+
21+
/**
22+
* Gets the effective log configuration with defaults applied
23+
* @param currentConfig - Current configuration from config handler
24+
* @returns Configuration with defaults applied
25+
*/
26+
export function getEffectiveLogConfig(currentConfig: any = {}) {
27+
const logLevel = currentConfig?.level || LOG_CONFIG_DEFAULTS.LEVEL;
28+
const logPath = resolveLogPath(currentConfig?.path || LOG_CONFIG_DEFAULTS.PATH);
29+
const showConsoleLogs = currentConfig?.['show-console-logs'] ?? LOG_CONFIG_DEFAULTS.SHOW_CONSOLE_LOGS;
30+
31+
return {
32+
level: logLevel,
33+
path: logPath,
34+
showConsoleLogs,
35+
};
36+
}

0 commit comments

Comments
 (0)