summaryrefslogtreecommitdiff
path: root/src/mongo/logv2
diff options
context:
space:
mode:
authorShreyas Kalyan <shreyas.kalyan@10gen.com>2020-11-17 11:22:48 -0800
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-12-11 00:14:22 +0000
commit05e0b5789d90774c262037b8f7ccda3a07cbecf5 (patch)
treee8ff8bb869f3f8958b5ecd4f27a9b32b0a38de43 /src/mongo/logv2
parent3ade1b6edcf2c4c21c0ad0fccfeb6f292baa9d1a (diff)
downloadmongo-05e0b5789d90774c262037b8f7ccda3a07cbecf5.tar.gz
SERVER-19470 logRotate - Currently rotates both audit log and server log
Diffstat (limited to 'src/mongo/logv2')
-rw-r--r--src/mongo/logv2/log_manager.cpp2
-rw-r--r--src/mongo/logv2/log_util.cpp48
-rw-r--r--src/mongo/logv2/log_util.h13
3 files changed, 44 insertions, 19 deletions
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.