diff --git a/CMakeLists.txt b/CMakeLists.txt index 254d8a2..7d35ccb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ set(Boost_USE_MULTITHREADED ON) set(Boost_INCLUDE_DIR ${BOOST_ROOT}) set(Boost_LIBRARY_DIR "${BOOST_ROOT}/stage/lib") -find_package(Boost 1.88.0 REQUIRED COMPONENTS filesystem json log) +find_package(Boost 1.88.0 REQUIRED COMPONENTS filesystem json log system filesystem) if (Boost_FOUND) include_directories(${Boost_INCLUDE_DIR}) endif () @@ -50,9 +50,17 @@ add_executable(App ./src/main.cpp ./src/endpoints_handlers/AuthLogoutExecutor.h ./src/exceptions/session_exception.cpp ./src/exceptions/session_exception.h + src/log/Log.h + src/log/Log.cpp ) -target_link_libraries(App PRIVATE Boost::boost Boost::json Threads::Threads mysql::concpp) +target_link_libraries(App PRIVATE Boost::boost + Boost::json + Boost::log + Boost::system + Boost::filesystem + Threads::Threads + mysql::concpp) if (MSVC) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj") diff --git a/src/log/Log.cpp b/src/log/Log.cpp new file mode 100644 index 0000000..7ba1a97 --- /dev/null +++ b/src/log/Log.cpp @@ -0,0 +1,21 @@ +#pragma once + +#include +#include +#include + +namespace logging = boost::log; + +namespace uad +{ +void InitLogs() +{ + logging::add_file_log( + logging::keywords::file_name = "app_%Y-%m-%d_%H-%M-%S.log", // Имя файла с timestamp + logging::keywords::rotation_size = 10 * 1024 * 1024, // Ротация при достижении 10 МБ + logging::keywords::time_based_rotation = + logging::sinks::file::rotation_at_time_point(0, 0, 0), // Ротация каждый день в полночь + logging::keywords::format = "[%TimeStamp%] [%Severity%]: %Message%" // Формат записи +); +} +} diff --git a/src/log/Log.h b/src/log/Log.h new file mode 100644 index 0000000..faaa3d2 --- /dev/null +++ b/src/log/Log.h @@ -0,0 +1,6 @@ +#pragma once + +namespace uad +{ +void InitLogs(); +} diff --git a/src/main.cpp b/src/main.cpp index ec8e577..bb9b3f8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,11 +14,15 @@ #include #include #include +#include +#include +#include #include "./session/WebsocketSession.h" #include "./listener/Listener.h" #include "./db/mysql_connector.h" #include "entities/user.h" +#include "log/Log.h" namespace beast = boost::beast; namespace http = beast::http; @@ -28,6 +32,7 @@ using tcp = boost::asio::ip::tcp; using namespace uad; using namespace std; using namespace std::string_literals; +namespace logging = boost::log; int main(int argc, char* argv[]) { @@ -44,6 +49,11 @@ int main(int argc, char* argv[]) auto const threads = std::max(1, std::atoi(argv[4])); string mysql_credentials = argv[5]; + InitLogs(); + + // Добавление общих атрибутов (включая время) + logging::add_common_attributes(); + uad::SetMySqlSession(new mysqlx::Session(mysql_credentials)); net::io_context ioc{threads}; @@ -53,6 +63,8 @@ int main(int argc, char* argv[]) net::signal_set signals(ioc, SIGINT, SIGTERM); signals.async_wait([&](beast::error_code const&, int) { ioc.stop(); }); + BOOST_LOG_TRIVIAL(error) << "Приложение запущено"; + std::vector v; v.reserve(threads - 1); for (auto i = threads - 1; i > 0; --i)