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
4 changes: 2 additions & 2 deletions .talismanrc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
fileignoreconfig:
- filename: package-lock.json
checksum: b7a69ac42575d552fa99e0f7b62fa16485cdb8bb83efec3c5d46c22537e8bc37
checksum: 64a89b20371afdaaf3e901f82d7d82e342d331015efba222a32032a58063780b
- filename: pnpm-lock.yaml
checksum: 62fc7744149464cc3a0b285544e90739d48e8e1384ac211d4799389c3c78db10
checksum: d9ad2eb48406e93e01b9db255da99bed98e29eb6db1fb5821b50f7e60b6f3efa
- filename: packages/contentstack-import-setup/test/unit/backup-handler.test.ts
checksum: 0582d62b88834554cf12951c8690a73ef3ddbb78b82d2804d994cf4148e1ef93
- filename: packages/contentstack-import-setup/test/config.json
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions packages/contentstack-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $ npm install -g @contentstack/cli-config
$ csdx COMMAND
running command...
$ csdx (--version)
@contentstack/cli-config/1.15.1 darwin-arm64 node-v22.14.0
@contentstack/cli-config/1.15.2 darwin-arm64 node-v22.14.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
Expand Down Expand Up @@ -329,9 +329,10 @@ USAGE
$ csdx config:set:log [--level debug|info|warn|error] [--path <value>] [--show-console-logs]

FLAGS
--level=<option> Set the log level for the CLI.
--level=<option> Set the log level for the CLI. Defaults to "info" if not specified.
<options: debug|info|warn|error>
--path=<value> Specify the file path where logs should be saved.
--path=<value> Specify the directory path where logs should be saved. Supports both relative and absolute
paths. Defaults to ~/.contentstack/logs if not specified.
--[no-]show-console-logs Enable console logging.

DESCRIPTION
Expand All @@ -340,9 +341,19 @@ DESCRIPTION
EXAMPLES
$ csdx config:set:log

$ csdx config:set:log --level debug --path ./logs/app.log --show-console-logs
$ csdx config:set:log --level debug

$ csdx config:set:log --path ./logs

$ csdx config:set:log --level debug --path ./logs --show-console-logs

$ csdx config:set:log --no-show-console-logs

$ csdx config:set:log --level warn --show-console-logs

$ csdx config:set:log --path ~/custom/logs

