diff --git a/dbms/programs/server/Server.cpp b/dbms/programs/server/Server.cpp index a78b183e6e1..d97fe4aff2c 100644 --- a/dbms/programs/server/Server.cpp +++ b/dbms/programs/server/Server.cpp @@ -273,17 +273,13 @@ int Server::main(const std::vector & /*args*/) * table engines could use Context on destroy. */ LOG_INFO(log, "Shutting down storages."); - if (text_log) - text_log->shutdown(); global_context->shutdown(); LOG_DEBUG(log, "Shutted down storages."); /** Explicitly destroy Context. It is more convenient than in destructor of Server, because logger is still available. * At this moment, no one could own shared part of Context. */ - text_log.reset(); global_context.reset(); - LOG_DEBUG(log, "Destroyed global context."); }); @@ -413,7 +409,7 @@ int Server::main(const std::vector & /*args*/) main_config_zk_changed_event, [&](ConfigurationPtr config) { - setTextLog(text_log); + setTextLog(global_context->getTextLog()); buildLoggers(*config, logger()); global_context->setClustersConfig(config); global_context->setMacros(std::make_unique(*config, "macros")); @@ -500,14 +496,11 @@ int Server::main(const std::vector & /*args*/) LOG_INFO(log, "Loading metadata from " + path); - /// Create text_log instance - text_log = createSystemLog(*global_context, "system", "text_log", global_context->getConfigRef(), "text_log"); - try { loadMetadataSystem(*global_context); /// After attaching system databases we can initialize system log. - global_context->initializeSystemLogs(text_log); + global_context->initializeSystemLogs(); /// After the system database is created, attach virtual system tables (in addition to query_log and part_log) attachSystemTablesServer(*global_context->getDatabase("system"), has_zookeeper); /// Then, load remaining databases diff --git a/dbms/programs/server/Server.h b/dbms/programs/server/Server.h index 5fc5f16b550..337d1551b70 100644 --- a/dbms/programs/server/Server.h +++ b/dbms/programs/server/Server.h @@ -57,7 +57,6 @@ protected: private: std::unique_ptr global_context; - std::shared_ptr text_log; }; } diff --git a/dbms/src/Interpreters/Context.cpp b/dbms/src/Interpreters/Context.cpp index 59c88ed5ed8..992593d852c 100644 --- a/dbms/src/Interpreters/Context.cpp +++ b/dbms/src/Interpreters/Context.cpp @@ -1645,11 +1645,10 @@ Compiler & Context::getCompiler() } -void Context::initializeSystemLogs(std::shared_ptr text_log) +void Context::initializeSystemLogs() { auto lock = getLock(); shared->system_logs.emplace(*global_context, getConfigRef()); - shared->system_logs->text_log = text_log; } bool Context::hasTraceCollector() @@ -1716,11 +1715,10 @@ std::shared_ptr Context::getTextLog() { auto lock = getLock(); - if (!shared->system_logs) - if (auto log = shared->system_logs->text_log.lock()) - return log; + if (!shared->system_logs || !shared->system_logs->text_log) + return {}; - return {}; + return shared->system_logs->text_log; } diff --git a/dbms/src/Interpreters/Context.h b/dbms/src/Interpreters/Context.h index f6998c77824..50b7ab3eba2 100644 --- a/dbms/src/Interpreters/Context.h +++ b/dbms/src/Interpreters/Context.h @@ -424,7 +424,7 @@ public: Compiler & getCompiler(); /// Call after initialization before using system logs. Call for global context. - void initializeSystemLogs(std::shared_ptr text_log); + void initializeSystemLogs(); void initializeTraceCollector(); bool hasTraceCollector(); diff --git a/dbms/src/Interpreters/SystemLog.cpp b/dbms/src/Interpreters/SystemLog.cpp index 4b456bc2542..f1f65dfe883 100644 --- a/dbms/src/Interpreters/SystemLog.cpp +++ b/dbms/src/Interpreters/SystemLog.cpp @@ -47,6 +47,7 @@ SystemLogs::SystemLogs(Context & global_context, const Poco::Util::AbstractConfi query_thread_log = createSystemLog(global_context, "system", "query_thread_log", config, "query_thread_log"); part_log = createSystemLog(global_context, "system", "part_log", config, "part_log"); trace_log = createSystemLog(global_context, "system", "trace_log", config, "trace_log"); + text_log = createSystemLog(global_context, "system", "text_log", config, "text_log"); part_log_database = config.getString("part_log.database", "system"); } @@ -67,6 +68,8 @@ void SystemLogs::shutdown() part_log->shutdown(); if (trace_log) trace_log->shutdown(); + if (text_log) + text_log->shutdown(); } } diff --git a/dbms/src/Interpreters/SystemLog.h b/dbms/src/Interpreters/SystemLog.h index 7b1192ac970..3dd329d577b 100644 --- a/dbms/src/Interpreters/SystemLog.h +++ b/dbms/src/Interpreters/SystemLog.h @@ -75,8 +75,7 @@ struct SystemLogs std::shared_ptr query_thread_log; /// Used to log query threads. std::shared_ptr part_log; /// Used to log operations with parts std::shared_ptr trace_log; /// Used to log traces from query profiler - std::weak_ptr text_log; /// Used to log all text. We use weak_ptr, because this log is - /// a server's field. + std::shared_ptr text_log; /// Used to log all text messages. String part_log_database; }; diff --git a/libs/libcommon/include/common/logger_useful.h b/libs/libcommon/include/common/logger_useful.h index e5f9ea2b996..d7466273320 100644 --- a/libs/libcommon/include/common/logger_useful.h +++ b/libs/libcommon/include/common/logger_useful.h @@ -20,23 +20,25 @@ using DB::CurrentThread; /// Logs a message to a specified logger with that level. -#define LOG_SIMPLE(logger, message, priority, PRIORITY) do \ -{ \ - const bool is_clients_log = (CurrentThread::getGroup() != nullptr) && \ - (CurrentThread::getGroup()->client_logs_level >= (priority)); \ - if ((logger)->is((PRIORITY)) || is_clients_log) { \ - std::stringstream oss_internal_rare; \ - oss_internal_rare << message; \ - if (auto channel = (logger)->getChannel()) { \ - std::string file_function; \ - file_function += __FILE__; \ - file_function += ", "; \ - file_function += __PRETTY_FUNCTION__; \ - Message poco_message((logger)->name(), oss_internal_rare.str(), \ - (PRIORITY), file_function.c_str(), __LINE__); \ - channel->log(poco_message); \ - } \ - } \ +#define LOG_SIMPLE(logger, message, priority, PRIORITY) do \ +{ \ + const bool is_clients_log = (CurrentThread::getGroup() != nullptr) && \ + (CurrentThread::getGroup()->client_logs_level >= (priority)); \ + if ((logger)->is((PRIORITY)) || is_clients_log) \ + { \ + std::stringstream oss_internal_rare; \ + oss_internal_rare << message; \ + if (auto channel = (logger)->getChannel()) \ + { \ + std::string file_function; \ + file_function += __FILE__; \ + file_function += "; "; \ + file_function += __PRETTY_FUNCTION__; \ + Message poco_message((logger)->name(), oss_internal_rare.str(), \ + (PRIORITY), file_function.c_str(), __LINE__); \ + channel->log(poco_message); \ + } \ + } \ } while (false)