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:
parent
6c8f8bac1f
commit
9db332af65
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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_ =
|
||||
|
|
|
@ -56,6 +56,7 @@ class EventLoop;
|
|||
class TcpConnection;
|
||||
using TcpConnectionPtr = std::shared_ptr<TcpConnection>;
|
||||
class Resolver;
|
||||
class AsyncFileLogger;
|
||||
} // namespace trantor
|
||||
|
||||
namespace drogon
|
||||
|
|
Loading…
Reference in New Issue