Skip to content
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

feat: support custom log path #1024

Merged
merged 3 commits into from
Aug 30, 2024
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
1 change: 1 addition & 0 deletions cortex-js/src/domain/config/config.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface Config {
// todo: will remove optional when all command request api server
apiServerPort?: number;
apiServerHost?: string;
logPath?: string;
}
9 changes: 7 additions & 2 deletions cortex-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export async function start(
portNumber?: number,
enginePortNumber?: number,
dataFolder?: string,
logPath?: string,
) {
if (logPath) {
fileManagerService.setLogPath(logPath);
}
if (name) {
fileManagerService.setConfigProfile(name);
const isProfileConfigExists = fileManagerService.profileConfigExists(name);
Expand Down Expand Up @@ -49,10 +53,10 @@ export async function start(
Number(enginePortNumber) || configCortexCppPort || defaultCortexCppPort;
const dataFolderPath = dataFolder;

return startServer(dataFolderPath);
return startServer(dataFolderPath, logPath);
}

async function startServer(dataFolderPath?: string) {
async function startServer(dataFolderPath?: string, logPath?: string) {
const config = await fileManagerService.getConfig();
try {
if (dataFolderPath) {
Expand Down Expand Up @@ -80,6 +84,7 @@ async function startServer(dataFolderPath?: string) {
apiServerHost: host,
apiServerPort: port,
dataFolderPath: dataFolderPath || config.dataFolderPath,
logPath: logPath || config.logPath,
cortexCppPort: enginePort,
});
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type ServeOptions = {
name?: string;
configPath?: string;
enginePort?: string;
logPath?: string;
};

@RootCommand({
Expand Down Expand Up @@ -74,6 +75,9 @@ export class CortexCommand extends CommandRunner {
if (options?.name) {
fileManagerService.setConfigProfile(options.name);
}
if (options?.logPath) {
fileManagerService.setLogPath(options.logPath);
}
if (options?.name) {
const isProfileConfigExists = fileManagerService.profileConfigExists(
options.name,
Expand Down Expand Up @@ -114,10 +118,14 @@ export class CortexCommand extends CommandRunner {
console.log(chalk.blue(`Github: ${pkg.homepage}`));
return;
}
return this.startServer(showLogs, dataFolderPath);
return this.startServer(showLogs, dataFolderPath, options?.logPath);
}

private async startServer(attach: boolean, dataFolderPath?: string) {
private async startServer(
attach: boolean,
dataFolderPath?: string,
logPath?: string,
) {
const config = await fileManagerService.getConfig();
try {
const startEngineSpinner = ora('Starting Cortex engine...');
Expand Down Expand Up @@ -155,11 +163,13 @@ export class CortexCommand extends CommandRunner {
`API Playground available at http://${this.host}:${this.port}/api`,
),
);

await fileManagerService.writeConfigFile({
...config,
apiServerHost: this.host,
apiServerPort: this.port,
dataFolderPath: dataFolderPath || config.dataFolderPath,
logPath: logPath || config.logPath,
cortexCppPort: this.enginePort,
});
if (!attach) process.exit(0);
Expand Down Expand Up @@ -237,4 +247,12 @@ export class CortexCommand extends CommandRunner {
parseEnginePort(value: string) {
return value;
}

@Option({
flags: '-lp, --logPath <logPath>',
description: 'Path to the logs folder',
})
parseLogPath(value: string) {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ export class FileManagerService {
private benchmarkFoldername = 'benchmark';
private cortexEnginesFolderName = 'engines';
private cortexTelemetryFolderName = 'telemetry';
private logFileName = 'cortex.log';
private configProfile = process.env.CORTEX_PROFILE || 'default';
private configPath = process.env.CORTEX_CONFIG_PATH || os.homedir();

private customLogPath = process.env.CORTEX_LOG_PATH || '';
/**
* Get cortex configs
* @returns the config object
Expand Down Expand Up @@ -309,7 +310,10 @@ export class FileManagerService {
* @returns the path to the cortex engines folder
*/
async getLogPath(): Promise<string> {
return join(await this.getDataFolderPath(), 'cortex.log');
if (this.customLogPath) {
return this.customLogPath + `/${this.logFileName}`;
}
return join(await this.getDataFolderPath(), this.logFileName);
}

async createFolderIfNotExistInDataFolder(folderName: string): Promise<void> {
Expand Down Expand Up @@ -401,6 +405,10 @@ export class FileManagerService {
public getConfigPath(): string {
return this.configPath;
}

public setLogPath(logPath: string) {
this.customLogPath = logPath;
}
}

export const fileManagerService = new FileManagerService();
Loading