Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit a5a30c2

Browse files
feat: support custom log path (#1024)
1 parent 670a477 commit a5a30c2

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

cortex-js/src/domain/config/config.interface.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export interface Config {
55
// todo: will remove optional when all command request api server
66
apiServerPort?: number;
77
apiServerHost?: string;
8+
logPath?: string;
89
}

cortex-js/src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ export async function start(
2121
portNumber?: number,
2222
enginePortNumber?: number,
2323
dataFolder?: string,
24+
logPath?: string,
2425
) {
26+
if (logPath) {
27+
fileManagerService.setLogPath(logPath);
28+
}
2529
if (name) {
2630
fileManagerService.setConfigProfile(name);
2731
const isProfileConfigExists = fileManagerService.profileConfigExists(name);
@@ -49,10 +53,10 @@ export async function start(
4953
Number(enginePortNumber) || configCortexCppPort || defaultCortexCppPort;
5054
const dataFolderPath = dataFolder;
5155

52-
return startServer(dataFolderPath);
56+
return startServer(dataFolderPath, logPath);
5357
}
5458

55-
async function startServer(dataFolderPath?: string) {
59+
async function startServer(dataFolderPath?: string, logPath?: string) {
5660
const config = await fileManagerService.getConfig();
5761
try {
5862
if (dataFolderPath) {
@@ -80,6 +84,7 @@ async function startServer(dataFolderPath?: string) {
8084
apiServerHost: host,
8185
apiServerPort: port,
8286
dataFolderPath: dataFolderPath || config.dataFolderPath,
87+
logPath: logPath || config.logPath,
8388
cortexCppPort: enginePort,
8489
});
8590
return app;

cortex-js/src/infrastructure/commanders/cortex-command.commander.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type ServeOptions = {
3434
name?: string;
3535
configPath?: string;
3636
enginePort?: string;
37+
logPath?: string;
3738
};
3839

3940
@RootCommand({
@@ -74,6 +75,9 @@ export class CortexCommand extends CommandRunner {
7475
if (options?.name) {
7576
fileManagerService.setConfigProfile(options.name);
7677
}
78+
if (options?.logPath) {
79+
fileManagerService.setLogPath(options.logPath);
80+
}
7781
if (options?.name) {
7882
const isProfileConfigExists = fileManagerService.profileConfigExists(
7983
options.name,
@@ -114,10 +118,14 @@ export class CortexCommand extends CommandRunner {
114118
console.log(chalk.blue(`Github: ${pkg.homepage}`));
115119
return;
116120
}
117-
return this.startServer(showLogs, dataFolderPath);
121+
return this.startServer(showLogs, dataFolderPath, options?.logPath);
118122
}
119123

120-
private async startServer(attach: boolean, dataFolderPath?: string) {
124+
private async startServer(
125+
attach: boolean,
126+
dataFolderPath?: string,
127+
logPath?: string,
128+
) {
121129
const config = await fileManagerService.getConfig();
122130
try {
123131
const startEngineSpinner = ora('Starting Cortex engine...');
@@ -155,11 +163,13 @@ export class CortexCommand extends CommandRunner {
155163
`API Playground available at http://${this.host}:${this.port}/api`,
156164
),
157165
);
166+
158167
await fileManagerService.writeConfigFile({
159168
...config,
160169
apiServerHost: this.host,
161170
apiServerPort: this.port,
162171
dataFolderPath: dataFolderPath || config.dataFolderPath,
172+
logPath: logPath || config.logPath,
163173
cortexCppPort: this.enginePort,
164174
});
165175
if (!attach) process.exit(0);
@@ -237,4 +247,12 @@ export class CortexCommand extends CommandRunner {
237247
parseEnginePort(value: string) {
238248
return value;
239249
}
250+
251+
@Option({
252+
flags: '-lp, --logPath <logPath>',
253+
description: 'Path to the logs folder',
254+
})
255+
parseLogPath(value: string) {
256+
return value;
257+
}
240258
}

cortex-js/src/infrastructure/services/file-manager/file-manager.service.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ export class FileManagerService {
3535
private benchmarkFoldername = 'benchmark';
3636
private cortexEnginesFolderName = 'engines';
3737
private cortexTelemetryFolderName = 'telemetry';
38+
private logFileName = 'cortex.log';
3839
private configProfile = process.env.CORTEX_PROFILE || 'default';
3940
private configPath = process.env.CORTEX_CONFIG_PATH || os.homedir();
40-
41+
private customLogPath = process.env.CORTEX_LOG_PATH || '';
4142
/**
4243
* Get cortex configs
4344
* @returns the config object
@@ -309,7 +310,10 @@ export class FileManagerService {
309310
* @returns the path to the cortex engines folder
310311
*/
311312
async getLogPath(): Promise<string> {
312-
return join(await this.getDataFolderPath(), 'cortex.log');
313+
if (this.customLogPath) {
314+
return this.customLogPath + `/${this.logFileName}`;
315+
}
316+
return join(await this.getDataFolderPath(), this.logFileName);
313317
}
314318

315319
async createFolderIfNotExistInDataFolder(folderName: string): Promise<void> {
@@ -401,6 +405,10 @@ export class FileManagerService {
401405
public getConfigPath(): string {
402406
return this.configPath;
403407
}
408+
409+
public setLogPath(logPath: string) {
410+
this.customLogPath = logPath;
411+
}
404412
}
405413

406414
export const fileManagerService = new FileManagerService();

0 commit comments

Comments
 (0)