summaryrefslogtreecommitdiff
path: root/src/mongo/logv2
diff options
context:
space:
mode:
authorSara Golemon <sara.golemon@mongodb.com>2020-07-20 16:07:10 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-08-18 23:05:40 +0000
commit749a0a172662baf99cf4487f97029bb2e29fb4f3 (patch)
tree6495ce2884ad42a2f29ced217ef6aee9a3a9ca7a /src/mongo/logv2
parent30acdea3aa54dab658cb76908a8cd9bf35e7762f (diff)
downloadmongo-749a0a172662baf99cf4487f97029bb2e29fb4f3.tar.gz
SERVER-49768 Remove logv1 logger components, and make rotation a callback list
Diffstat (limited to 'src/mongo/logv2')
-rw-r--r--src/mongo/logv2/log_manager.cpp9
-rw-r--r--src/mongo/logv2/log_util.cpp39
-rw-r--r--src/mongo/logv2/log_util.h9
3 files changed, 39 insertions, 18 deletions
diff --git a/src/mongo/logv2/log_manager.cpp b/src/mongo/logv2/log_manager.cpp
index a9c6ca5013e..4794636ea45 100644
--- a/src/mongo/logv2/log_manager.cpp
+++ b/src/mongo/logv2/log_manager.cpp
@@ -35,9 +35,11 @@
#include <boost/log/core.hpp>
+#include "mongo/base/init.h"
#include "mongo/logv2/log.h"
#include "mongo/logv2/log_domain.h"
#include "mongo/logv2/log_domain_global.h"
+#include "mongo/logv2/log_util.h"
namespace mongo::logv2 {
@@ -98,4 +100,11 @@ LogComponentSettings& LogManager::getGlobalSettings() {
return getGlobalDomainInternal().settings();
}
+MONGO_INITIALIZER(GlobalLogRotator)(InitializerContext*) {
+ addLogRotator([](bool renameFiles, StringData suffix) {
+ return LogManager::global().getGlobalDomainInternal().rotate(renameFiles, suffix);
+ });
+ return Status::OK();
+}
+
} // namespace mongo::logv2
diff --git a/src/mongo/logv2/log_util.cpp b/src/mongo/logv2/log_util.cpp
index 79f2abbd88a..0e7a6526a80 100644
--- a/src/mongo/logv2/log_util.cpp
+++ b/src/mongo/logv2/log_util.cpp
@@ -31,38 +31,41 @@
#include "mongo/logv2/log_util.h"
-#include "mongo/logger/logger.h"
-#include "mongo/logger/rotatable_file_manager.h"
#include "mongo/logv2/log.h"
-#include "mongo/logv2/log_domain_global.h"
-#include "mongo/logv2/log_manager.h"
+#include "mongo/platform/atomic_word.h"
#include "mongo/util/time_support.h"
#include <string>
+#include <vector>
namespace mongo::logv2 {
namespace {
AtomicWord<bool> redactionEnabled{false};
+std::vector<LogRotateCallback> logRotateCallbacks;
+} // namespace
+
+void addLogRotator(LogRotateCallback cb) {
+ logRotateCallbacks.push_back(std::move(cb));
}
+
bool rotateLogs(bool renameFiles) {
// Rotate on both logv1 and logv2 so all files that need rotation gets rotated
- LOGV2(23166, "Log rotation initiated");
std::string suffix = "." + terseCurrentTime(false);
- Status resultv2 =
- logv2::LogManager::global().getGlobalDomainInternal().rotate(renameFiles, suffix);
- if (!resultv2.isOK())
- LOGV2_WARNING(23168, "Log rotation failed", "reason"_attr = resultv2);
+ LOGV2(23166, "Log rotation initiated", "suffix"_attr = suffix);
+ bool success = true;
- using logger::RotatableFileManager;
- RotatableFileManager* manager = logger::globalRotatableFileManager();
- RotatableFileManager::FileNameStatusPairVector result(manager->rotateAll(renameFiles, suffix));
- for (RotatableFileManager::FileNameStatusPairVector::iterator it = result.begin();
- it != result.end();
- it++) {
- LOGV2_WARNING(
- 23169, "Rotating log file failed", "file"_attr = it->first, "reason"_attr = it->second);
+ // 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);
+ if (!status.isOK()) {
+ LOGV2_WARNING(23168, "Log rotation failed", "reason"_attr = status);
+ success = false;
+ }
}
- return resultv2.isOK() && result.empty();
+
+ return success;
}
bool shouldRedactLogs() {
diff --git a/src/mongo/logv2/log_util.h b/src/mongo/logv2/log_util.h
index c03d81f05f6..bd7bda6865e 100644
--- a/src/mongo/logv2/log_util.h
+++ b/src/mongo/logv2/log_util.h
@@ -29,7 +29,16 @@
#pragma once
+#include <functional>
+
+#include <mongo/base/status.h>
+#include <mongo/base/string_data.h>
+
namespace mongo::logv2 {
+
+using LogRotateCallback = std::function<Status(bool, StringData)>;
+void addLogRotator(LogRotateCallback cb);
+
/**
* Rotates the log files. Returns true if all logs rotate successfully.
*