summaryrefslogtreecommitdiff
path: root/src/mongo/logger
diff options
context:
space:
mode:
authorHenrik Edin <henrik.edin@mongodb.com>2020-01-21 20:23:50 +0000
committerevergreen <evergreen@mongodb.com>2020-01-21 20:23:50 +0000
commitb7846ff4dceec36e344b0f87c48783dffa2c6a32 (patch)
tree73068bb126bf19901e75bb972a941f17c9759754 /src/mongo/logger
parent41b3d7da7c763e304bc8f4056d0d31d200742e0b (diff)
downloadmongo-b7846ff4dceec36e344b0f87c48783dffa2c6a32.tar.gz
SERVER-45583 Unittests are using logv2.
Capture log system uses logv2, tests can handle converted log statements.
Diffstat (limited to 'src/mongo/logger')
-rw-r--r--src/mongo/logger/log_function_test.cpp3
-rw-r--r--src/mongo/logger/log_manager.cpp17
-rw-r--r--src/mongo/logger/log_test.cpp14
-rw-r--r--src/mongo/logger/log_test.h32
4 files changed, 53 insertions, 13 deletions
diff --git a/src/mongo/logger/log_function_test.cpp b/src/mongo/logger/log_function_test.cpp
index dd4242dbe55..72b3a78d3db 100644
--- a/src/mongo/logger/log_function_test.cpp
+++ b/src/mongo/logger/log_function_test.cpp
@@ -41,6 +41,7 @@
#include "mongo/logger/encoder.h"
#include "mongo/logger/log_component.h"
#include "mongo/logger/message_event_utf8_encoder.h"
+#include "mongo/logv2/text_formatter.h"
#include "mongo/util/log.h"
#include "mongo/util/str.h"
@@ -49,7 +50,7 @@ using namespace mongo::logger;
namespace mongo {
namespace {
-typedef LogTest<MessageEventDetailsEncoder> LogTestDetailsEncoder;
+typedef LogTest<MessageEventDetailsEncoder, logv2::TextFormatter> LogTestDetailsEncoder;
// Constants for log component test cases.
const LogComponent componentA = LogComponent::kCommand;
diff --git a/src/mongo/logger/log_manager.cpp b/src/mongo/logger/log_manager.cpp
index c167853353f..48eb660324f 100644
--- a/src/mongo/logger/log_manager.cpp
+++ b/src/mongo/logger/log_manager.cpp
@@ -27,12 +27,17 @@
* it in the license file.
*/
+#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kDefault
+
#include "mongo/platform/basic.h"
#include "mongo/logger/log_manager.h"
#include "mongo/logger/console_appender.h"
+#include "mongo/logger/logv2_appender.h"
#include "mongo/logger/message_event_utf8_encoder.h"
+#include "mongo/logv2/log_domain_global.h"
+#include "mongo/util/log.h"
namespace mongo {
namespace logger {
@@ -63,9 +68,15 @@ void LogManager::detachDefaultConsoleAppender() {
void LogManager::reattachDefaultConsoleAppender() {
invariant(!_defaultAppender);
- _defaultAppender =
- _globalDomain.attachAppender(std::make_unique<ConsoleAppender<MessageEventEphemeral>>(
- std::make_unique<MessageEventDetailsEncoder>()));
+ if (logV2Enabled()) {
+ _defaultAppender = _globalDomain.attachAppender(
+ std::make_unique<logger::LogV2Appender<MessageEventEphemeral>>(
+ &logv2::LogManager::global().getGlobalDomain(), false));
+ } else {
+ _defaultAppender =
+ _globalDomain.attachAppender(std::make_unique<ConsoleAppender<MessageEventEphemeral>>(
+ std::make_unique<MessageEventDetailsEncoder>()));
+ }
}
bool LogManager::isDefaultConsoleAppenderAttached() const {
diff --git a/src/mongo/logger/log_test.cpp b/src/mongo/logger/log_test.cpp
index 3daf413bf9a..dcb4bb671cb 100644
--- a/src/mongo/logger/log_test.cpp
+++ b/src/mongo/logger/log_test.cpp
@@ -45,6 +45,8 @@
#include "mongo/logger/message_log_domain.h"
#include "mongo/logger/rotatable_file_appender.h"
#include "mongo/logger/rotatable_file_writer.h"
+#include "mongo/logv2/plain_formatter.h"
+#include "mongo/logv2/text_formatter.h"
#include "mongo/platform/compiler.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/concurrency/thread_name.h"
@@ -56,8 +58,8 @@ using namespace mongo::logger;
namespace mongo {
namespace {
-typedef LogTest<MessageEventDetailsEncoder> LogTestDetailsEncoder;
-typedef LogTest<MessageEventUnadornedEncoder> LogTestUnadornedEncoder;
+typedef LogTest<MessageEventDetailsEncoder, logv2::TextFormatter> LogTestDetailsEncoder;
+typedef LogTest<MessageEventUnadornedEncoder, logv2::PlainFormatter> LogTestUnadornedEncoder;
TEST_F(LogTestUnadornedEncoder, logContext) {
logContext("WHA!");
@@ -124,8 +126,8 @@ public:
TEST_F(LogTestUnadornedEncoder, LogstreamBuilderReentrance) {
log() << "Logging A() -- " << A() << " -- done!" << std::endl;
ASSERT_EQUALS(2U, _logLines.size());
- ASSERT_EQUALS(std::string("Golly!\n"), _logLines[0]);
- ASSERT_EQUALS(std::string("Logging A() -- Golly! -- done!\n"), _logLines[1]);
+ ASSERT(StringData(_logLines[0]).startsWith("Golly!"));
+ ASSERT(StringData(_logLines[1]).startsWith("Logging A() -- Golly! -- done!"));
}
//
@@ -155,14 +157,14 @@ TEST_F(LogTestUnadornedEncoder, MongoLogMacroNoFileScopeLogComponent) {
LOG(2) << "This is logged";
LOG(3) << "This is not logged";
ASSERT_EQUALS(1U, _logLines.size());
- ASSERT_EQUALS(std::string("This is logged\n"), _logLines[0]);
+ ASSERT(StringData(_logLines[0]).startsWith("This is logged"));
// MONGO_LOG_COMPONENT
_logLines.clear();
MONGO_LOG_COMPONENT(2, componentA) << "This is logged";
MONGO_LOG_COMPONENT(3, componentA) << "This is not logged";
ASSERT_EQUALS(1U, _logLines.size());
- ASSERT_EQUALS(std::string("This is logged\n"), _logLines[0]);
+ ASSERT(StringData(_logLines[0]).startsWith("This is logged"));
}
//
diff --git a/src/mongo/logger/log_test.h b/src/mongo/logger/log_test.h
index a5e8d914454..72c4a12b00f 100644
--- a/src/mongo/logger/log_test.h
+++ b/src/mongo/logger/log_test.h
@@ -29,6 +29,7 @@
#pragma once
+#include <boost/log/core.hpp>
#include <sstream>
#include <string>
#include <vector>
@@ -37,7 +38,13 @@
#include "mongo/logger/appender.h"
#include "mongo/logger/log_severity.h"
#include "mongo/logger/logger.h"
+#include "mongo/logger/logv2_appender.h"
#include "mongo/logger/message_log_domain.h"
+#include "mongo/logv2/component_settings_filter.h"
+#include "mongo/logv2/log_capture_backend.h"
+#include "mongo/logv2/log_domain.h"
+#include "mongo/logv2/log_domain_global.h"
+#include "mongo/logv2/log_manager.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/log_global_settings.h"
@@ -47,19 +54,37 @@ namespace logger {
// Used for testing logging framework only.
// TODO(schwerin): Have logger write to a different log from the global log, so that tests can
// redirect their global log output for examination.
-template <typename MessageEventEncoder>
+template <typename MessageEventEncoder, typename LOGV2Formatter>
class LogTest : public unittest::Test {
friend class LogTestAppender;
public:
LogTest() : _severityOld(getMinimumLogSeverity()) {
globalLogDomain()->clearAppenders();
- _appenderHandle =
- globalLogDomain()->attachAppender(std::make_unique<LogTestAppender>(this));
+ if (logV2Enabled()) {
+ _appenderHandle = globalLogDomain()->attachAppender(
+ std::make_unique<LogV2Appender<MessageEventEphemeral>>(
+ &logv2::LogManager::global().getGlobalDomain(), true));
+
+ if (!_captureSink) {
+ _captureSink = logv2::LogCaptureBackend::create(_logLines);
+ _captureSink->set_filter(logv2::ComponentSettingsFilter(
+ logv2::LogManager::global().getGlobalDomain(),
+ logv2::LogManager::global().getGlobalSettings()));
+ _captureSink->set_formatter(LOGV2Formatter());
+ }
+ boost::log::core::get()->add_sink(_captureSink);
+ } else {
+ _appenderHandle =
+ globalLogDomain()->attachAppender(std::make_unique<LogTestAppender>(this));
+ }
}
virtual ~LogTest() {
globalLogDomain()->detachAppender(_appenderHandle);
+ if (logV2Enabled()) {
+ boost::log::core::get()->remove_sink(_captureSink);
+ }
setMinimumLoggedSeverity(_severityOld);
}
@@ -86,6 +111,7 @@ private:
};
MessageLogDomain::AppenderHandle _appenderHandle;
+ boost::shared_ptr<boost::log::sinks::synchronous_sink<logv2::LogCaptureBackend>> _captureSink;
};
} // namespace logger