英文:
MacOS unified log with oslog crate
问题
I'd like to write to Apple's unified log on macOS from Rust.
I found the oslog crate which seems promising and I have the following code so far:
#[macro_use]
extern crate log;
use oslog::OsLogger;
fn main() {
//env_logger::init();
OsLogger::new("com.rust")
//.level_filter(LevelFilter::Trace)
.init()
.unwrap();
info!("starting up");
}
If I use the commented env_logger
I can see the logs show up in Terminal, but with OSLogger - nothing shows up in the Console.app on macOS or if I stream the log like so:
log stream --predicate 'subsystem == "com.rust"'
Anyone here have done this? Maybe I'm missing something obvious, no experience with Rust yet.
英文:
I'd like to write to Apple's unified log on macOS from Rust.
I found the oslog crate which seems promising and I have the following code so far:
#[macro_use]
extern crate log;
use oslog::OsLogger;
fn main() {
//env_logger::init();
OsLogger::new("com.rust")
//.level_filter(LevelFilter::Trace)
.init()
.unwrap();
info!("starting up");
}
If I use the commented env_logger
I can see the logs show up in Terminal, but with OSLogger - nothing shows up in the Console.app on macOS or if I stream the log like so:
log stream --predicate 'subsystem == "com.rust"'
Anyone here have done this? Maybe I'm missing something obvious, no experience with Rust yet.
答案1
得分: 1
First of all, you have to set an environment variable that enables logging when running the program. The value sets the level of log filtering to apply:
首先,您需要设置一个环境变量,以在运行程序时启用日志记录。该值设置要应用的日志筛选级别:
$ RUST_LOG=trace ./myrustprogram
Secondly, you have to set matching log levels when creating the oslog facade via level_filter(...)
:
其次,在创建oslog门面时,您需要设置匹配的日志级别,通过level_filter(...)
函数:
#[macro_use]
extern crate log;
use oslog::OsLogger;
use log::LevelFilter;
fn main() {
OsLogger::new("com.rust")
.level_filter(LevelFilter::Info)
.init()
.unwrap();
info!("Hello from Rust: true");
info!("Velocity: 2");
}
This writes to the unified log and can be observed via Console.app on macOS.
这将写入统一日志,可以通过macOS上的Console.app观察到。
英文:
Ok, so there were 2 parts to solving this and I'm posting the full code below.
First of all, you have to set an environment variable that enables logging when running the program. The value sets the level of log filtering to apply:
$ RUST_LOG=trace ./myrustprogram
Secondly, you have to set matching log levels when creating the oslog facade via level_filter(...)
:
#[macro_use]
extern crate log;
use oslog::OsLogger;
use log::LevelFilter;
fn main() {
OsLogger::new("com.rust")
.level_filter(LevelFilter::Info)
.init()
.unwrap();
info!("Hello from Rust: true");
info!("Velocity: 2");
}
This writes to the unified log and can be observed via Console.app on macOS
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论