Skip to content

Commit 77a482e

Browse files
committed
feat: 修改日志路径
1 parent ac984c0 commit 77a482e

File tree

5 files changed

+32
-5
lines changed

5 files changed

+32
-5
lines changed

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ RUN set -ex \
3838
&& make \
3939
&& make install \
4040
&& ln -sf /etc/nginx/sbin/nginx /usr/local/bin/nginx \
41-
&& mkdir -p /var/log/nginx \
42-
&& ln -sf /dev/stdout /var/log/nginx/access.log \
43-
&& ln -sf /dev/stderr /var/log/nginx/error.log
41+
&& ln -sf /dev/stdout /etc/nginx/logs/access.log \
42+
&& ln -sf /dev/stderr /etc/nginx/logs/error.log \
43+
&& mkdir -p /tmp/polaris
4444

4545
CMD ["nginx", "-g", "daemon off;", "-c", "/etc/nginx/conf/nginx.conf"]

build/build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ folder_name=$(echo "nginx-gateway-release-${version}.${build_os}.${build_arch}"
3333
echo "target name $folder_name"
3434
rm -rf ../$folder_name
3535
mkdir -p ../$folder_name
36+
mkdir -p /tmp/polaris
3637

3738
pushd "$ngx_file_name"/
3839
chmod +x configure

source/nginx_polaris_limit_module/ngx_http_polaris_limit_module.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//
1313

1414
#include "ngx_http_polaris_limit_module.h"
15+
#include "polaris/log.h"
1516

1617
typedef struct {
1718
ngx_int_t enable; // 是否启用限流
@@ -420,8 +421,32 @@ const std::string defaultConfigContent = R"##(
420421
service: polaris.limiter
421422
)##";
422423

424+
const char kPathSeparator =
425+
#ifdef _WIN32
426+
'\\';
427+
#else
428+
'/';
429+
#endif
430+
431+
std::string resolveNgxLogDir(ngx_log_t *logger) {
432+
ngx_str_t* filename = &(logger->file->name);
433+
std::string filename_str = std::string(reinterpret_cast<char *>(filename->data), filename->len);
434+
int pos = filename_str.find_last_of(kPathSeparator);
435+
if (pos == -1) {
436+
return std::string("");
437+
}
438+
return filename_str.substr(0, pos);
439+
}
440+
423441
void LimitApiWrapper::Init(ngx_log_t *logger) {
424442
ngx_log_error(NGX_LOG_NOTICE, logger, 0, "[PolarisRateLimiting] start to init polaris limit api, polaris config %s", m_polaris_config.c_str());
443+
std::string logDir = resolveNgxLogDir(logger);
444+
if (logDir.size() == 0) {
445+
logDir = DEFAULT_POLARIS_LOG_DIR;
446+
}
447+
ngx_log_error(NGX_LOG_NOTICE, logger, 0, "[PolarisRateLimiting] polaris logDir: %s", logDir.c_str());
448+
polaris::SetLogDir(logDir);
449+
425450
std::string err_msg("");
426451
m_limit = polaris::LimitApi::CreateFromString(m_polaris_config, err_msg);
427452
if (NULL == m_limit) {

source/nginx_polaris_limit_module/ngx_http_polaris_limit_module.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ static const std::string LABEL_KEY_HEADER = "$header.";
4848
static const std::string LABEL_KEY_QUERY = "$query.";
4949
static const std::string LABEL_KEY_CALLER_IP = "$caller_ip";
5050
static const std::string PATH_SBIN = "sbin";
51+
static const std::string DEFAULT_POLARIS_LOG_DIR = "/tmp/polaris";
5152

5253
class LimitApiWrapper {
5354
public:

third_party/nginx/nginx.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ worker_processes 1;
44

55
#error_log logs/error.log;
66
#error_log logs/error.log notice;
7-
error_log /var/log/nginx/error.log info;
7+
error_log logs/error.log info;
88

99
#pid logs/nginx.pid;
1010

@@ -22,7 +22,7 @@ http {
2222
'$status $body_bytes_sent "$http_referer" '
2323
'"$http_user_agent" "$http_x_forwarded_for"';
2424

25-
access_log /var/log/nginx/access.log main;
25+
access_log logs/access.log main;
2626

2727
sendfile on;
2828
#tcp_nopush on;

0 commit comments

Comments
 (0)