Skip to content

feat: added support of show-console-logs flag in config:set:log command #2021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 25, 2025
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
9 changes: 5 additions & 4 deletions packages/contentstack-config/messages/index.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"CLI_CONFIG_SET_REGION_DESCRIPTION": "Set region for CLI",
"CLI_CONFIG_SET_REGION_FLAG_D_DESCRIPTION": "Custom host to set for content delivery API, if this flag is added then cma and name flags are required",
"CLI_CONFIG_SET_REGION_FLAG_M_DESCRIPTION": "Custom host to set for content management API, if this flag is added then cda and name flags are required",
"CLI_CONFIG_SET_REGION_FLAG_M_DESCRIPTION": "Custom host to set for content management API, , if this flag is added then cda and name flags are required",
"CLI_CONFIG_SET_REGION_FLAG_N_DESCRIPTION": "Name for the region, if this flag is added then cda and cma flags are required",
"CLI_CONFIG_SET_REGION_DEFAULT": "No argument or custom flag provided. Setting region to default AWS-NA",
"CLI_CONFIG_SET_REGION_DEFAULT": "No argument or custom flag provided. Setting region to default NA",
"CLI_CONFIG_GET_REGION_DESCRIPTION": "Get current region set for CLI",
"CLI_CONFIG_GET_REGION_NOT_FOUND": "No region found, please set by running command $ csdx config:set:region",
"CLI_CONFIG_INQUIRE_REGION_NAME": "Enter custom region name",
Expand All @@ -19,5 +19,6 @@
"CLI_CONFIG_LOG_NO_CONFIG": "No config found, please set by running command $ csdx config:set:log",
"CLI_CONFIG_LOG_SET_SUCCESS": "Log config set successfully",
"CLI_CONFIG_LOG_LEVEL_SET": "Log level: '%s'",
"CLI_CONFIG_LOG_PATH_SET": "Log path: '%s'"
}
"CLI_CONFIG_LOG_PATH_SET": "Log path: '%s'",
"CLI_CONFIG_LOG_CONSOLE_SET": "Log console: '%s'"
}
36 changes: 19 additions & 17 deletions packages/contentstack-config/src/commands/config/set/log.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
import { Command } from '@contentstack/cli-command';
import { cliux, flags, configHandler, FlagInput, messageHandler, CLIError } from '@contentstack/cli-utilities';
import { cliux, flags, configHandler, FlagInput, messageHandler } from '@contentstack/cli-utilities';
import { interactive } from '../../../utils';
import { existsSync, statSync } from 'fs';
import { dirname } from 'path';

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

static flags: FlagInput = {
level: flags.string({
'level': flags.string({
description: 'Set the log level for the CLI.',
options: ['debug', 'info', 'warn', 'error'],
}),
path: flags.string({
description: ' Specify the file path where logs should be saved.',
'path': flags.string({
description: 'Specify the file path where logs should be saved.',
}),
'show-console-logs': flags.boolean({
description: 'Enable console logging.',
allowNo: true, // no-show-console-logs
default: false,
})
};

static examples = ['csdx config:set:log', 'csdx config:set:log --level debug --path ./logs/app.log'];

static examples = [
'csdx config:set:log',
'csdx config:set:log --level debug --path ./logs/app.log --show-console-logs',
'csdx config:set:log --no-show-console-logs',
];

async run() {
try {
const { flags } = await this.parse(LogSetCommand);

let logLevel: string = flags['level'];
let logPath: string = flags['path'];
const showConsoleLogs: boolean = flags['show-console-logs'];

// Interactive prompts if not passed via flags
if (!logLevel) {
Expand All @@ -35,23 +44,16 @@ export default class LogSetCommand extends Command {
logPath = await interactive.askLogPath();
}

if (logPath) {
const logDir = dirname(logPath);
// Check if the directory part of the path exists and is actually a file
if (existsSync(logDir) && statSync(logDir).isFile()) {
throw new CLIError({
message: `The directory path '${logDir}' is a file, not a directory. Please provide a valid directory path for the log file.`,
});
}
}

const currentLoggingConfig = configHandler.get('log') || {};
if (logLevel) currentLoggingConfig.level = logLevel;
if (logPath) currentLoggingConfig.path = logPath;
currentLoggingConfig['show-console-logs'] = showConsoleLogs;

configHandler.set('log', currentLoggingConfig);

cliux.success(messageHandler.parse('CLI_CONFIG_LOG_LEVEL_SET', logLevel));
cliux.success(messageHandler.parse('CLI_CONFIG_LOG_PATH_SET', logPath));
cliux.success(messageHandler.parse('CLI_CONFIG_LOG_CONSOLE_SET', String(showConsoleLogs)));
cliux.print(messageHandler.parse('CLI_CONFIG_LOG_SET_SUCCESS'), { color: 'green' });
} catch (error) {
cliux.error('error', error);
Expand Down
Loading
Loading