-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathlog.ts
More file actions
77 lines (67 loc) · 2.74 KB
/
log.ts
File metadata and controls
77 lines (67 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { Command } from '@contentstack/cli-command';
import { cliux, flags, configHandler, FlagInput, messageHandler } from '@contentstack/cli-utilities';
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. Defaults to "info" if not specified.',
options: ['debug', 'info', 'warn', 'error'],
}),
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',
'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'];
}
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 (flags['show-console-logs'] !== undefined) {
currentLoggingConfig['show-console-logs'] = flags['show-console-logs'];
}
configHandler.set('log', currentLoggingConfig);
if (flags['level'] !== undefined) {
cliux.success(messageHandler.parse('CLI_CONFIG_LOG_LEVEL_SET', currentLoggingConfig.level));
}
if (flags['path'] !== undefined) {
cliux.success(messageHandler.parse('CLI_CONFIG_LOG_PATH_SET', currentLoggingConfig.path));
}
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);
}
}
}