use std::{thread, time::Duration}; use async_httpc::{AsyncHttpc, AsyncHttpcBuilder}; use log::{debug, error, info, trace, warn, LevelFilter, SetLoggerError}; use log4rs::{ append::console::{ConsoleAppender, Target}, config::{Appender, Config, Logger, Root}, encode::pattern::PatternEncoder, filter::threshold::ThresholdFilter, }; use pi_logger::{ ali_sls::SLSAppender, encode_json::JsonEncoder, encode_json_str::JsonStrEncoder, http::{http_request, HttpAppender}, }; use r#async::rt::multi_thread::{MultiTaskRuntime, MultiTaskRuntimeBuilder, StealableTaskPool}; fn rt_fn() -> MultiTaskRuntime<()> { let pool = StealableTaskPool::with(8, 8); let builder = MultiTaskRuntimeBuilder::new(pool) .thread_prefix("PI-SERV-FILE") .thread_stack_size(2 * 1024 * 1024) .init_worker_size(8) .set_worker_limit(8, 8) .set_timeout(10) .set_timer_interval(1); builder.build() } fn httpc() -> AsyncHttpc { AsyncHttpcBuilder::new() // .bind_address("127.0.0.1") .set_default_user_agent("piServHttpClient") .add_default_header("Accept", "*/*") .add_default_header("Accept-Encoding", "gzip, br") .add_default_header("Connection", "keep-alive") .enable_cookie(true) .enable_gzip(true) .enable_auto_referer(true) .enable_strict(false) .set_redirect_limit(10) .set_connect_timeout(5000) .set_request_timeout(10000) .set_connection_timeout(Some(10000)) .set_host_connection_limit(10) .build() .unwrap() } fn main() -> Result<(), SetLoggerError> { let level = log::LevelFilter::Info; let rt = rt_fn(); // Build a stderr logger. let stdout = ConsoleAppender::builder().target(Target::Stdout).build(); // 处理pi_pt及项目日志 let elk_js = SLSAppender::builder() .encoder(Box::new(JsonStrEncoder::new())) .build( "https://gcwl-logs.cn-chengdu.log.aliyuncs.com/logstores/logs/track".to_string(), rt.clone(), httpc(), ) .unwrap(); // 上层日志处理 let elk_appender_js = Appender::builder().build("elk_js", Box::new(elk_js)); // Log Trace level output to file where trace is the default level // and the programmatically specified level to stderr. let config = Config::builder() .appender(elk_appender_js) .appender( Appender::builder() .filter(Box::new(ThresholdFilter::new(level))) .build("stdout", Box::new(stdout)), ) .logger( Logger::builder() .appender("elk_js") .appender("stdout") .additive(false) .build("log_to_file::a", level), ) .build( Root::builder() .appender("stdout") .build(level), ) .unwrap(); // Use this to change log levels at runtime. // This means you can change the default log level to trace // if you are trying to debug an issue and need more logs on then turn it off // once you are done. let _handle = log4rs::init_config(config)?; // warn!("Goes to stderr and file"); // info!("Goes to stderr and file"); // debug!("Goes to file only"); // trace!("Goes to file only"); // 模拟js层日志打印 a::foo(); thread::sleep(Duration::from_millis(10000000000)); Ok(()) } mod a { use log::{error, info, warn}; pub fn foo() { let data = r#"{"__logs__": [{"message": "rust test!!!!!!!!!!!!!!!!!"}]}"#; error!("{}", data); } }