diff --git a/python/ray/tempfile_services.py b/python/ray/tempfile_services.py index 5e5bf2c3f..0ffced3fa 100644 --- a/python/ray/tempfile_services.py +++ b/python/ray/tempfile_services.py @@ -169,10 +169,6 @@ def new_log_files(name, redirect_output): logs_dir = get_logs_dir_path() # Create another directory that will be used by some of the RL algorithms. - # TODO(suquark): This is done by the old code. - # We should be able to control its path later. - try_to_create_directory("/tmp/ray") - log_stdout = make_inc_temp( suffix=".out", prefix=name, directory_name=logs_dir) log_stderr = make_inc_temp( diff --git a/src/ray/util/logging.cc b/src/ray/util/logging.cc index cdc12e427..97c871d8c 100644 --- a/src/ray/util/logging.cc +++ b/src/ray/util/logging.cc @@ -67,6 +67,7 @@ typedef ray::CerrLog LoggingProvider; RayLogLevel RayLog::severity_threshold_ = RayLogLevel::INFO; std::string RayLog::app_name_ = ""; +std::string RayLog::log_dir_ = ""; #ifdef RAY_USE_GLOG using namespace google; @@ -117,14 +118,14 @@ void RayLog::StartRayLog(const std::string &app_name, RayLogLevel severity_thres } severity_threshold_ = severity_threshold; app_name_ = app_name; + log_dir_ = log_dir; #ifdef RAY_USE_GLOG int mapped_severity_threshold = GetMappedSeverity(severity_threshold_); - google::InitGoogleLogging(app_name_.c_str()); google::SetStderrLogging(mapped_severity_threshold); - // Enble log file if log_dir is not empty. - if (!log_dir.empty()) { - auto dir_ends_with_slash = log_dir; - if (log_dir[log_dir.length() - 1] != '/') { + // Enable log file if log_dir_ is not empty. + if (!log_dir_.empty()) { + auto dir_ends_with_slash = log_dir_; + if (log_dir_[log_dir_.length() - 1] != '/') { dir_ends_with_slash += "/"; } auto app_name_without_path = app_name; @@ -137,8 +138,13 @@ void RayLog::StartRayLog(const std::string &app_name, RayLogLevel severity_thres app_name_without_path = app_name.substr(pos + 1); } } + google::InitGoogleLogging(app_name_.c_str()); google::SetLogFilenameExtension(app_name_without_path.c_str()); - google::SetLogDestination(mapped_severity_threshold, log_dir.c_str()); + for (int i = static_cast(severity_threshold_); + i <= static_cast(RayLogLevel::FATAL); ++i) { + int level = GetMappedSeverity(static_cast(i)); + google::SetLogDestination(level, dir_ends_with_slash.c_str()); + } } #endif } @@ -162,7 +168,9 @@ void RayLog::UninstallSignalAction() { void RayLog::ShutDownRayLog() { #ifdef RAY_USE_GLOG UninstallSignalAction(); - google::ShutdownGoogleLogging(); + if (!log_dir_.empty()) { + google::ShutdownGoogleLogging(); + } #endif } diff --git a/src/ray/util/logging.h b/src/ray/util/logging.h index 1da3652a7..6f657142e 100644 --- a/src/ray/util/logging.h +++ b/src/ray/util/logging.h @@ -107,6 +107,9 @@ class RayLog : public RayLogBase { // In InitGoogleLogging, it simply keeps the pointer. // We need to make sure the app name passed to InitGoogleLogging exist. static std::string app_name_; + /// The directory where the log files are stored. + /// If this is empty, logs are printed to stdout. + static std::string log_dir_; protected: virtual std::ostream &Stream(); diff --git a/src/ray/util/logging_test.cc b/src/ray/util/logging_test.cc index 85fc5ee06..a514d6c3b 100644 --- a/src/ray/util/logging_test.cc +++ b/src/ray/util/logging_test.cc @@ -41,7 +41,7 @@ TEST(PrintLogTest, LogTestWithoutInit) { TEST(PrintLogTest, LogTestWithInit) { // Test empty app name. - RayLog::StartRayLog("", RayLogLevel::DEBUG); + RayLog::StartRayLog("", RayLogLevel::DEBUG, "/tmp/"); PrintLog(); RayLog::ShutDownRayLog(); }