mirror of
https://github.com/starship/starship.git
synced 2024-11-25 09:43:36 +01:00
feat(logger): add option to disable logging to file
This commit is contained in:
parent
6fc4152624
commit
b6b98cfede
@ -65,6 +65,12 @@ Or for Cmd (Windows) would be adding this line to your `starship.lua`:
|
||||
os.setenv('STARSHIP_CACHE', 'C:\\Users\\user\\AppData\\Local\\Temp')
|
||||
```
|
||||
|
||||
To completely disable logging to a file, and only log to standard error, set the environment variable `STARSHIP_DISABLE_LOG_FILE`:
|
||||
|
||||
```sh
|
||||
export STARSHIP_DISABLE_LOG_FILE=1
|
||||
```
|
||||
|
||||
### Terminology
|
||||
|
||||
**Module**: A component in the prompt giving information based on contextual information from your OS. For example, the "nodejs" module shows the version of Node.js that is currently installed on your computer, if your current directory is a Node.js project.
|
||||
|
@ -19,6 +19,10 @@ pub struct StarshipLogger {
|
||||
log_level: Level,
|
||||
}
|
||||
|
||||
struct StarshipStderrLogger {
|
||||
log_level: Level,
|
||||
}
|
||||
|
||||
/// Returns the path to the log directory.
|
||||
pub fn get_log_dir() -> PathBuf {
|
||||
env::var_os("STARSHIP_CACHE")
|
||||
@ -32,6 +36,36 @@ pub fn get_log_dir() -> PathBuf {
|
||||
})
|
||||
}
|
||||
|
||||
/// Return the log level.
|
||||
fn get_log_level() -> Level {
|
||||
env::var("STARSHIP_LOG")
|
||||
.map(|level| match level.to_ascii_lowercase().as_str() {
|
||||
"trace" => Level::Trace,
|
||||
"debug" => Level::Debug,
|
||||
"info" => Level::Info,
|
||||
"warn" => Level::Warn,
|
||||
"error" => Level::Error,
|
||||
_ => Level::Warn,
|
||||
})
|
||||
.unwrap_or_else(|_| Level::Warn)
|
||||
}
|
||||
|
||||
/// Print a record to standard error.
|
||||
fn eprint_record(record: &Record) {
|
||||
eprintln!(
|
||||
"[{}] - ({}): {}",
|
||||
match record.level() {
|
||||
Level::Trace => Color::Blue.dimmed().paint(format!("{}", record.level())),
|
||||
Level::Debug => Color::Cyan.paint(format!("{}", record.level())),
|
||||
Level::Info => Color::White.paint(format!("{}", record.level())),
|
||||
Level::Warn => Color::Yellow.paint(format!("{}", record.level())),
|
||||
Level::Error => Color::Red.paint(format!("{}", record.level())),
|
||||
},
|
||||
record.module_path().unwrap_or_default(),
|
||||
record.args()
|
||||
);
|
||||
}
|
||||
|
||||
/// Deletes all log files in the log directory that were modified more than 24 hours ago.
|
||||
pub fn cleanup_log_files<P: AsRef<Path>>(path: P) {
|
||||
let log_dir = path.as_ref();
|
||||
@ -103,16 +137,7 @@ impl Default for StarshipLogger {
|
||||
),
|
||||
log_file: OnceCell::new(),
|
||||
log_file_path: session_log_file,
|
||||
log_level: env::var("STARSHIP_LOG")
|
||||
.map(|level| match level.to_ascii_lowercase().as_str() {
|
||||
"trace" => Level::Trace,
|
||||
"debug" => Level::Debug,
|
||||
"info" => Level::Info,
|
||||
"warn" => Level::Warn,
|
||||
"error" => Level::Error,
|
||||
_ => Level::Warn,
|
||||
})
|
||||
.unwrap_or_else(|_| Level::Warn),
|
||||
log_level: get_log_level(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -204,18 +229,7 @@ impl log::Log for StarshipLogger {
|
||||
}
|
||||
|
||||
// Print messages to stderr
|
||||
eprintln!(
|
||||
"[{}] - ({}): {}",
|
||||
match record.level() {
|
||||
Level::Trace => Color::Blue.dimmed().paint(format!("{}", record.level())),
|
||||
Level::Debug => Color::Cyan.paint(format!("{}", record.level())),
|
||||
Level::Info => Color::White.paint(format!("{}", record.level())),
|
||||
Level::Warn => Color::Yellow.paint(format!("{}", record.level())),
|
||||
Level::Error => Color::Red.paint(format!("{}", record.level())),
|
||||
},
|
||||
record.module_path().unwrap_or_default(),
|
||||
record.args()
|
||||
);
|
||||
eprint_record(record);
|
||||
|
||||
// Add to duplicate detection set
|
||||
if let Ok(mut c) = self.log_file_content.write() {
|
||||
@ -236,8 +250,40 @@ impl log::Log for StarshipLogger {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for StarshipStderrLogger {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
log_level: get_log_level(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl log::Log for StarshipStderrLogger {
|
||||
fn enabled(&self, metadata: &Metadata) -> bool {
|
||||
metadata.level() <= self.log_level
|
||||
}
|
||||
|
||||
fn log(&self, record: &Record) {
|
||||
// Early return if the log level is not enabled
|
||||
if !self.enabled(record.metadata()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Print messages to stderr
|
||||
eprint_record(record);
|
||||
}
|
||||
|
||||
fn flush(&self) {
|
||||
std::io::stderr().flush().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init() {
|
||||
log::set_boxed_logger(Box::<StarshipLogger>::default()).unwrap();
|
||||
if env::var_os("STARSHIP_DISABLE_LOG_FILE").is_some() {
|
||||
log::set_boxed_logger(Box::<StarshipStderrLogger>::default()).unwrap();
|
||||
} else {
|
||||
log::set_boxed_logger(Box::<StarshipLogger>::default()).unwrap();
|
||||
}
|
||||
log::set_max_level(LevelFilter::Trace);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user