Default logger logs to stderr, not stdout

This commit is contained in:
Sebastian Meßmer 2015-10-17 16:49:58 +02:00
parent 08c09e4af8
commit 05f9723295
4 changed files with 13 additions and 14 deletions

View File

@ -31,7 +31,7 @@ namespace logging {
private:
static std::shared_ptr<spdlog::logger> _defaultLogger() {
static std::shared_ptr<spdlog::logger> singleton = spdlog::stdout_logger_mt("Log");
static auto singleton = spdlog::stderr_logger_mt("Log");
return singleton;
}

View File

@ -15,6 +15,6 @@ TEST_F(LoggerTest, IsSingleton) {
}
TEST_F(LoggerTest, SetLogger) {
logger().setLogger(spdlog::stdout_logger_mt("MyTestLog1"));
logger().setLogger(spdlog::stderr_logger_mt("MyTestLog1"));
EXPECT_EQ("MyTestLog1", logger()->name());
}

View File

@ -9,30 +9,30 @@ using namespace cpputils::logging;
using std::string;
using testing::MatchesRegex;
TEST_F(LoggingTest, DefaultLoggerIsStdout) {
string output = captureStdout([]{
TEST_F(LoggingTest, DefaultLoggerIsStderr) {
string output = captureStderr([]{
LOG(INFO) << "My log message";
});
EXPECT_THAT(output, MatchesRegex(".*\\[Log\\].*\\[info\\].*My log message.*"));
}
TEST_F(LoggingTest, SetLogger_NewLoggerIsUsed) {
setLogger(spdlog::stdout_logger_mt("MyTestLog2"));
string output = captureStdout([]{
setLogger(spdlog::stderr_logger_mt("MyTestLog2"));
string output = captureStderr([]{
LOG(INFO) << "My log message";
});
EXPECT_THAT(output, MatchesRegex(".*\\[MyTestLog2\\].*\\[info\\].*My log message.*"));
}
TEST_F(LoggingTest, SetNonStdoutLogger_LogsToNewLogger) {
TEST_F(LoggingTest, SetNonStderrLogger_LogsToNewLogger) {
setLogger(mockLogger.get());
logger()->info() << "My log message";
EXPECT_THAT(mockLogger.capturedLog(), MatchesRegex(".*\\[MockLogger\\].*\\[info\\].*My log message.*"));
}
TEST_F(LoggingTest, SetNonStdoutLogger_DoesNotLogToStdout) {
TEST_F(LoggingTest, SetNonStderrLogger_DoesNotLogToStderr) {
setLogger(mockLogger.get());
string output = captureStdout([] {
string output = captureStderr([] {
logger()->info() << "My log message";
});
EXPECT_EQ("", output);
@ -71,8 +71,7 @@ void logAndExit(const string &message) {
// fork() only forks the main thread. This test ensures that logging doesn't depend on threads that suddenly aren't
// there anymore after a fork().
TEST_F(LoggingTest, LoggingAlsoWorksAfterFork) {
auto sink = std::make_shared<spdlog::sinks::ostream_sink<std::mutex>>(std::cerr, true);
setLogger(spdlog::create("StderrLogger", {sink}));
setLogger(spdlog::stderr_logger_mt("StderrLogger"));
//TODO Use EXPECT_EXIT instead once the gtest version is new enough to support it
EXPECT_DEATH(
logAndExit("My log message"),

View File

@ -33,10 +33,10 @@ private:
class LoggingTest: public ::testing::Test {
public:
std::string captureStdout(std::function<void()> func) {
testing::internal::CaptureStdout();
std::string captureStderr(std::function<void()> func) {
testing::internal::CaptureStderr();
func();
return testing::internal::GetCapturedStdout();
return testing::internal::GetCapturedStderr();
}
~LoggingTest() {