From 538ba7a95bb5fb850c613c1495ad0391657f3811 Mon Sep 17 00:00:00 2001 From: vicanso Date: Tue, 15 Oct 2024 19:23:39 +0800 Subject: [PATCH] refactor: support more rolling type --- src/logger/mod.rs | 49 ++++++++++++++++++++++++++-------- src/util/mod.rs | 12 +++++++++ web/src/helpers/util.ts | 2 +- web/src/pages/Certificates.tsx | 2 +- web/src/pages/Locations.tsx | 2 +- web/src/pages/Servers.tsx | 2 +- web/src/pages/Upstreams.tsx | 2 +- 7 files changed, 55 insertions(+), 16 deletions(-) diff --git a/src/logger/mod.rs b/src/logger/mod.rs index 66717177..24974b16 100644 --- a/src/logger/mod.rs +++ b/src/logger/mod.rs @@ -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; @@ -59,20 +60,46 @@ pub fn logger_try_init(params: LoggerParams) -> Result<(), Box> { let writer = if params.file.is_empty() { BoxMakeWriter::new(std::io::stderr) } else { - let file = util::resolve_path(¶ms.file); + let mut file = util::resolve_path(¶ms.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 { diff --git a/src/util/mod.rs b/src/util/mod.rs index b43d1e88..088fe3d7 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -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; @@ -130,6 +131,17 @@ pub fn get_cookie_value<'a>( None } +/// Converts query string to map. +pub fn convert_query_map(query: &str) -> HashMap { + 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, diff --git a/web/src/helpers/util.ts b/web/src/helpers/util.ts index c103cf17..9827637a 100644 --- a/web/src/helpers/util.ts +++ b/web/src/helpers/util.ts @@ -65,7 +65,7 @@ export function random(length = 8) { export function formatLabel(label: string) { if (label === "*") { - return "NEW"; + return "New"; } return label; } diff --git a/web/src/pages/Certificates.tsx b/web/src/pages/Certificates.tsx index 9905e1aa..8640e64b 100644 --- a/web/src/pages/Certificates.tsx +++ b/web/src/pages/Certificates.tsx @@ -153,7 +153,7 @@ export default function Certificates() {

- {formatLabel(currentCertificate).toUpperCase()} + {formatLabel(currentCertificate)}

- {formatLabel(currentLocation).toUpperCase()} + {formatLabel(currentLocation)}

- {formatLabel(currentServer).toUpperCase()} + {formatLabel(currentServer)}

- {formatLabel(currentUpstream).toUpperCase()} + {formatLabel(currentUpstream)}