Skip to content

Commit

Permalink
feat: support alias for log format layout
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Mar 24, 2024
1 parent d324e7f commit 14e2b0e
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 24 deletions.
40 changes: 20 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ readme = "./README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-trait = "0.1.78"
async-trait = "0.1.79"
base64 = "0.22.0"
bytes = "1.6.0"
bytesize = "1.3.0"
Expand Down
3 changes: 2 additions & 1 deletion conf/pingap.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ addr = "0.0.0.0:6188"
# tls_key = ""

# Access log format layout for the server. Default `None`
access_log = '"{method} {uri} {proto}" {status} {size-human} "{referer}" "{user-agent}" {latency-human}'
# access_log = '"{method} {uri} {proto}" {status} {size-human} "{referer}" "{user-agent}" {latency-human}'
access_log = "tiny"

# Location list for the server, item should be the name of location.
# The locations should be served by the server.
Expand Down
29 changes: 28 additions & 1 deletion docs/log_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,31 @@
description: Pingap 请求日志格式化
---

Pingap格式化相关参数,具体参数说明如下:
Pingap格式化可以使用以下几种默认形式`combined``common``short`以及`tiny`。也可自定义完整的日志格式化输出,具体参数说明如下:

- `{host}`: 请求的host属性
- `{method}`: 请求的method
- `{path}`: 请求的路径
- `{proto}`: 协议类型,`HTTP/1.1``HTTP/2.0`
- `{query}`: 请求的querystring
- `{remote}`: 请求的源ip
- `{client-ip}`: 客户ip,根据`x-forwarded-for`中获取
- `{scheme}`:
- `{uri}`: 请求的完整地址
- `{referer}`: 请求头中的referer
- `{user-agent}`: 请求的user-agent
- `{when}`: 日志的输出时间
- `{when-utc-iso}`: 日志的输出的utc时间
- `{when-unix}`: 日志的输出时间,格式为时间戳
- `{size}`: 响应数据的字节数
- `{size-human}`: 响应数据的大小,按数据大小格式化字符串
- `{status}`: 响应状态码
- `{latency}`: 响应时间的ms
- `{latency-human}`: 响应时间,按时间格式化
- `{payload-size}`: 请求数据的字节大小
- `{payload-size-human}`: 请求数据的大小,按数据大小格式化字符串
- `{~cookiename}`: 从cookie中获取对应的值
- `{>requestHeaderName}`: 请求头中获取对应的值
- `{<responseHeaderName}`: 响应头中获取对应的值
- `{:contentName}`: 从context中获取对应的值,暂时仅支持以下属性:`reused`, `upstream-address`, `processing`
- `{$envName}`: 从环境变量中获取对应的值,仅启动时获取
1 change: 1 addition & 0 deletions src/cache/http_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Result<T, E = Error> = std::result::Result<T, E>;

pub type HttpHeader = (HeaderName, HeaderValue);

/// Converts string slice to http headers
pub fn convert_headers(header_values: &[String]) -> Result<Vec<HttpHeader>> {
let mut arr = vec![];
for item in header_values {
Expand Down
8 changes: 8 additions & 0 deletions src/cache/http_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@ use std::time::{SystemTime, UNIX_EPOCH};

#[derive(Default, Debug, Clone)]
pub struct HttpResponse {
// http response status
pub status: StatusCode,
// http response body
pub body: Bytes,
// max age of http response
pub max_age: Option<u32>,
// created time of http response
pub created_at: Option<u64>,
// private for cache control
pub private: Option<bool>,
// headers for http response
pub headers: Option<Vec<HttpHeader>>,
}

impl HttpResponse {
/// Gets the response header from http response
pub fn get_response_header(&self) -> pingora::Result<ResponseHeader> {
let fix_size = 3;
let size = self
Expand Down Expand Up @@ -60,6 +67,7 @@ impl HttpResponse {
}
Ok(resp)
}
/// Sends http response to client
pub async fn send(self, session: &mut Session) -> pingora::Result<usize> {
let header = self.get_response_header()?;
let size = self.body.len();
Expand Down
13 changes: 13 additions & 0 deletions src/proxy/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,21 @@ fn format_extra_tag(key: &str) -> Option<Tag> {
}
}

static COMBINED: &str =
r###"{remote} "{method} {uri} {proto}" {status} {size-human} "{referer}" "{userAgent}""###;
static COMMON: &str = r###"{remote} "{method} {uri} {proto}" {status} {size-human}""###;
static SHORT: &str = r###"{remote} {method} {uri} {proto} {status} {size-human} - {latency}ms"###;
static TINY: &str = r###"{method} {uri} {status} {size-human} - {latency}ms"###;

impl From<&str> for Parser {
fn from(value: &str) -> Self {
let value = match value {
"combined" => COMBINED,
"common" => COMMON,
"short" => SHORT,
"tiny" => TINY,
_ => value,
};
let reg = Regex::new(r"(\{[a-zA-Z_<>\-~:$]+*\})").unwrap();
let mut current = 0;
let mut end = 0;
Expand Down
1 change: 0 additions & 1 deletion src/proxy/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ impl ProxyHttp for Server {
return Ok(true);
}
}

Ok(false)
}
async fn upstream_peer(
Expand Down

0 comments on commit 14e2b0e

Please sign in to comment.