Skip to content

Commit

Permalink
make log dir configurable and make log name compatible with RocketMQ …
Browse files Browse the repository at this point in the history
…main project (#401)
  • Loading branch information
humkum authored Aug 8, 2022
1 parent 215771d commit c64df6a
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ declare fname_boost="boost*.tar.gz"
declare fname_openssl_down="openssl-1.1.1d.tar.gz"
declare fname_libevent_down="release-2.1.11-stable.zip"
declare fname_jsoncpp_down="0.10.7.zip"
declare fname_boost_down="1.58.0/boost_1_58_0.tar.gz"
declare fname_boost_down="1.78.0/boost_1_78_0.tar.gz"

PrintParams() {
echo "=========================================one key build help============================================"
Expand Down
2 changes: 1 addition & 1 deletion src/common/ByteOrder.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <boost/detail/endian.hpp>
#include <boost/endian/arithmetic.hpp>
#include "RocketMQClient.h"
#include "UtilAll.h"
//==============================================================================
Expand Down
1 change: 1 addition & 0 deletions src/common/UtilAll.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const string DEFAULT_CLIENT_KEY_PASSWD = null;
const string DEFAULT_CLIENT_KEY_FILE = "/etc/rocketmq/client.key";
const string DEFAULT_CLIENT_CERT_FILE = "/etc/rocketmq/client.pem";
const string DEFAULT_CA_CERT_FILE = "/etc/rocketmq/ca.pem";
const string ROCKETMQ_CLIENT_LOG_DIR = "ROCKETMQ_CLIENT_LOG_DIR";
const string WS_ADDR =
"please set nameserver domain by setDomainName, there is no default "
"nameserver domain";
Expand Down
1 change: 1 addition & 0 deletions src/extern/CProducer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ int SetProducerLogPath(CProducer* producer, const char* logPath) {
if (producer == NULL) {
return NULL_POINTER;
}
setenv(ROCKETMQ_CLIENT_LOG_DIR.c_str(), logPath, 1);
return OK;
}

Expand Down
2 changes: 2 additions & 0 deletions src/extern/CPullConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "CMessageExt.h"
#include "DefaultMQPullConsumer.h"
#include "MQClientErrorContainer.h"
#include "Logging.h"

using namespace rocketmq;
using namespace std;
Expand Down Expand Up @@ -114,6 +115,7 @@ int SetPullConsumerLogPath(CPullConsumer* consumer, const char* logPath) {
}
// Todo, This api should be implemented by core api.
//((DefaultMQPullConsumer *) consumer)->setInstanceName(instanceName);
setenv(ROCKETMQ_CLIENT_LOG_DIR.c_str(), logPath, 1);
return OK;
}

Expand Down
2 changes: 2 additions & 0 deletions src/extern/CPushConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "CMessageExt.h"
#include "DefaultMQPushConsumer.h"
#include "MQClientErrorContainer.h"
#include "Logging.h"

using namespace rocketmq;
using namespace std;
Expand Down Expand Up @@ -278,6 +279,7 @@ int SetPushConsumerLogPath(CPushConsumer* consumer, const char* logPath) {
}
// Todo, This api should be implemented by core api.
//((DefaultMQPushConsumer *) consumer)->setInstanceName(instanceName);
setenv(ROCKETMQ_CLIENT_LOG_DIR.c_str(), logPath, 1);
return OK;
}

Expand Down
25 changes: 21 additions & 4 deletions src/log/Logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,19 @@ logAdapter* logAdapter::getLogInstance() {
}

logAdapter::logAdapter() : m_logLevel(eLOG_LEVEL_INFO) {
setLogDir();
string homeDir(UtilAll::getHomeDirectory());
homeDir.append("/logs/rocketmq-cpp/");
homeDir.append(m_log_dir);
m_logFile += homeDir;
std::string fileName = UtilAll::to_string(getpid()) + "_" + "rocketmq-cpp.log.%N";
std::string fileName = "rocketmq_client.log";
m_logFile += fileName;

// boost::log::expressions::attr<
// boost::log::attributes::current_thread_id::value_type>("ThreadID");
boost::log::register_simple_formatter_factory<boost::log::trivial::severity_level, char>("Severity");
m_logSink = logging::add_file_log(keywords::file_name = m_logFile, keywords::rotation_size = 100 * 1024 * 1024,
m_logSink = logging::add_file_log(keywords::file_name = m_logFile,
keywords::target_file_name = "rocketmq_client_%Y%m%d-%N.log",
keywords::rotation_size = 100 * 1024 * 1024,
keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 0, 0),
keywords::format = "[%TimeStamp%](%Severity%):%Message%",
keywords::min_free_space = 300 * 1024 * 1024, keywords::target = homeDir,
Expand Down Expand Up @@ -99,9 +102,23 @@ elogLevel logAdapter::getLogLevel() {
return m_logLevel;
}

void logAdapter::setLogDir() {
char* p = nullptr;
if ((p = getenv(ROCKETMQ_CLIENT_LOG_DIR.c_str()))) {
m_log_dir = p;
}
if (!m_log_dir.empty()) {
if (m_log_dir[m_log_dir.length() - 1] != '/') {
m_log_dir += '/';
}
} else {
m_log_dir = "/logs/rocketmq-client/";
}
}

void logAdapter::setLogFileNumAndSize(int logNum, int sizeOfPerFile) {
string homeDir(UtilAll::getHomeDirectory());
homeDir.append("/logs/rocketmq-cpp/");
homeDir.append(m_log_dir);
m_logSink->locked_backend()->set_file_collector(sinks::file::make_collector(
keywords::target = homeDir, keywords::max_size = logNum * sizeOfPerFile * 1024 * 1024));
}
Expand Down
2 changes: 2 additions & 0 deletions src/log/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class logAdapter {
public:
~logAdapter();
static logAdapter* getLogInstance();
void setLogDir();
void setLogLevel(elogLevel logLevel);
elogLevel getLogLevel();
void setLogFileNumAndSize(int logNum, int sizeOfPerFile);
Expand All @@ -60,6 +61,7 @@ class logAdapter {
boost::shared_ptr<logSink_t> m_logSink;
static logAdapter* alogInstance;
static boost::mutex m_imtx;
std::string m_log_dir;
};

#define ALOG_ADAPTER logAdapter::getLogInstance()
Expand Down

0 comments on commit c64df6a

Please sign in to comment.