-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
966 additions
and
969 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// All log macros and common types are under `spdlog::prelude` module. | ||
use spdlog::prelude::*; | ||
|
||
fn main() { | ||
// Writes a log at "info" level with the info level, and this log will be | ||
// processed by the global default logger - It will be output to `stdout`. | ||
info!("program started"); | ||
|
||
let file = "config.json"; | ||
|
||
// They will be output to `stderr`. | ||
error!("failed to open file: {}", file); | ||
warn!("undetermined locale, defaults to `en_US.UTF-8`"); | ||
|
||
// Level "trace" and "debug" will be ignored by default, you can modify the | ||
// level filter of the global default logger to enable all levels. | ||
spdlog::default_logger().set_level_filter(LevelFilter::All); | ||
|
||
trace!("position x: {}, y: {}", 11.4, -5.14); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use std::{env, sync::Arc}; | ||
|
||
use spdlog::{ | ||
prelude::*, | ||
sink::{FileSink, RotatingFileSink, RotationPolicy}, | ||
}; | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
configure_file_logger()?; | ||
configure_rotating_file_logger()?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn configure_file_logger() -> Result<(), Box<dyn std::error::Error>> { | ||
let path = env::current_exe()?.with_file_name("file.log"); | ||
|
||
let file_sink = Arc::new(FileSink::builder().path(path).build()?); | ||
let new_logger = Arc::new(Logger::builder().sink(file_sink).build()?); | ||
spdlog::set_default_logger(new_logger); | ||
|
||
info!("this log will be written to the file `all.log`"); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn configure_rotating_file_logger() -> Result<(), Box<dyn std::error::Error>> { | ||
let path = env::current_exe()?.with_file_name("rotating.log"); | ||
|
||
let file_sink = Arc::new( | ||
RotatingFileSink::builder() | ||
.base_path(path) | ||
.rotation_policy(RotationPolicy::Daily { hour: 0, minute: 0 }) | ||
.build()?, | ||
); | ||
let new_logger = Arc::new(Logger::builder().sink(file_sink).build()?); | ||
spdlog::set_default_logger(new_logger); | ||
|
||
info!("this log will be written to the file `rotating.log`, and the file will be rotated daily at 00:00"); | ||
|
||
Ok(()) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use std::{env, sync::Arc, time::Duration}; | ||
|
||
use spdlog::{ | ||
prelude::*, | ||
sink::{FileSink, Sink}, | ||
}; | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
// `spdlog-rs` has a global default logger and logs will be processed by it | ||
// by default, You can configure it. | ||
let default_logger = spdlog::default_logger(); | ||
default_logger.set_level_filter(LevelFilter::All); | ||
|
||
// Or completely replace it with a new one. | ||
let path = env::current_exe()?.with_file_name("all.log"); | ||
let file_sink = Arc::new(FileSink::builder().path(path).build()?); | ||
|
||
let new_logger = Arc::new( | ||
Logger::builder() | ||
.level_filter(LevelFilter::All) | ||
.flush_level_filter(LevelFilter::MoreSevereEqual(Level::Warn)) | ||
.sink(file_sink.clone()) | ||
.build()?, | ||
); | ||
new_logger.set_flush_period(Some(Duration::from_secs(3))); | ||
spdlog::set_default_logger(new_logger); | ||
|
||
info!("this log will be written to the file `all.log`"); | ||
|
||
// In addition to having the global default logger, more loggers are allowed to | ||
// be configured, stored and used independently. | ||
let db = AppDatabase::new(file_sink)?; | ||
db.write_i32(114514); | ||
|
||
Ok(()) | ||
} | ||
|
||
struct AppDatabase { | ||
logger: Logger, | ||
} | ||
|
||
impl AppDatabase { | ||
fn new(all_log_sink: Arc<dyn Sink>) -> Result<Self, Box<dyn std::error::Error>> { | ||
let path = env::current_exe()?.with_file_name("db.log"); | ||
let db_file_sink = Arc::new(FileSink::builder().path(path).build()?); | ||
|
||
let logger = Logger::builder() | ||
.name("database") | ||
.level_filter(LevelFilter::All) | ||
.flush_level_filter(LevelFilter::MoreSevereEqual(Level::Warn)) | ||
.sinks([all_log_sink, db_file_sink]) | ||
.build()?; | ||
Ok(Self { logger }) | ||
} | ||
|
||
fn write_i32(&self, value: i32) { | ||
// This log will be written to both files `all.log` and `db.log`. | ||
trace!(logger: self.logger, "writing value {} to the database", value); | ||
} | ||
} |
Oops, something went wrong.