MacOS统一日志与oslog库

huangapple go评论63阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月27日 00:14:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573306.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定