diff --git a/CMakeLists.txt b/CMakeLists.txt index d6958fd6d..d790479fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/src/communication/server.hpp b/src/communication/server.hpp index 3c1f9a0b4..189199fa3 100644 --- a/src/communication/server.hpp +++ b/src/communication/server.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include #include @@ -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(); } diff --git a/src/logging/streams/file.hpp b/src/logging/streams/file.hpp new file mode 100644 index 000000000..1e971afda --- /dev/null +++ b/src/logging/streams/file.hpp @@ -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_; +}; + diff --git a/src/memgraph_bolt.cpp b/src/memgraph_bolt.cpp index 3e15dcc61..a02c291d9 100644 --- a/src/memgraph_bolt.cpp +++ b/src/memgraph_bolt.cpp @@ -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()); +#endif + logging::log->pipe(std::make_unique(FLAGS_log_file)); // Get logger. logger = logging::log->logger("Main");