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

Commit bc9544a

Browse files
chore: support loglevel when start server (#1636)
* chore: support loglevel when start server * Fix comment and set logger for engine * Update docs
1 parent 086de70 commit bc9544a

File tree

11 files changed

+68
-10
lines changed

11 files changed

+68
-10
lines changed

docs/docs/cli/start.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ You can use the `--verbose` flag to display more detailed output of the internal
3434
| Option | Description | Required | Default value | Example |
3535
| ---------------------------- | ----------------------------------------- | -------- | ------------- | ----------------------------- |
3636
| `-h`, `--help` | Display help information for the command. | No | - | `-h` |
37-
| `-p`, `--port <port>` | Port to serve the application. | No | `39281` | `-p 39281` |
37+
| `-p`, `--port <port>` | Port to serve the application. | No | - | `-p 39281` |
38+
| `--loglevel <loglevel>` | Setup loglevel for cortex server, supported levels are TRACE, DEBUG, INFO, WARN, ERROR | No | - | `--loglevel DEBUG` |
39+
3840
<!-- | `-a`, `--address <address>` | Address to use. | No | - | `-a 192.168.1.1` | -->
3941
<!--| `--dataFolder <dataFolder>` | Set the data folder directory | No | - | `--dataFolder /path/to/data` | -->

engine/cli/command_line_parser.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,16 @@ void CommandLineParser::SetupSystemCommands() {
468468
start_cmd->group(kSystemGroup);
469469
cml_data_.port = std::stoi(cml_data_.config.apiServerPort);
470470
start_cmd->add_option("-p, --port", cml_data_.port, "Server port to listen");
471+
start_cmd->add_option("--loglevel", cml_data_.log_level,
472+
"Set up log level for server, accepted TRACE, DEBUG, "
473+
"INFO, WARN, ERROR");
474+
if (cml_data_.log_level != "INFO" && cml_data_.log_level != "TRACE" &&
475+
cml_data_.log_level != "DEBUG" && cml_data_.log_level != "WARN" &&
476+
cml_data_.log_level != "ERROR") {
477+
CLI_LOG("Invalid log level: " << cml_data_.log_level
478+
<< ", Set Loglevel to INFO");
479+
cml_data_.log_level = "INFO";
480+
}
471481
start_cmd->callback([this] {
472482
if (std::exchange(executed_, true))
473483
return;
@@ -484,7 +494,7 @@ void CommandLineParser::SetupSystemCommands() {
484494
}
485495
commands::ServerStartCmd ssc;
486496
ssc.Exec(cml_data_.config.apiServerHost,
487-
std::stoi(cml_data_.config.apiServerPort));
497+
std::stoi(cml_data_.config.apiServerPort), cml_data_.log_level);
488498
});
489499

490500
auto stop_cmd = app_.add_subcommand("stop", "Stop the API server");

engine/cli/command_line_parser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ class CommandLineParser {
5858
bool display_engine = false;
5959
bool display_version = false;
6060
std::string filter = "";
61+
std::string log_level = "INFO";
62+
6163
bool show_menu = false;
6264

65+
6366
int port;
6467
config_yaml_utils::CortexConfig config;
6568
std::unordered_map<std::string, std::string> model_update_options;

engine/cli/commands/server_start_cmd.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,14 @@ bool TryConnectToServer(const std::string& host, int port) {
2626

2727
ServerStartCmd::ServerStartCmd() {}
2828

29-
bool ServerStartCmd::Exec(const std::string& host, int port) {
29+
bool ServerStartCmd::Exec(const std::string& host, int port,
30+
const std::optional<std::string>& log_level) {
31+
std::string log_level_;
32+
if (!log_level.has_value()) {
33+
log_level_ = "INFO";
34+
} else {
35+
log_level_ = log_level.value();
36+
}
3037
auto exe = commands::GetCortexServerBinary();
3138
auto get_config_file_path = []() -> std::string {
3239
if (file_manager_utils::cortex_config_file_path.empty()) {
@@ -53,6 +60,7 @@ bool ServerStartCmd::Exec(const std::string& host, int port) {
5360
std::string params = "--start-server";
5461
params += " --config_file_path " + get_config_file_path();
5562
params += " --data_folder_path " + get_data_folder_path();
63+
params += " --loglevel " + log_level_;
5664
std::string cmds = cortex_utils::GetCurrentPath() + "/" + exe + " " + params;
5765
// Create child process
5866
if (!CreateProcess(
@@ -107,7 +115,7 @@ bool ServerStartCmd::Exec(const std::string& host, int port) {
107115
std::string p = cortex_utils::GetCurrentPath() + "/" + exe;
108116
execl(p.c_str(), exe.c_str(), "--start-server", "--config_file_path",
109117
get_config_file_path().c_str(), "--data_folder_path",
110-
get_data_folder_path().c_str(), (char*)0);
118+
get_data_folder_path().c_str(), "--loglevel", log_level_.c_str(), (char*)0);
111119
} else {
112120
// Parent process
113121
if (!TryConnectToServer(host, port)) {

engine/cli/commands/server_start_cmd.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <string>
44
#include "httplib.h"
5-
5+
#include <optional>
66
namespace commands {
77

88
inline bool IsServerAlive(const std::string& host, int port) {
@@ -17,6 +17,6 @@ inline bool IsServerAlive(const std::string& host, int port) {
1717
class ServerStartCmd {
1818
public:
1919
ServerStartCmd();
20-
bool Exec(const std::string& host, int port);
20+
bool Exec(const std::string& host, int port, const std::optional<std::string>& log_level = std::nullopt);
2121
};
2222
} // namespace commands

engine/controllers/server.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ void server::ChatCompletion(
2727
LOG_DEBUG << "Start chat completion";
2828
auto json_body = req->getJsonObject();
2929
bool is_stream = (*json_body).get("stream", false).asBool();
30+
LOG_DEBUG << "request body: " << json_body->toStyledString();
3031
auto q = std::make_shared<services::SyncQueue>();
3132
auto ir = inference_svc_->HandleChatCompletion(q, json_body);
3233
if (ir.has_error()) {
@@ -141,7 +142,7 @@ void server::ProcessStreamRes(std::function<void(const HttpResponsePtr&)> cb,
141142
}
142143

143144
auto str = res["data"].asString();
144-
LOG_TRACE << "data: " << str;
145+
LOG_DEBUG << "data: " << str;
145146
std::size_t n = std::min(str.size(), buf_size);
146147
memcpy(buf, str.data(), n);
147148

@@ -157,6 +158,7 @@ void server::ProcessNonStreamRes(std::function<void(const HttpResponsePtr&)> cb,
157158
services::SyncQueue& q) {
158159
auto [status, res] = q.wait_and_pop();
159160
function_calling_utils::PostProcessResponse(res);
161+
LOG_DEBUG << "response: " << res.toStyledString();
160162
auto resp = cortex_utils::CreateCortexHttpJsonResponse(res);
161163
resp->setStatusCode(
162164
static_cast<drogon::HttpStatusCode>(status["status_code"].asInt()));

engine/cortex-common/EngineI.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <memory>
55

66
#include "json/value.h"
7-
7+
#include "trantor/utils/Logger.h"
88
class EngineI {
99
public:
1010
virtual ~EngineI() {}
@@ -36,4 +36,5 @@ class EngineI {
3636

3737
virtual bool SetFileLogger(int max_log_lines,
3838
const std::string& log_path) = 0;
39+
virtual void SetLogLevel(trantor::Logger::LogLevel logLevel) = 0;
3940
};

engine/main.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ void RunServer(std::optional<int> port) {
5757
}
5858
std::cout << "Host: " << config.apiServerHost
5959
<< " Port: " << config.apiServerPort << "\n";
60-
6160
// Create logs/ folder and setup log to file
6261
std::filesystem::create_directories(
6362
std::filesystem::path(config.logFolderPath) /
@@ -181,6 +180,9 @@ int main(int argc, char* argv[]) {
181180
file_manager_utils::cortex_data_folder_path = argv[i + 1];
182181
} else if (strcmp(argv[i], "--port") == 0) {
183182
server_port = std::stoi(argv[i + 1]);
183+
} else if (strcmp(argv[i], "--loglevel") == 0) {
184+
std::string log_level = argv[i + 1];
185+
logging_utils_helper::SetLogLevel(log_level);
184186
}
185187
}
186188

engine/services/engine_service.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,11 @@ cpp::result<void, std::string> EngineService::LoadEngine(
832832
} else {
833833
CTL_WRN("Method SetFileLogger is not supported yet");
834834
}
835+
if (en->IsSupported("SetLogLevel")) {
836+
en->SetLogLevel(trantor::Logger::logLevel());
837+
} else {
838+
CTL_WRN("Method SetLogLevel is not supported yet");
839+
}
835840
}
836841
CTL_DBG("Loaded engine: " << ne);
837842
return {};

engine/services/inference_service.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,4 @@ bool InferenceService::HasFieldInReq(std::shared_ptr<Json::Value> json_body,
240240
}
241241
return true;
242242
}
243-
} // namespace services
243+
} // namespace services

0 commit comments

Comments
 (0)