diff --git a/docs/docs/cli/start.mdx b/docs/docs/cli/start.mdx index 37a521ab1..91a8e2819 100644 --- a/docs/docs/cli/start.mdx +++ b/docs/docs/cli/start.mdx @@ -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 to serve the application. | No | `39281` | `-p 39281` | +| `-p`, `--port ` | Port to serve the application. | No | - | `-p 39281` | +| `--loglevel ` | Setup loglevel for cortex server, supported levels are TRACE, DEBUG, INFO, WARN, ERROR | No | - | `--loglevel DEBUG` | + diff --git a/engine/cli/command_line_parser.cc b/engine/cli/command_line_parser.cc index 625750248..5718b18b7 100644 --- a/engine/cli/command_line_parser.cc +++ b/engine/cli/command_line_parser.cc @@ -406,6 +406,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; @@ -422,7 +432,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"); diff --git a/engine/cli/command_line_parser.h b/engine/cli/command_line_parser.h index 9f3cdda12..95eb09a41 100644 --- a/engine/cli/command_line_parser.h +++ b/engine/cli/command_line_parser.h @@ -55,8 +55,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 model_update_options; diff --git a/engine/cli/commands/server_start_cmd.cc b/engine/cli/commands/server_start_cmd.cc index 47a8cf320..e039e5329 100644 --- a/engine/cli/commands/server_start_cmd.cc +++ b/engine/cli/commands/server_start_cmd.cc @@ -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& 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()) { @@ -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( @@ -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)) { diff --git a/engine/cli/commands/server_start_cmd.h b/engine/cli/commands/server_start_cmd.h index 35bd07717..780123172 100644 --- a/engine/cli/commands/server_start_cmd.h +++ b/engine/cli/commands/server_start_cmd.h @@ -2,7 +2,7 @@ #include #include "httplib.h" - +#include namespace commands { inline bool IsServerAlive(const std::string& host, int port) { @@ -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& log_level = std::nullopt); }; } // namespace commands diff --git a/engine/controllers/server.cc b/engine/controllers/server.cc index 95826c6c6..4bec96f76 100644 --- a/engine/controllers/server.cc +++ b/engine/controllers/server.cc @@ -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(); auto ir = inference_svc_->HandleChatCompletion(q, json_body); if (ir.has_error()) { @@ -141,7 +142,7 @@ void server::ProcessStreamRes(std::function 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); @@ -157,6 +158,7 @@ void server::ProcessNonStreamRes(std::function 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(status["status_code"].asInt())); diff --git a/engine/cortex-common/EngineI.h b/engine/cortex-common/EngineI.h index e35c3c3f6..95ce605de 100644 --- a/engine/cortex-common/EngineI.h +++ b/engine/cortex-common/EngineI.h @@ -4,7 +4,7 @@ #include #include "json/value.h" - +#include "trantor/utils/Logger.h" class EngineI { public: virtual ~EngineI() {} @@ -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; }; diff --git a/engine/main.cc b/engine/main.cc index f8c20410f..df5f7b2b1 100644 --- a/engine/main.cc +++ b/engine/main.cc @@ -55,7 +55,6 @@ void RunServer(std::optional 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) / @@ -151,6 +150,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); } } diff --git a/engine/services/engine_service.cc b/engine/services/engine_service.cc index ac5b55a4b..7e903a02f 100644 --- a/engine/services/engine_service.cc +++ b/engine/services/engine_service.cc @@ -832,6 +832,11 @@ cpp::result 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 {}; diff --git a/engine/services/inference_service.cc b/engine/services/inference_service.cc index e7eca3755..46309823d 100644 --- a/engine/services/inference_service.cc +++ b/engine/services/inference_service.cc @@ -240,4 +240,4 @@ bool InferenceService::HasFieldInReq(std::shared_ptr json_body, } return true; } -} // namespace services +} // namespace services \ No newline at end of file diff --git a/engine/utils/logging_utils.h b/engine/utils/logging_utils.h index 02892ed34..c656fd607 100644 --- a/engine/utils/logging_utils.h +++ b/engine/utils/logging_utils.h @@ -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 \ No newline at end of file