Skip to content

Commit

Permalink
chore: support loglevel when start server (#1636)
Browse files Browse the repository at this point in the history
* chore: support loglevel when start server

* Fix comment and set logger for engine

* Update docs
  • Loading branch information
nguyenhoangthuan99 authored Nov 7, 2024
1 parent 086de70 commit bc9544a
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 10 deletions.
4 changes: 3 additions & 1 deletion docs/docs/cli/start.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ You can use the `--verbose` flag to display more detailed output of the internal
| Option | Description | Required | Default value | Example |
| ---------------------------- | ----------------------------------------- | -------- | ------------- | ----------------------------- |
| `-h`, `--help` | Display help information for the command. | No | - | `-h` |
| `-p`, `--port <port>` | Port to serve the application. | No | `39281` | `-p 39281` |
| `-p`, `--port <port>` | Port to serve the application. | No | - | `-p 39281` |
| `--loglevel <loglevel>` | Setup loglevel for cortex server, supported levels are TRACE, DEBUG, INFO, WARN, ERROR | No | - | `--loglevel DEBUG` |

<!-- | `-a`, `--address <address>` | Address to use. | No | - | `-a 192.168.1.1` | -->
<!--| `--dataFolder <dataFolder>` | Set the data folder directory | No | - | `--dataFolder /path/to/data` | -->
12 changes: 11 additions & 1 deletion engine/cli/command_line_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,16 @@ void CommandLineParser::SetupSystemCommands() {
start_cmd->group(kSystemGroup);
cml_data_.port = std::stoi(cml_data_.config.apiServerPort);
start_cmd->add_option("-p, --port", cml_data_.port, "Server port to listen");
start_cmd->add_option("--loglevel", cml_data_.log_level,
"Set up log level for server, accepted TRACE, DEBUG, "
"INFO, WARN, ERROR");
if (cml_data_.log_level != "INFO" && cml_data_.log_level != "TRACE" &&
cml_data_.log_level != "DEBUG" && cml_data_.log_level != "WARN" &&
cml_data_.log_level != "ERROR") {
CLI_LOG("Invalid log level: " << cml_data_.log_level
<< ", Set Loglevel to INFO");
cml_data_.log_level = "INFO";
}
start_cmd->callback([this] {
if (std::exchange(executed_, true))
return;
Expand All @@ -484,7 +494,7 @@ void CommandLineParser::SetupSystemCommands() {
}
commands::ServerStartCmd ssc;
ssc.Exec(cml_data_.config.apiServerHost,
std::stoi(cml_data_.config.apiServerPort));
std::stoi(cml_data_.config.apiServerPort), cml_data_.log_level);
});

auto stop_cmd = app_.add_subcommand("stop", "Stop the API server");
Expand Down
3 changes: 3 additions & 0 deletions engine/cli/command_line_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ class CommandLineParser {
bool display_engine = false;
bool display_version = false;
std::string filter = "";
std::string log_level = "INFO";

bool show_menu = false;


int port;
config_yaml_utils::CortexConfig config;
std::unordered_map<std::string, std::string> model_update_options;
Expand Down
12 changes: 10 additions & 2 deletions engine/cli/commands/server_start_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ bool TryConnectToServer(const std::string& host, int port) {

ServerStartCmd::ServerStartCmd() {}

bool ServerStartCmd::Exec(const std::string& host, int port) {
bool ServerStartCmd::Exec(const std::string& host, int port,
const std::optional<std::string>& log_level) {
std::string log_level_;
if (!log_level.has_value()) {
log_level_ = "INFO";
} else {
log_level_ = log_level.value();
}
auto exe = commands::GetCortexServerBinary();
auto get_config_file_path = []() -> std::string {
if (file_manager_utils::cortex_config_file_path.empty()) {
Expand All @@ -53,6 +60,7 @@ bool ServerStartCmd::Exec(const std::string& host, int port) {
std::string params = "--start-server";
params += " --config_file_path " + get_config_file_path();
params += " --data_folder_path " + get_data_folder_path();
params += " --loglevel " + log_level_;
std::string cmds = cortex_utils::GetCurrentPath() + "/" + exe + " " + params;
// Create child process
if (!CreateProcess(
Expand Down Expand Up @@ -107,7 +115,7 @@ bool ServerStartCmd::Exec(const std::string& host, int port) {
std::string p = cortex_utils::GetCurrentPath() + "/" + exe;
execl(p.c_str(), exe.c_str(), "--start-server", "--config_file_path",
get_config_file_path().c_str(), "--data_folder_path",
get_data_folder_path().c_str(), (char*)0);
get_data_folder_path().c_str(), "--loglevel", log_level_.c_str(), (char*)0);
} else {
// Parent process
if (!TryConnectToServer(host, port)) {
Expand Down
4 changes: 2 additions & 2 deletions engine/cli/commands/server_start_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <string>
#include "httplib.h"

#include <optional>
namespace commands {

inline bool IsServerAlive(const std::string& host, int port) {
Expand All @@ -17,6 +17,6 @@ inline bool IsServerAlive(const std::string& host, int port) {
class ServerStartCmd {
public:
ServerStartCmd();
bool Exec(const std::string& host, int port);
bool Exec(const std::string& host, int port, const std::optional<std::string>& log_level = std::nullopt);
};
} // namespace commands
4 changes: 3 additions & 1 deletion engine/controllers/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void server::ChatCompletion(
LOG_DEBUG << "Start chat completion";
auto json_body = req->getJsonObject();
bool is_stream = (*json_body).get("stream", false).asBool();
LOG_DEBUG << "request body: " << json_body->toStyledString();
auto q = std::make_shared<services::SyncQueue>();
auto ir = inference_svc_->HandleChatCompletion(q, json_body);
if (ir.has_error()) {
Expand Down Expand Up @@ -141,7 +142,7 @@ void server::ProcessStreamRes(std::function<void(const HttpResponsePtr&)> cb,
}

auto str = res["data"].asString();
LOG_TRACE << "data: " << str;
LOG_DEBUG << "data: " << str;
std::size_t n = std::min(str.size(), buf_size);
memcpy(buf, str.data(), n);

Expand All @@ -157,6 +158,7 @@ void server::ProcessNonStreamRes(std::function<void(const HttpResponsePtr&)> cb,
services::SyncQueue& q) {
auto [status, res] = q.wait_and_pop();
function_calling_utils::PostProcessResponse(res);
LOG_DEBUG << "response: " << res.toStyledString();
auto resp = cortex_utils::CreateCortexHttpJsonResponse(res);
resp->setStatusCode(
static_cast<drogon::HttpStatusCode>(status["status_code"].asInt()));
Expand Down
3 changes: 2 additions & 1 deletion engine/cortex-common/EngineI.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <memory>

#include "json/value.h"

#include "trantor/utils/Logger.h"
class EngineI {
public:
virtual ~EngineI() {}
Expand Down Expand Up @@ -36,4 +36,5 @@ class EngineI {

virtual bool SetFileLogger(int max_log_lines,
const std::string& log_path) = 0;
virtual void SetLogLevel(trantor::Logger::LogLevel logLevel) = 0;
};
4 changes: 3 additions & 1 deletion engine/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ void RunServer(std::optional<int> port) {
}
std::cout << "Host: " << config.apiServerHost
<< " Port: " << config.apiServerPort << "\n";

// Create logs/ folder and setup log to file
std::filesystem::create_directories(
std::filesystem::path(config.logFolderPath) /
Expand Down Expand Up @@ -181,6 +180,9 @@ int main(int argc, char* argv[]) {
file_manager_utils::cortex_data_folder_path = argv[i + 1];
} else if (strcmp(argv[i], "--port") == 0) {
server_port = std::stoi(argv[i + 1]);
} else if (strcmp(argv[i], "--loglevel") == 0) {
std::string log_level = argv[i + 1];
logging_utils_helper::SetLogLevel(log_level);
}
}

Expand Down
5 changes: 5 additions & 0 deletions engine/services/engine_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,11 @@ cpp::result<void, std::string> EngineService::LoadEngine(
} else {
CTL_WRN("Method SetFileLogger is not supported yet");
}
if (en->IsSupported("SetLogLevel")) {
en->SetLogLevel(trantor::Logger::logLevel());
} else {
CTL_WRN("Method SetLogLevel is not supported yet");
}
}
CTL_DBG("Loaded engine: " << ne);
return {};
Expand Down
2 changes: 1 addition & 1 deletion engine/services/inference_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,4 @@ bool InferenceService::HasFieldInReq(std::shared_ptr<Json::Value> json_body,
}
return true;
}
} // namespace services
} // namespace services
25 changes: 25 additions & 0 deletions engine/utils/logging_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,28 @@ inline bool is_server = false;
LOG_ERROR << msg; \
std::cout << msg << std::endl; \
}

namespace logging_utils_helper {
inline void SetLogLevel(const std::string& log_level) {
if (log_level == "TRACE") {
trantor::Logger::setLogLevel(trantor::Logger::kTrace);
std::cout << "Set log level to TRACE" << std::endl;
} else if (log_level == "DEBUG") {
trantor::Logger::setLogLevel(trantor::Logger::kDebug);
std::cout << "Set log level to DEBUG" << std::endl;
} else if (log_level == "INFO") {
trantor::Logger::setLogLevel(trantor::Logger::kInfo);
std::cout << "Set log level to INFO" << std::endl;
} else if (log_level == "WARN") {
trantor::Logger::setLogLevel(trantor::Logger::kWarn);
std::cout << "Set log level to WARN" << std::endl;
} else if (log_level == "ERROR") {
trantor::Logger::setLogLevel(trantor::Logger::kError);
std::cout << "Set log level to ERROR" << std::endl;
} else {
std::cerr << "Invalid log level: " << log_level
<< ", loglevel must be (TRACE, DEBUG, INFO, WARN or ERROR)"
<< std::endl;
}
}
} // namespace logging_utils_helper

0 comments on commit bc9544a

Please sign in to comment.