diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index f54de077..c980a64f 100644 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -350,6 +350,17 @@ class DROGON_EXPORT HttpAppFramework : public trantor::NonCopyable const std::function &advice) = 0; + /// Setup output of logs to files + /** + * @note + * Logs are output to the standard output by default. + * Logging is setuped only if output path of logs is defined. + * This method is called in run() function, hence use this method only if + * you want to setup logging earlier. + * @return HttpAppFramework& + */ + virtual HttpAppFramework &setupFileLogger() = 0; + /* End of AOP methods */ /// Load the configuration file with json format. diff --git a/lib/src/HttpAppFrameworkImpl.cc b/lib/src/HttpAppFrameworkImpl.cc index 792d3dda..4b2e5e84 100644 --- a/lib/src/HttpAppFrameworkImpl.cc +++ b/lib/src/HttpAppFrameworkImpl.cc @@ -452,7 +452,6 @@ void HttpAppFrameworkImpl::run() getLoop()->moveToCurrentThread(); } LOG_TRACE << "Start to run..."; - trantor::AsyncFileLogger asyncFileLogger; // Create dirs for cache files for (int i = 0; i < 256; ++i) { @@ -517,33 +516,7 @@ void HttpAppFrameworkImpl::run() } #endif } - // set logger - if (!logPath_.empty()) - { - // std::filesystem does not provide a method to check access - // permissions, so keep existing code - if (os_access(utils::toNativePath(logPath_).c_str(), R_OK | W_OK) != 0) - { - LOG_ERROR << "log file path not exist"; - abort(); - } - else - { - std::string baseName = logfileBaseName_; - if (baseName.empty()) - { - baseName = "drogon"; - } - asyncFileLogger.setFileName(baseName, ".log", logPath_); - asyncFileLogger.startLogging(); - trantor::Logger::setOutputFunction( - [&](const char *msg, const uint64_t len) { - asyncFileLogger.output(msg, len); - }, - [&]() { asyncFileLogger.flush(); }); - asyncFileLogger.setFileSizeLimit(logfileSize_); - } - } + setupFileLogger(); if (relaunchOnError_) { LOG_INFO << "Start child process"; @@ -1120,6 +1093,38 @@ HttpAppFramework &HttpAppFrameworkImpl::setDefaultHandler( return *this; } +HttpAppFramework &HttpAppFrameworkImpl::setupFileLogger() +{ + if (!logPath_.empty() && !asyncFileLoggerPtr_) + { + // std::filesystem does not provide a method to check access + // permissions, so keep existing code + if (os_access(utils::toNativePath(logPath_).c_str(), R_OK | W_OK) != 0) + { + LOG_ERROR << "log file path not exist"; + abort(); + } + else + { + std::string baseName = logfileBaseName_; + if (baseName.empty()) + { + baseName = "drogon"; + } + asyncFileLoggerPtr_ = std::make_unique(); + asyncFileLoggerPtr_->setFileName(baseName, ".log", logPath_); + asyncFileLoggerPtr_->startLogging(); + trantor::Logger::setOutputFunction( + [this](const char *msg, const uint64_t len) { + asyncFileLoggerPtr_->output(msg, len); + }, + [this]() { asyncFileLoggerPtr_->flush(); }); + asyncFileLoggerPtr_->setFileSizeLimit(logfileSize_); + } + } + return *this; +} + HttpAppFramework &HttpAppFrameworkImpl::registerCustomExtensionMime( const std::string &ext, const std::string &mime) diff --git a/lib/src/HttpAppFrameworkImpl.h b/lib/src/HttpAppFrameworkImpl.h index 9d2d73a2..c47dcdfa 100644 --- a/lib/src/HttpAppFrameworkImpl.h +++ b/lib/src/HttpAppFrameworkImpl.h @@ -205,6 +205,8 @@ class HttpAppFrameworkImpl final : public HttpAppFramework } HttpAppFramework &setDefaultHandler(DefaultHandler handler) override; + HttpAppFramework &setupFileLogger() override; + HttpAppFramework &enableSession(const size_t timeout) override { useSession_ = true; @@ -634,6 +636,7 @@ class HttpAppFrameworkImpl final : public HttpAppFramework std::function termSignalHandler_{[]() { app().quit(); }}; std::function intSignalHandler_{[]() { app().quit(); }}; std::unique_ptr sessionManagerPtr_; + std::unique_ptr asyncFileLoggerPtr_; Json::Value jsonConfig_; HttpResponsePtr custom404_; std::function customErrorHandler_ = diff --git a/lib/src/impl_forwards.h b/lib/src/impl_forwards.h index dba1c0b6..dd7e266a 100644 --- a/lib/src/impl_forwards.h +++ b/lib/src/impl_forwards.h @@ -56,6 +56,7 @@ class EventLoop; class TcpConnection; using TcpConnectionPtr = std::shared_ptr; class Resolver; +class AsyncFileLogger; } // namespace trantor namespace drogon