toml
[dependencies]
tklog = "0.2.6"
tokio = "1.40"
log = "0.4"
结合系统自带的日志库
use std::{thread, time::Duration};
use tklog::{Format, LEVEL, LOG};
fn test_synclog() {
//init LOG
LOG.set_console(true)
.set_level(LEVEL::Debug)
.set_cutmode_by_size("logsize.log", 10000, 10, true)
.uselog(); //Enable the official log library
log::trace!("trace>>>>{}{}{}{}{}", "aaaa", 1, 2, 3, 4);
log::debug!("debug>>>>{}{}",1,2);
log::info!("info log");
log::warn!("warn log");
log::error!("error log");
thread::sleep(Duration::from_secs(1))
}
结合系统自带的日志库,异步
use std::{thread, time::Duration};
use tklog::{Format, LEVEL, ASYNC_LOG};
async fn test_synclog() {
//init ASYNC LOG
ASYNC_LOG.set_console(false)
.set_cutmode_by_size("asynclogsize.log", 10000, 10, true).await
.uselog(); //Enable the official log library
log::trace!("trace async log>>>>{}{}{}{}{}", "aaaaaaaaa", 1, 2, 3, 4);
log::debug!("debug async log>>>>{}{}",1,2);
log::info!("info async log");
log::warn!("warn async log");
log::error!("error async log");
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
}
异步,需结合tokio
use tklog::{
async_debug, async_error, async_fatal, async_info, async_trace, async_warn, LEVEL, Format, ASYNC_LOG
};
async fn async_log_init() {
// Configure global singleton
ASYNC_LOG
.set_console(false) // Disable console output
.set_level(LEVEL::Trace) // Set log level to Trace
.set_format(Format::LevelFlag | Format::Time | Format::ShortFileName) // Define structured logging output
.set_cutmode_by_size("tklog_async.txt", 10000, 10, false) // Rotate log files by size, every 10,000 bytes, with 10 backups
.await;
}
#[tokio::test]
async fn testlog() {
async_log_init().await;
async_trace!("trace>>>>", "aaaaaaa", 1, 2, 3);
async_debug!("debug>>>>", "aaaaaaa", 1, 2, 3);
async_info!("info>>>>", "bbbbbbbbb", 1, 2, 3);
async_warn!("warn>>>>", "cccccccccc", 1, 2, 3);
async_error!("error>>>>", "ddddddddddddd", 1, 2, 3);
async_fatal!("fatal>>>>", "eeeeeeeeeeeeee", 1, 2, 3);
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
}
多实例异步
use std::sync::Arc;
use tklog::{
async_debugs, async_errors, async_fatals, async_infos, async_traces, async_warns, LEVEL, Format, ASYNC_LOG, MODE
};
#[tokio::test]
async fn testmultilogs() {
let mut log = tklog::Async::Logger::new();
log.set_console(false)
.set_level(LEVEL::Debug)
.set_cutmode_by_time("tklogasync.log", MODE::DAY, 10, true)
.await
.set_formatter("{message} | {time} {file}{level}\n");
let mut logger = Arc::clone(&Arc::new(Mutex::new(log)));
let log = logger.borrow_mut();
async_traces!(log, "async_traces>>>>", "AAAAAAAAAA", 1, 2, 3);
async_debugs!(log, "async_debugs>>>>", "BBBBBBBBBB", 1, 2, 3);
async_infos!(log, "async_infos>>>>", "CCCCCCCCCC", 1, 2, 3);
async_warns!(log, "async_warns>>>>", "DDDDDDDDDD", 1, 2, 3);
async_errors!(log, "async_errors>>>>", "EEEEEEEEEEE", 1, 2, 3);
async_fatals!(log, "async_fatals>>>>", "FFFFFFFFFFFF", 1, 2, 3);
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
}