Making custom Elsticsearch logger 2.

This commit is contained in:
Ara Sadoyan
2026-09-24 18:27:53 +02:00
parent d56968c4c3
commit 3fe037de5a
4 changed files with 75 additions and 46 deletions
+16 -30
View File
@@ -2,7 +2,7 @@ use crate::logging::types::sendlog;
use crate::utils::metrics::LOGGING_ERRORS;
use crate::utils::types::AppConfig;
use anyhow::Result;
use log::{error, info, warn, LevelFilter, Record};
use log::{error, info, log, Level, LevelFilter, Record};
use log4rs::append::rolling_file::policy::compound::roll::fixed_window::FixedWindowRoller;
use log4rs::append::rolling_file::policy::compound::trigger::size::SizeTrigger;
use log4rs::append::rolling_file::policy::compound::CompoundPolicy;
@@ -335,33 +335,19 @@ pub async fn access_log_receiver(mut receiver: mpsc::Receiver<LogMessage>) {
}
async fn write_access_log(msg: &LogMessage) {
match MatchStatus::from_code(msg.response_code) {
MatchStatus::Ok2xx => info!(
"{}, {}, {}, client: {}, version: {:?}, useragent: {}",
msg.response_code,
msg.cache_status.as_str(),
msg.summary,
msg.client_ip,
msg.version,
msg.user_agent,
),
MatchStatus::Er4xx => warn!(
"{}, {}, {}, client: {}, version: {:?}, useragent: {}",
msg.response_code,
msg.cache_status.as_str(),
msg.summary,
msg.client_ip,
msg.version,
msg.user_agent,
),
MatchStatus::Er5xx => error!(
"{}, {}, {}, client: {}, version: {:?}, useragent: {}",
msg.response_code,
msg.cache_status.as_str(),
msg.summary,
msg.client_ip,
msg.version,
msg.user_agent,
),
}
let level = match MatchStatus::from_code(msg.response_code) {
MatchStatus::Ok2xx => Level::Info,
MatchStatus::Er4xx => Level::Warn,
MatchStatus::Er5xx => Level::Error,
};
log!(
level,
"{}, {}, {}, client: {}, version: {:?}, useragent: {}",
msg.response_code,
msg.cache_status.as_str(),
msg.summary,
msg.client_ip,
msg.version,
msg.user_agent,
);
}
+55 -12
View File
@@ -6,32 +6,73 @@ use bytes::Bytes;
use std::env;
use std::sync::LazyLock;
pub struct ElasticSearchConfig {
pub hosts: &'static [&'static str],
static ELASTIC: LazyLock<ElasticSearchConfig> = LazyLock::new(ElasticSearchConfig::load_from_env);
#[derive(Debug)]
struct ElasticHost {
address: &'static str,
port: u16,
tls: bool,
url: &'static str,
}
struct ElasticSearchConfig {
pub hosts: &'static [ElasticHost],
pub user: &'static str,
pub password: &'static str,
}
impl ElasticSearchConfig {
pub fn load_from_env() -> Self {
let hosts: Vec<&'static str> = env::var("LOG_ELASTIC_HOSTS")
.unwrap_or_else(|_| "127.0.0.1".to_string())
.split(',')
.map(|s| -> &'static str { Box::leak(s.trim().to_string().into_boxed_str()) })
.collect();
let raw_hosts = env::var("LOG_ELASTIC_HOSTS").unwrap_or_else(|_| "http://127.0.0.1:9200".to_string());
let mut hosts_struct = Vec::new();
for entry in raw_hosts.split(',') {
let entry = entry.trim();
if entry.is_empty() {
continue;
}
let (scheme, rest) = if let Some(stripped) = entry.strip_prefix("https://") {
("https", stripped)
} else if let Some(stripped) = entry.strip_prefix("http://") {
("http", stripped)
} else {
("http", entry)
};
let (address, port) = match rest.split_once(':') {
Some((host_part, port_part)) => {
let port = port_part.parse::<u16>().unwrap_or(9200);
(host_part, port)
}
None => (rest, 9200),
};
let tls = scheme == "https";
let full_url = format!("{}://{}:{}", scheme, address, port);
hosts_struct.push(ElasticHost {
address: Box::leak(address.to_string().into_boxed_str()),
port,
tls,
url: Box::leak(full_url.into_boxed_str()), // Clean static URL
});
}
let user = env::var("LOG_ELASTIC_USER").unwrap_or_else(|_| "elastic".to_string());
let password = env::var("LOG_ELASTIC_PASSWORD").unwrap_or_else(|_| "elastic".to_string());
Self {
hosts: Box::leak(hosts.into_boxed_slice()),
hosts: Box::leak(hosts_struct.into_boxed_slice()),
user: Box::leak(user.into_boxed_str()),
password: Box::leak(password.into_boxed_str()),
}
}
}
pub static ELASTIC: LazyLock<ElasticSearchConfig> = LazyLock::new(ElasticSearchConfig::load_from_env);
pub struct ElasticSearch;
struct ElasticSearch;
#[async_trait]
impl WriteLog for ElasticSearch {
@@ -43,8 +84,10 @@ impl WriteLog for ElasticSearch {
return;
}
};
println!("{} => {} => {}:{:?}", ELASTIC.hosts[0], ELASTIC.hosts[1], ELASTIC.user, ELASTIC.password);
let _ = httpclient("POST", false, "127.0.0.1", "/i", "localhost", 8000, "http://127.0.0.1:8000/d".to_string(), payload).await;
if let Some(target) = ELASTIC.hosts.first() {
let _ = httpclient("POST", target.tls, target.address, "/i", target.address, target.port, target.url, payload).await;
}
}
}