Skip to content

Commit 3a4b958

Browse files
committed
chore: allow using flag or env vars for both log level and file
1 parent ffddee2 commit 3a4b958

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ or have any ideas. Pull requests are warmly welcomed.
649649
650650
# Troubleshooting
651651
652-
To troubleshoot what's happening, you can set the environment variable `RUST_LOG` to either `debug` or even `trace`, and set `--log-file` to a path. You can then read those logs during or after the execution to better understand what's happening. Don't hesitate to add those logs to an issue if you need help.
652+
To troubleshoot what's happening, you can set the environment variable `SKIM_LOG` or the flag `--log-level` to either `debug` or even `trace`, and set the environment variable `SKIM_LOG_FILE` or the flag `--log-file` to a path. You can then read those logs during or after the execution to better understand what's happening. Don't hesitate to add those logs to an issue if you need help.
653653
654654
## No line feed issues with nix, FreeBSD, termux
655655

src/bin/main.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,17 @@ fn main() -> Result<()> {
3131
e.exit();
3232
});
3333
color_eyre::install()?;
34-
let log_target = if let Some(ref log_file) = opts.log_file {
35-
env_logger::Target::Pipe(Box::new(File::create(log_file).expect("Failed to create log file")))
36-
} else {
37-
env_logger::Target::Stdout
38-
};
39-
env_logger::builder()
40-
.target(log_target)
41-
.format(|buf, record| {
34+
{
35+
let target = if let Some(ref log_file) = opts.log_file.as_ref().or(std::env::var("SKIM_LOG_FILE").ok().as_ref())
36+
{
37+
env_logger::Target::Pipe(Box::new(File::create(log_file).expect("Failed to create log file")))
38+
} else {
39+
env_logger::Target::Stdout
40+
};
41+
42+
let env_var = "SKIM_LOG";
43+
44+
let format = |buf: &mut env_logger::fmt::Formatter, record: &log::Record<'_>| {
4245
writeln!(
4346
buf,
4447
"[{} {} {} ({}:{})] [{}/{:?}] {}",
@@ -51,8 +54,23 @@ fn main() -> Result<()> {
5154
std::thread::current().id(),
5255
record.args()
5356
)
54-
})
55-
.init();
57+
};
58+
59+
if let Some(level) = opts.log_level {
60+
env_logger::builder()
61+
.filter_level(level)
62+
.parse_env(env_var)
63+
.target(target)
64+
.format(format)
65+
.init();
66+
} else {
67+
env_logger::builder()
68+
.parse_env(env_var)
69+
.target(target)
70+
.format(format)
71+
.init();
72+
};
73+
}
5674
// Build the options after setting the log target
5775
opts = opts.build();
5876
trace!("Command line: {:?}", std::env::args());

src/options.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,10 @@ pub struct SkimOptions {
757757
#[cfg_attr(feature = "cli", arg(long, verbatim_doc_comment, help_heading = "Display", default_missing_value = "center,50%", num_args=0..))]
758758
pub tmux: Option<String>,
759759

760+
/// Set the log level
761+
#[cfg_attr(feature = "cli", arg(long, help_heading = "Scripting"))]
762+
pub log_level: Option<log::LevelFilter>,
763+
760764
/// Pipe log output to a file
761765
#[cfg_attr(feature = "cli", arg(long, help_heading = "Scripting"))]
762766
pub log_file: Option<String>,
@@ -1100,6 +1104,7 @@ impl Default for SkimOptions {
11001104
#[cfg(feature = "cli")]
11011105
shell_bindings: false,
11021106
flags: Default::default(),
1107+
log_level: Default::default(),
11031108
}
11041109
}
11051110
}

0 commit comments

Comments
 (0)