Enable setup output of logs to files at any time. (#1140)

Co-authored-by: Volodymyr Romanishyn <volodymyr.romanishyn@ingenico.com>
This commit is contained in:
Vladimir 2021-12-29 07:20:39 +02:00 committed by GitHub
parent 6c8f8bac1f
commit 9db332af65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 28 deletions

View File

@ -350,6 +350,17 @@ class DROGON_EXPORT HttpAppFramework : public trantor::NonCopyable
const std::function<void(const HttpRequestPtr &,
const HttpResponsePtr &)> &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.

View File

@ -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<trantor::AsyncFileLogger>();
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)

View File

@ -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<void()> termSignalHandler_{[]() { app().quit(); }};
std::function<void()> intSignalHandler_{[]() { app().quit(); }};
std::unique_ptr<SessionManager> sessionManagerPtr_;
std::unique_ptr<trantor::AsyncFileLogger> asyncFileLoggerPtr_;
Json::Value jsonConfig_;
HttpResponsePtr custom404_;
std::function<HttpResponsePtr(HttpStatusCode)> customErrorHandler_ =

View File

@ -56,6 +56,7 @@ class EventLoop;
class TcpConnection;
using TcpConnectionPtr = std::shared_ptr<TcpConnection>;
class Resolver;
class AsyncFileLogger;
} // namespace trantor
namespace drogon