Apple系统日志时间(以毫秒为单位)?

And*_*can 6 macos logging ios

NSLog()使用毫秒级分辨率的时间戳,但是它是一个钝器,因为它的所有日志消息都处于警告级别.

Apple System Log是一个更细粒度的系统,有8个不同的级别.但是......它的时间戳只有1秒的分辨率.我阅读了有关时间格式的手册页,但它们似乎都只是第二种.

很明显,这些信息是可用的,至少对NSLog来说是这样.很多事情可以在一秒钟内继续下去; 有没有办法通过ASL获得更好的分辨率?

ɲeu*_*urɳ 0

您可以附加".#"到 ASL(和 syslog)中的时间格式来指定精度。因此"utc.3"将以毫秒为单位的 UTC 格式进行格式化。msg_fmt您可以将其添加到或参数中time_fmt

时间格式精度似乎仅记录在syslogd(1)中。我不确定为什么它没有到达asl(3)

例如,asl_add_output_file在 中使用和指定time_fmt可能如下所示:

// setup, do once!
aslclient log = asl_open(NULL, NULL, 0);
asl_add_output_file(log, STDERR_FILENO,
    "$Time $((Level)(str)) - $Message", // $Time here uses time_fmt arg on next line 
    ASL_TIME_FMT_UTC ".3", // time_fmt: append ".3" for milliseconds here
    ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG), ASL_ENCODE_SAFE);

// example log
asl_log(log, NULL, ASL_LEVEL_INFO, "Hello");

// Note in the above ASL_TIME_FMT_UTC is #defined to "utc", so the we're
// using compile-time string concatenation of "utc" ".3" to "utc.3".
// Alternately you can just specify "utc.3" directly.
Run Code Online (Sandbox Code Playgroud)

和输出:

2016-02-01 19:16:39.561Z Info - Hello
Run Code Online (Sandbox Code Playgroud)

msg_fmt在的参数中指定asl_add_output_file 可能如下所示:

// setup, do once!
aslclient log = asl_open(NULL, NULL, 0);
asl_add_output_file(log, STDERR_FILENO,
    // in msg_fmt below, use ISO8601 + ".6" for microseconds
    "$((Time)(ISO8601.6)) $((Level)(str)) - $Message",
    ASL_TIME_FMT_UTC,
    ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG), ASL_ENCODE_SAFE);

// example log
asl_log(log, NULL, ASL_LEVEL_INFO, "Hello");
Run Code Online (Sandbox Code Playgroud)

和输出:

2016-02-01T14:16:39.562030-05 Info - Hello
Run Code Online (Sandbox Code Playgroud)

(我要提醒的是,上面的代码片段只是为了演示在 ASL 中指定时间格式精度。实际使用可能会涉及 fordispatch_once设置,使用串行调度队列进行日志记录。)