Add simple logging to a file

Reviewers: buda

Reviewed By: buda

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D476
This commit is contained in:
Teon Banek 2017-06-16 09:07:33 +02:00
parent c820b25c9c
commit d0016ab98c
4 changed files with 40 additions and 0 deletions

View File

@ -159,7 +159,13 @@ endif()
# -----------------------------------------------------------------------------
# logging levels --------------------------------------------------------------
option (LOG_NO_STDOUT
"Disable logging to stdout. (Docker has a bug with large logs.)"
OFF)
option(LOG_NO_TRACE "Disable trace logging" OFF)
if (LOG_NO_STDOUT)
add_definitions(-DLOG_NO_STDOUT)
endif()
message(STATUS "LOG_NO_TRACE: ${LOG_NO_TRACE}")
if (LOG_NO_TRACE)
add_definitions(-DLOG_NO_TRACE)

View File

@ -1,6 +1,7 @@
#pragma once
#include <atomic>
#include <iostream>
#include <memory>
#include <thread>
#include <vector>
@ -63,6 +64,12 @@ class Server
dbms_, query_engine_));
workers_.back()->Start(alive_);
}
#ifdef LOG_NO_STDOUT
// TODO: Remove this when we switch to glog.
std::cout << "Server is fully armed and operational" << std::endl
<< "Listening on " << socket_.endpoint().address() << " at "
<< socket_.endpoint().port() << std::endl;
#endif
logger_.info("Server is fully armed and operational");
logger_.info("Listening on {} at {}", socket_.endpoint().address(),
socket_.endpoint().port());
@ -70,6 +77,10 @@ class Server
this->WaitAndProcessEvents();
}
#ifdef LOG_NO_STDOUT
// TODO: Remove this when we switch to glog.
std::cout << "Shutting down..." << std::endl;
#endif
logger_.info("Shutting down...");
for (auto &worker : workers_) worker->thread_.join();
}

View File

@ -0,0 +1,17 @@
#pragma once
#include "logging/log.hpp"
#include "logging/streams/format.hpp"
class File : public Log::Stream {
public:
File(const std::string &filename) : file_(filename) {}
void emit(const Log::Record &record) override {
file_ << logging::Formatter::format(logging::format::out, record);
file_.flush();
}
private:
std::ofstream file_;
};

View File

@ -15,6 +15,7 @@
#include "logging/default.hpp"
#include "logging/streams/stdout.hpp"
#include "logging/streams/file.hpp"
#include "utils/flag_validation.hpp"
#include "utils/signals/handler.hpp"
@ -38,6 +39,8 @@ DEFINE_string(port, "7687", "Default port on which to listen.");
DEFINE_VALIDATED_int32(num_workers,
std::max(std::thread::hardware_concurrency(), 1U),
"Number of workers", FLAG_IN_RANGE(1, INT32_MAX));
DEFINE_string(log_file, "memgraph.log",
"Path to where the log should be stored.");
// Load flags in this order, the last one has the highest priority:
// 1) /etc/memgraph/config
@ -90,7 +93,10 @@ int main(int argc, char **argv) {
#else
logging::init_async();
#endif
#ifndef LOG_NO_STDOUT
logging::log->pipe(std::make_unique<Stdout>());
#endif
logging::log->pipe(std::make_unique<File>(FLAGS_log_file));
// Get logger.
logger = logging::log->logger("Main");