Skip to content

Commit

Permalink
refactor: support more rolling type
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Oct 15, 2024
1 parent 2b917cf commit 538ba7a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 16 deletions.
49 changes: 38 additions & 11 deletions src/logger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use crate::util;
use crate::util::convert_query_map;
use std::error::Error;
use std::fs;
use std::io;
Expand Down Expand Up @@ -59,20 +60,46 @@ pub fn logger_try_init(params: LoggerParams) -> Result<(), Box<dyn Error>> {
let writer = if params.file.is_empty() {
BoxMakeWriter::new(std::io::stderr)
} else {
let file = util::resolve_path(&params.file);
let mut file = util::resolve_path(&params.file);
let mut rolling_type = "".to_string();
if let Some((_, query)) = params.file.split_once('?') {
file = file.replace(&format!("?{query}"), "");
let m = convert_query_map(query);
if let Some(value) = m.get("rolling") {
rolling_type = value.to_string();
}
}

let filepath = Path::new(&file);
let dir = filepath.parent().ok_or_else(|| {
io::Error::new(
io::ErrorKind::Other,
"parent of file log is invalid",
)
})?;
let dir = if filepath.is_dir() {
filepath
} else {
filepath.parent().ok_or_else(|| {
io::Error::new(
io::ErrorKind::Other,
"parent of file log is invalid",
)
})?
};
fs::create_dir_all(dir)?;
let filename = filepath.file_name().ok_or_else(|| {
io::Error::new(io::ErrorKind::Other, "file log is invalid")
})?;
let filename = if filepath.is_dir() {
"".to_string()
} else {
filepath
.file_name()
.ok_or_else(|| {
io::Error::new(io::ErrorKind::Other, "file log is invalid")
})?
.to_string_lossy()
.to_string()
};
let file_appender = match rolling_type.as_str() {
"minutely" => tracing_appender::rolling::minutely(dir, filename),
"hourly" => tracing_appender::rolling::hourly(dir, filename),
"never" => tracing_appender::rolling::never(dir, filename),
_ => tracing_appender::rolling::daily(dir, filename),
};

let file_appender = tracing_appender::rolling::daily(dir, filename);
if params.capacity < 4096 {
BoxMakeWriter::new(file_appender)
} else {
Expand Down
12 changes: 12 additions & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use path_absolutize::*;
use pingora::cache::CacheKey;
use pingora::tls::ssl::SslVersion;
use pingora::{http::RequestHeader, proxy::Session};
use std::collections::HashMap;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::{path::Path, str::FromStr};
use substring::Substring;
Expand Down Expand Up @@ -130,6 +131,17 @@ pub fn get_cookie_value<'a>(
None
}

/// Converts query string to map.
pub fn convert_query_map(query: &str) -> HashMap<String, String> {
let mut m = HashMap::new();
for item in query.split('&') {
if let Some((key, value)) = item.split_once('=') {
m.insert(key.to_string(), value.to_string());
}
}
m
}

/// Gets query value from req header.
pub fn get_query_value<'a>(
req_header: &'a RequestHeader,
Expand Down
2 changes: 1 addition & 1 deletion web/src/helpers/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function random(length = 8) {

export function formatLabel(label: string) {
if (label === "*") {
return "NEW";
return "New";
}
return label;
}
2 changes: 1 addition & 1 deletion web/src/pages/Certificates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export default function Certificates() {
<div className="grow lg:border-l overflow-auto p-4">
<h2 className="h-8 mb-1">
<span className="border-b-2 border-solid p-1 border-[rgb(var(--foreground-rgb))]">
{formatLabel(currentCertificate).toUpperCase()}
{formatLabel(currentCertificate)}
</span>
</h2>
<ExForm
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/Locations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export default function Locations() {
<div className="grow lg:border-l overflow-auto p-4">
<h2 className="h-8 mb-1">
<span className="border-b-2 border-solid p-1 border-[rgb(var(--foreground-rgb))]">
{formatLabel(currentLocation).toUpperCase()}
{formatLabel(currentLocation)}
</span>
</h2>
<ExForm
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/Servers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ export default function Servers() {
<div className="grow lg:border-l overflow-auto p-4">
<h2 className="h-8 mb-1">
<span className="border-b-2 border-solid p-1 border-[rgb(var(--foreground-rgb))]">
{formatLabel(currentServer).toUpperCase()}
{formatLabel(currentServer)}
</span>
</h2>
<ExForm
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/Upstreams.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export default function Upstreams() {
<div className="grow lg:border-l overflow-auto p-4">
<h2 className="h-8 mb-1">
<span className="border-b-2 border-solid p-1 border-[rgb(var(--foreground-rgb))]">
{formatLabel(currentUpstream).toUpperCase()}
{formatLabel(currentUpstream)}
</span>
</h2>
<ExForm
Expand Down

0 comments on commit 538ba7a

Please sign in to comment.