diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/commands/generic_servers.cpp | 16 | ||||
-rw-r--r-- | src/mongo/logv2/log_manager.cpp | 2 | ||||
-rw-r--r-- | src/mongo/logv2/log_util.cpp | 48 | ||||
-rw-r--r-- | src/mongo/logv2/log_util.h | 13 |
4 files changed, 56 insertions, 23 deletions
diff --git a/src/mongo/db/commands/generic_servers.cpp b/src/mongo/db/commands/generic_servers.cpp index 8e79e8a9806..342acc56b63 100644 --- a/src/mongo/db/commands/generic_servers.cpp +++ b/src/mongo/db/commands/generic_servers.cpp @@ -193,11 +193,19 @@ public: const std::string& ns, const BSONObj& cmdObj, BSONObjBuilder& result) { - bool didRotate = logv2::rotateLogs(serverGlobalParams.logRenameOnRotate); - if (didRotate) + BSONElement val = cmdObj.firstElement(); + + boost::optional<StringData> logType = boost::none; + if (val.type() == String) { + logType = val.checkAndGetStringData(); + } + + if (logv2::rotateLogs(serverGlobalParams.logRenameOnRotate, logType)) { logProcessDetailsForLogRotate(opCtx->getServiceContext()); - return didRotate; - return true; + return true; + } + + return false; } } logRotateCmd; diff --git a/src/mongo/logv2/log_manager.cpp b/src/mongo/logv2/log_manager.cpp index e75b2f155bc..4edc4eac83e 100644 --- a/src/mongo/logv2/log_manager.cpp +++ b/src/mongo/logv2/log_manager.cpp @@ -101,7 +101,7 @@ LogComponentSettings& LogManager::getGlobalSettings() { } MONGO_INITIALIZER(GlobalLogRotator)(InitializerContext*) { - addLogRotator([](bool renameFiles, StringData suffix) { + addLogRotator(logv2::kServerLogTag, [](bool renameFiles, StringData suffix) { return LogManager::global().getGlobalDomainInternal().rotate(renameFiles, suffix); }); } diff --git a/src/mongo/logv2/log_util.cpp b/src/mongo/logv2/log_util.cpp index b37e98b6bed..63aadc11735 100644 --- a/src/mongo/logv2/log_util.cpp +++ b/src/mongo/logv2/log_util.cpp @@ -39,33 +39,49 @@ #include <vector> namespace mongo::logv2 { + namespace { AtomicWord<bool> redactionEnabled{false}; -std::vector<LogRotateCallback> logRotateCallbacks; +std::map<StringData, LogRotateCallback> logRotateCallbacks; } // namespace -void addLogRotator(LogRotateCallback cb) { - logRotateCallbacks.push_back(std::move(cb)); +void addLogRotator(StringData logType, LogRotateCallback cb) { + logRotateCallbacks.emplace(logType, std::move(cb)); } -bool rotateLogs(bool renameFiles) { - // Rotate on both logv1 and logv2 so all files that need rotation gets rotated +bool rotateLogs(bool renameFiles, boost::optional<StringData> logType) { std::string suffix = "." + terseCurrentTimeForFilename(); - LOGV2(23166, "Log rotation initiated", "suffix"_attr = suffix); - bool success = true; - // Call each callback in turn. - // If they fail, they must log why. - // We only return true if all succeed. - for (const auto& cb : logRotateCallbacks) { - auto status = cb(renameFiles, suffix); + LOGV2(23166, "Log rotation initiated", "suffix"_attr = suffix, "logType"_attr = logType); + + if (logType) { + auto it = logRotateCallbacks.find(logType.get()); + if (it == logRotateCallbacks.end()) { + LOGV2_WARNING( + ErrorCodes::NoSuchKey, "Unknown log type for rotate", "logType"_attr = logType); + return false; + } + auto status = it->second(renameFiles, suffix); if (!status.isOK()) { - LOGV2_WARNING(23168, "Log rotation failed", "reason"_attr = status); - success = false; + LOGV2_WARNING( + 1947001, "Log rotation failed", "reason"_attr = status, "logType"_attr = logType); + return false; } + return true; + } else { + bool ret = true; + for (const auto& entry : logRotateCallbacks) { + auto status = entry.second(renameFiles, suffix); + if (!status.isOK()) { + LOGV2_WARNING(23168, + "Log rotation failed", + "reason"_attr = status, + "logType"_attr = entry.first); + ret = false; + } + } + return ret; } - - return success; } bool shouldRedactLogs() { diff --git a/src/mongo/logv2/log_util.h b/src/mongo/logv2/log_util.h index bd7bda6865e..1bbafffda85 100644 --- a/src/mongo/logv2/log_util.h +++ b/src/mongo/logv2/log_util.h @@ -31,13 +31,22 @@ #include <functional> +#include <boost/optional.hpp> #include <mongo/base/status.h> #include <mongo/base/string_data.h> namespace mongo::logv2 { +constexpr auto kServerLogTag = "server"_sd; +constexpr auto kAuditLogTag = "audit"_sd; + using LogRotateCallback = std::function<Status(bool, StringData)>; -void addLogRotator(LogRotateCallback cb); + +/** + * logType param needs to have static lifetime. If a new logType needs to be defined, add it above + * with the other constexpr logTags. + */ +void addLogRotator(StringData logType, LogRotateCallback cb); /** * Rotates the log files. Returns true if all logs rotate successfully. @@ -50,7 +59,7 @@ void addLogRotator(LogRotateCallback cb); * We expect logrotate to rename the existing file before we rotate, and so the next open * we do should result in a file create. */ -bool rotateLogs(bool renameFiles); +bool rotateLogs(bool renameFiles, boost::optional<StringData> logType = boost::none); /** * Returns true if system logs should be redacted. |