Skip to content

Add log4rs to print log messages to a file #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
log.txt
/target
*.iml
138 changes: 135 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ lazy_static = "1.4"
syntect = "5.0"
textwrap = {version = "0.16", default-features = false, optional = false, features = ["unicode-width"]}
git-graph = "0.5.3"
log4rs = "1.2.0"
log = "0.4.18"
51 changes: 51 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,45 @@ fn main() {
});
}

fn setup_logger(log_level: &String) {
let level = match log_level.as_str() {
"error" => log::LevelFilter::Error,
"warn" => log::LevelFilter::Warn,
"info" => log::LevelFilter::Info,
"debug" => log::LevelFilter::Debug,
"trace" => log::LevelFilter::Trace,
"off" => log::LevelFilter::Off,
_ => log::LevelFilter::Off,
};

// Logging to log file.
let logfile = log4rs::append::file::FileAppender::builder()
// Pattern: https://docs.rs/log4rs/*/log4rs/encode/pattern/index.html
.encoder(Box::new(log4rs::encode::pattern::PatternEncoder::new(
"{l} - {m}\n",
)))
.build("log.txt")
.unwrap();

// Log Trace level output to file where trace is the default level
// and the programmatically specified level to stderr.
let config = log4rs::Config::builder()
.appender(log4rs::config::Appender::builder().build("logfile", Box::new(logfile)))
.build(
log4rs::config::Root::builder()
.appender("logfile")
.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)?;
let _handle = log4rs::init_config(config).unwrap();
}

fn from_args() -> Result<(), String> {
let app_dir = AppDirs::new(Some("git-graph"), false).unwrap().config_dir;
let mut models_dir = app_dir;
Expand Down Expand Up @@ -125,6 +164,13 @@ fn from_args() -> Result<(), String> {
.required(false)
.num_args(0),
)
.arg(
Arg::new("log-level")
.long("log-level")
.help("Output log messages to a file. Default off. One of [error|warn|info|debug|trace|off].")
.required(false)
.num_args(1),
)
.arg(
Arg::new("color")
.long("color")
Expand Down Expand Up @@ -285,6 +331,11 @@ fn from_args() -> Result<(), String> {
let include_remote = !matches.get_flag("local");

let compact = !matches.get_flag("sparse");
match matches.get_one::<String>("log-level") {
Some(log_level) => setup_logger(log_level),
None => {}
}

let style = matches
.get_one::<String>("style")
.map(|s| Characters::from_str(s))
Expand Down