$ csdx config:set:log --path /var/log/contentstack
```

_See code: [src/commands/config/set/log.ts](https://github.com/contentstack/cli/blob/main/packages/contentstack-config/src/commands/config/set/log.ts)_
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-config/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@contentstack/cli-config",
"description": "Contentstack CLI plugin for configuration",
"version": "1.15.1",
"version": "1.15.2",
"author": "Contentstack",
"scripts": {
"build": "npm run clean && npm run compile",
Expand Down
40 changes: 22 additions & 18 deletions packages/contentstack-config/src/commands/config/get/log.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command } from '@contentstack/cli-command';
import { cliux, configHandler, messageHandler, TableHeader } from '@contentstack/cli-utilities';
import { cliux, configHandler, TableHeader } from '@contentstack/cli-utilities';
import { getEffectiveLogConfig } from '../../../utils/log-config-defaults';

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

if (logLevel || logPath) {
const logConfigList = [
{
'Log Level': logLevel || 'Not set',
'Log Path': logPath || 'Not set',
},
];
const logConfigList = [
{
Setting: 'Log Level',
Value: effectiveConfig.level,
},
{
Setting: 'Log Path',
Value: effectiveConfig.path,
},
{
Setting: 'Show Console Logs',
Value: effectiveConfig.showConsoleLogs.toString(),
},
];

const headers: TableHeader[] = [
{ value: 'Log Level' },
{ value: 'Log Path' },
];
const headers: TableHeader[] = [{ value: 'Setting' }, { value: 'Value' }];

cliux.table(headers, logConfigList);
} else {
cliux.print(`error: ${messageHandler.parse('CLI_CONFIG_LOG_NO_CONFIG')}`, { color: 'red' });
}
cliux.table(headers, logConfigList);
cliux.print('\nNote: Absolute paths are displayed. Relative paths are resolved from current working directory.', {
color: 'dim',
});
} catch (error) {
cliux.error('error', error);
}
Expand Down
67 changes: 41 additions & 26 deletions packages/contentstack-config/src/commands/config/set/log.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,75 @@
import { Command } from '@contentstack/cli-command';
import { cliux, flags, configHandler, FlagInput, messageHandler } from '@contentstack/cli-utilities';
import { interactive } from '../../../utils';
import { resolveLogPath } from '../../../utils/log-config-defaults';
import * as path from 'path';

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

static flags: FlagInput = {
'level': flags.string({
description: 'Set the log level for the CLI.',
level: flags.string({
description: 'Set the log level for the CLI. Defaults to "info" if not specified.',
options: ['debug', 'info', 'warn', 'error'],
}),
'path': flags.string({
description: 'Specify the file path where logs should be saved.',
path: flags.string({
description:
'Specify the directory path where logs should be saved. Supports both relative and absolute paths. Defaults to ~/.contentstack/logs if not specified.',
}),
'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 --show-console-logs',
'csdx config:set:log --level debug',
'csdx config:set:log --path ./logs',
'csdx config:set:log --level debug --path ./logs --show-console-logs',
'csdx config:set:log --no-show-console-logs',
'csdx config:set:log --level warn --show-console-logs',
'csdx config:set:log --path ~/custom/logs',
'csdx config:set:log --path /var/log/contentstack',
];

async run() {
try {
const { flags } = await this.parse(LogSetCommand);
const currentLoggingConfig = configHandler.get('log') || {};
if (flags['level'] !== undefined) {
currentLoggingConfig.level = flags['level'];
}

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) {
logLevel = await interactive.askLogLevel();
if (flags['path'] !== undefined) {
// Convert to absolute path and ensure it's a directory
let resolvedPath = resolveLogPath(flags['path']);
const pathExt = path.extname(resolvedPath);
if (pathExt && pathExt.length > 0) {
resolvedPath = path.dirname(resolvedPath);
}

currentLoggingConfig.path = resolvedPath;
}

if (!logPath) {
logPath = await interactive.askLogPath();
if (flags['show-console-logs'] !== undefined) {
currentLoggingConfig['show-console-logs'] = flags['show-console-logs'];
}
configHandler.set('log', currentLoggingConfig);

const currentLoggingConfig = configHandler.get('log') || {};
if (logLevel) currentLoggingConfig.level = logLevel;
if (logPath) currentLoggingConfig.path = logPath;
currentLoggingConfig['show-console-logs'] = showConsoleLogs;
if (flags['level'] !== undefined) {
cliux.success(messageHandler.parse('CLI_CONFIG_LOG_LEVEL_SET', currentLoggingConfig.level));
}

configHandler.set('log', currentLoggingConfig);
if (flags['path'] !== undefined) {
cliux.success(messageHandler.parse('CLI_CONFIG_LOG_PATH_SET', currentLoggingConfig.path));
}

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' });
if (flags['show-console-logs'] !== undefined) {
cliux.success(
messageHandler.parse('CLI_CONFIG_LOG_CONSOLE_SET', String(currentLoggingConfig['show-console-logs'])),
);
}
} catch (error) {
cliux.error('error', error);
}
Expand Down
36 changes: 36 additions & 0 deletions packages/contentstack-config/src/utils/log-config-defaults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as path from 'path';
import * as os from 'os';

export const LOG_CONFIG_DEFAULTS = {
LEVEL: 'info',
PATH: path.join(os.homedir(), '.contentstack', 'logs'),
SHOW_CONSOLE_LOGS: false,
} as const;

/**
* Resolves a log path to an absolute path
* @param logPath - The path to resolve
* @returns Absolute path
*/
export function resolveLogPath(logPath: string): string {
if (!logPath) return LOG_CONFIG_DEFAULTS.PATH;

return path.isAbsolute(logPath) ? logPath : path.resolve(process.cwd(), logPath);
}

/**
* Gets the effective log configuration with defaults applied
* @param currentConfig - Current configuration from config handler
* @returns Configuration with defaults applied
*/
export function getEffectiveLogConfig(currentConfig: any = {}) {
const logLevel = currentConfig?.level || LOG_CONFIG_DEFAULTS.LEVEL;
const logPath = resolveLogPath(currentConfig?.path || LOG_CONFIG_DEFAULTS.PATH);
const showConsoleLogs = currentConfig?.['show-console-logs'] ?? LOG_CONFIG_DEFAULTS.SHOW_CONSOLE_LOGS;

return {
level: logLevel,
path: logPath,
showConsoleLogs,
};
}
Loading
Loading