Unified Logging and Activity Tracing

183
© 2016 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. Logging for the future System Frameworks #WWDC16 Session 721 Unified Logging and Activity Tracing Steven Szymanski Core OS Engineering Matthieu Lucas System Applications

Transcript of Unified Logging and Activity Tracing

Page 1: Unified Logging and Activity Tracing

© 2016 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

Logging for the future

System Frameworks #WWDC16

Session 721

Unified Logging and Activity Tracing

Steven Szymanski Core OS EngineeringMatthieu Lucas System Applications

Page 2: Unified Logging and Activity Tracing

Agenda

IntroductionLogging ConceptsDemoUsing the Unified SystemToolsBest PracticesGathering LogsDeprications

Page 3: Unified Logging and Activity Tracing

Introduction

Page 4: Unified Logging and Activity Tracing

Background

Page 5: Unified Logging and Activity Tracing

Background

In 2014 Apple introduced Activity Tracing

Page 6: Unified Logging and Activity Tracing

Background

In 2014 Apple introduced Activity TracingWe also introduced the concept of Faults and Errors

Page 7: Unified Logging and Activity Tracing

Background

In 2014 Apple introduced Activity TracingWe also introduced the concept of Faults and ErrorsWe recognize that Apple has several logging APIs

Page 8: Unified Logging and Activity Tracing

Goals

Page 9: Unified Logging and Activity Tracing

Goals

One common, efficient logging mechanism for both user and kernel mode

Page 10: Unified Logging and Activity Tracing

Goals

One common, efficient logging mechanism for both user and kernel modeMaximize information collected while minimizing observer effect

Page 11: Unified Logging and Activity Tracing

Goals

One common, efficient logging mechanism for both user and kernel modeMaximize information collected while minimizing observer effect• Compressing data

Page 12: Unified Logging and Activity Tracing

Goals

One common, efficient logging mechanism for both user and kernel modeMaximize information collected while minimizing observer effect• Compressing data• Deferring work and data collection

Page 13: Unified Logging and Activity Tracing

Goals

One common, efficient logging mechanism for both user and kernel modeMaximize information collected while minimizing observer effect• Compressing data• Deferring work and data collection• Managing log message lifecycle

Page 14: Unified Logging and Activity Tracing

Goals

One common, efficient logging mechanism for both user and kernel modeMaximize information collected while minimizing observer effect• Compressing data• Deferring work and data collection• Managing log message lifecycle

We want as much logging on all the time as possible

Page 15: Unified Logging and Activity Tracing

Goals

One common, efficient logging mechanism for both user and kernel modeMaximize information collected while minimizing observer effect• Compressing data• Deferring work and data collection• Managing log message lifecycle

We want as much logging on all the time as possibleDesign privacy into the system

Page 16: Unified Logging and Activity Tracing

Features

Page 17: Unified Logging and Activity Tracing

Features

Improved categorization and filtering of log messages

Page 18: Unified Logging and Activity Tracing

Features

Improved categorization and filtering of log messagesLogging system collects caller information for you

Page 19: Unified Logging and Activity Tracing

Features

Improved categorization and filtering of log messagesLogging system collects caller information for youNew builtin type specifiers - simplifies log message preparation

Page 20: Unified Logging and Activity Tracing

Features

Improved categorization and filtering of log messagesLogging system collects caller information for youNew builtin type specifiers - simplifies log message preparationNew Console application and command-line tool

Page 21: Unified Logging and Activity Tracing

Features

Improved categorization and filtering of log messagesLogging system collects caller information for youNew builtin type specifiers - simplifies log message preparationNew Console application and command-line toolSupported on macOS, iOS, tvOS, watchOS, and Simulators

Page 22: Unified Logging and Activity Tracing

Features

Improved categorization and filtering of log messagesLogging system collects caller information for youNew builtin type specifiers - simplifies log message preparationNew Console application and command-line toolSupported on macOS, iOS, tvOS, watchOS, and SimulatorsSupport for Objective-C, C++ and C

Page 23: Unified Logging and Activity Tracing

Features

Improved categorization and filtering of log messagesLogging system collects caller information for youNew builtin type specifiers - simplifies log message preparationNew Console application and command-line toolSupported on macOS, iOS, tvOS, watchOS, and SimulatorsSupport for Objective-C, C++ and C Swift support in upcoming seed

Page 24: Unified Logging and Activity Tracing

Current Console

Page 25: Unified Logging and Activity Tracing

Console Revisited…

Page 26: Unified Logging and Activity Tracing

Console Revisited…

Page 27: Unified Logging and Activity Tracing

Logging Concepts

Page 28: Unified Logging and Activity Tracing

Adoption

Page 29: Unified Logging and Activity Tracing

Adoption

If you want to use the new Unified Logging system• Build with the macOS 10.12, iOS 10.0, tvOS 10.0 or watchOS 3.0 SDK• Legacy APIs (NSLog, asl_log_message, syslog…) redirected into new system• Log data will be in new format and location

Page 30: Unified Logging and Activity Tracing

Adoption

If you want to use the new Unified Logging system• Build with the macOS 10.12, iOS 10.0, tvOS 10.0 or watchOS 3.0 SDK• Legacy APIs (NSLog, asl_log_message, syslog…) redirected into new system• Log data will be in new format and location

If you don’t want to use the new Unified Logging system• Build with macOS10.11, iOS 9.0, tvOS 9.0 and watchOS 2.0 SDK• No changes

Page 31: Unified Logging and Activity Tracing

New File Formats

Page 32: Unified Logging and Activity Tracing

New File Formats

Log data is kept in a compressed binary format: .tracev3 files

Page 33: Unified Logging and Activity Tracing

New File Formats

Log data is kept in a compressed binary format: .tracev3 filesStored under /var/db/diagnostics/ with support in /var/db/uuidtext

Page 34: Unified Logging and Activity Tracing

New File Formats

Log data is kept in a compressed binary format: .tracev3 filesStored under /var/db/diagnostics/ with support in /var/db/uuidtextNew tools to access the stored and live log messages

Page 35: Unified Logging and Activity Tracing

New File Formats

Log data is kept in a compressed binary format: .tracev3 filesStored under /var/db/diagnostics/ with support in /var/db/uuidtextNew tools to access the stored and live log messages• Because data is stored in binary format, you MUST use new tools to access files

Page 36: Unified Logging and Activity Tracing

New File Formats

Log data is kept in a compressed binary format: .tracev3 filesStored under /var/db/diagnostics/ with support in /var/db/uuidtextNew tools to access the stored and live log messages• Because data is stored in binary format, you MUST use new tools to access files

New .logarchive format for portability of logs

Page 37: Unified Logging and Activity Tracing

New File Formats

Log data is kept in a compressed binary format: .tracev3 filesStored under /var/db/diagnostics/ with support in /var/db/uuidtextNew tools to access the stored and live log messages• Because data is stored in binary format, you MUST use new tools to access files

New .logarchive format for portability of logs

Page 38: Unified Logging and Activity Tracing

NEWSubsystems and Categories

Page 39: Unified Logging and Activity Tracing

NEWSubsystems and Categories

Log messages can be associated with a subsystem and category

Page 40: Unified Logging and Activity Tracing

NEWSubsystems and Categories

Log messages can be associated with a subsystem and categoryCan be used to control how log messages are filtered and displayed

Page 41: Unified Logging and Activity Tracing

NEWSubsystems and Categories

Log messages can be associated with a subsystem and categoryCan be used to control how log messages are filtered and displayedA subsystem can contain multiple categories

Page 42: Unified Logging and Activity Tracing

NEWSubsystems and Categories

Log messages can be associated with a subsystem and categoryCan be used to control how log messages are filtered and displayedA subsystem can contain multiple categoriesYou can use as many subsystems and categories as needed

Page 43: Unified Logging and Activity Tracing

NEWSubsystems and Categories

Log messages can be associated with a subsystem and categoryCan be used to control how log messages are filtered and displayedA subsystem can contain multiple categoriesYou can use as many subsystems and categories as needed

Example:

Page 44: Unified Logging and Activity Tracing

NEWSubsystems and Categories

Log messages can be associated with a subsystem and categoryCan be used to control how log messages are filtered and displayedA subsystem can contain multiple categoriesYou can use as many subsystems and categories as needed

Subsystem Category

com.your-company.your-application setup, inprogress, teardown

Example:

Page 45: Unified Logging and Activity Tracing

NEWSubsystems and Categories

Log messages can be associated with a subsystem and categoryCan be used to control how log messages are filtered and displayedA subsystem can contain multiple categoriesYou can use as many subsystems and categories as needed

Subsystem Category

com.your-company.your-application setup, inprogress, teardown

com.your-company.test.your-application test

Example:

Page 46: Unified Logging and Activity Tracing

Logging Behavior NEW

Page 47: Unified Logging and Activity Tracing

Logging Behavior

Each log message has a level determined by the API used

NEW

Page 48: Unified Logging and Activity Tracing

Logging Behavior

Each log message has a level determined by the API used• Three basic levels—Default, Info, Debug

NEW

Page 49: Unified Logging and Activity Tracing

Logging Behavior

Each log message has a level determined by the API used• Three basic levels—Default, Info, Debug• Two special levels—Fault, Error

NEW

Page 50: Unified Logging and Activity Tracing

Logging Behavior

Each log message has a level determined by the API used• Three basic levels—Default, Info, Debug• Two special levels—Fault, Error

Each basic level has two characteristics that can be set for system, subsystem, or category

NEW

Page 51: Unified Logging and Activity Tracing

Logging Behavior

Each log message has a level determined by the API used• Three basic levels—Default, Info, Debug• Two special levels—Fault, Error

Each basic level has two characteristics that can be set for system, subsystem, or category• Is is enabled? (Default messages are always enabled)

NEW

Page 52: Unified Logging and Activity Tracing

Logging Behavior

Each log message has a level determined by the API used• Three basic levels—Default, Info, Debug• Two special levels—Fault, Error

Each basic level has two characteristics that can be set for system, subsystem, or category• Is is enabled? (Default messages are always enabled)• Is it stored to disk or memory?

NEW

Page 53: Unified Logging and Activity Tracing

Logging Behavior

Each log message has a level determined by the API used• Three basic levels—Default, Info, Debug• Two special levels—Fault, Error

Each basic level has two characteristics that can be set for system, subsystem, or category• Is is enabled? (Default messages are always enabled)• Is it stored to disk or memory?

The levels are hierarchical

NEW

Page 54: Unified Logging and Activity Tracing

Logging Behavior

Each log message has a level determined by the API used• Three basic levels—Default, Info, Debug• Two special levels—Fault, Error

Each basic level has two characteristics that can be set for system, subsystem, or category• Is is enabled? (Default messages are always enabled)• Is it stored to disk or memory?

The levels are hierarchical• So setting Debug to go to disk implies that Info will also go to disk

NEW

Page 55: Unified Logging and Activity Tracing

Logging Behavior

Each log message has a level determined by the API used• Three basic levels—Default, Info, Debug• Two special levels—Fault, Error

Each basic level has two characteristics that can be set for system, subsystem, or category• Is is enabled? (Default messages are always enabled)• Is it stored to disk or memory?

The levels are hierarchical• So setting Debug to go to disk implies that Info will also go to disk

Behavior can be customized by installing profiles or, on macOS, via log command

NEW

Page 56: Unified Logging and Activity Tracing

Standard Behavior NEW

Page 57: Unified Logging and Activity Tracing

Standard Behavior

Message Level Enabled Destination

DEFAULT LEVEL ALWAYS DISK

INFO LEVEL YES MEMORY

DEBUG LEVEL NO N/A

NEW

Page 58: Unified Logging and Activity Tracing

Standard Behavior

Message Level Enabled Destination

DEFAULT LEVEL ALWAYS DISK

INFO LEVEL YES MEMORY

DEBUG LEVEL NO N/A

ERROR ALWAYS DISK

FAULT ALWAYS DISK

NEW

Page 59: Unified Logging and Activity Tracing

Privacy NEW

Page 60: Unified Logging and Activity Tracing

Privacy

Prevent accidental logging of Personally Identifiable Information (PII)

NEW

Page 61: Unified Logging and Activity Tracing

Privacy

Prevent accidental logging of Personally Identifiable Information (PII)Dynamic strings, collections, arrays, etc. are assumed to be private

NEW

Page 62: Unified Logging and Activity Tracing

Faults and Errors NEW

Page 63: Unified Logging and Activity Tracing

Faults and Errors

We do extra work saving additional information on Fault or Error

NEW

Page 64: Unified Logging and Activity Tracing

Faults and Errors

We do extra work saving additional information on Fault or Error Errors represent issues discovered within a given application/library

NEW

Page 65: Unified Logging and Activity Tracing

Faults and Errors

We do extra work saving additional information on Fault or Error Errors represent issues discovered within a given application/libraryFaults represent more global problems in the system

NEW

Page 66: Unified Logging and Activity Tracing

Faults and Errors

We do extra work saving additional information on Fault or Error Errors represent issues discovered within a given application/libraryFaults represent more global problems in the systemFaults and Error log information is captured into a separate set of log files

NEW

Page 67: Unified Logging and Activity Tracing

Architecture

Page 68: Unified Logging and Activity Tracing

Architecture

Process A

Process B

4 k4 kBuffer

4 k4 kBuffer

Page 69: Unified Logging and Activity Tracing

Architecture

Process A logd

Compressor

Process B

4 k4 kBuffer

BufferBufferBufferBufferCompressed Buffer

4 k4 kBuffer

Page 70: Unified Logging and Activity Tracing

Architecture

Process A logd

Compressor

Process B

4 k4 kBufferMemory Only Buffer

Regular Log Data

Fault and Error Log Data

Others

Compressed Log Files

BufferBufferBufferBufferCompressed Buffer

4 k4 kBuffer

Page 71: Unified Logging and Activity Tracing

Architecture

Process A logd

Compressor

Process B

4 k4 kBufferMemory Only Buffer

Regular Log Data

Fault and Error Log Data

Others“Live” log stream

Compressed Log Filesdiagnosticd

BufferBufferBufferBufferCompressed Buffer

4 k4 kBuffer

Page 72: Unified Logging and Activity Tracing

Architecture

Process A logd

Compressor

Process B

Profile can change routing and rules for given applications or subsystems

4 k4 kBufferMemory Only Buffer

Regular Log Data

Fault and Error Log Data

Others“Live” log stream

Compressed Log Filesdiagnosticd

BufferBufferBufferBufferCompressed Buffer

4 k4 kBuffer

Page 73: Unified Logging and Activity Tracing

DemoConsole demo

Page 74: Unified Logging and Activity Tracing

Using the Unified System

Page 75: Unified Logging and Activity Tracing

API Destination Description

os_log Disk Default logging level that is always captured

os_log_info Memory Additional information (defaults to memory-only buffers)

os_log_debug Off Debug level content (off-by default)

os_log_error Disk Process local error

os_log_fault Disk System-level error (usually involves multiple processes)

os_log_create n/a Create a log object for custom behaviors

Summary of New APIs NEW

Page 76: Unified Logging and Activity Tracing

Creating a Log Object NEW

Page 77: Unified Logging and Activity Tracing

Creating a Log Object NEW

os_log_t log = os_log_create("com.your_company.subsystem", "network");

Create thread-safe singleton object that controls behavior of log messages

Page 78: Unified Logging and Activity Tracing

Creating a Log Object NEW

os_log_t log = os_log_create("com.your_company.subsystem", "network");

Create thread-safe singleton object that controls behavior of log messages Defaults to system-behavior

Page 79: Unified Logging and Activity Tracing

Creating a Log Object NEW

os_log_t log = os_log_create("com.your_company.subsystem", "network");

Create thread-safe singleton object that controls behavior of log messages Defaults to system-behaviorUsage

os_log(log, "This happened");

Page 80: Unified Logging and Activity Tracing

Creating a Log Object NEW

os_log_t log = os_log_create("com.your_company.subsystem", "network");

Create thread-safe singleton object that controls behavior of log messages Defaults to system-behaviorUsage

os_log(log, "This happened");

Reference to category and subsystem stored with every log message

Page 81: Unified Logging and Activity Tracing

Creating a Log Object NEW

os_log_t log = os_log_create("com.your_company.subsystem", "network");

Create thread-safe singleton object that controls behavior of log messages Defaults to system-behaviorUsage

os_log(log, "This happened");

Reference to category and subsystem stored with every log messageOr use OS_LOG_DEFAULT • For messages not associated with subsystem/category

Page 82: Unified Logging and Activity Tracing

Creating a Log Object NEW

os_log_t log = os_log_create("com.your_company.subsystem", "network");

Create thread-safe singleton object that controls behavior of log messages Defaults to system-behaviorUsage

os_log(log, "This happened");

Reference to category and subsystem stored with every log messageOr use OS_LOG_DEFAULT • For messages not associated with subsystem/category

Page 83: Unified Logging and Activity Tracing

Built-in Type Formatters NEW

We all spend too much code converting binary information to strings to log

Page 84: Unified Logging and Activity Tracing

Built-in Type Formatters NEW

We all spend too much code converting binary information to strings to log

Built-in decoding for common values"%{time_t}d" or "%{errno}d"

Page 85: Unified Logging and Activity Tracing

Built-in Type Formatters NEW

We all spend too much code converting binary information to strings to log

Built-in decoding for common values"%{time_t}d" or "%{errno}d"Arbitrary binary data using a new format type"%.*P"

Page 86: Unified Logging and Activity Tracing

Built-in Type Formatters NEW

We all spend too much code converting binary information to strings to log

Built-in decoding for common values"%{time_t}d" or "%{errno}d"Arbitrary binary data using a new format type"%.*P"

Built-in decoding for binary-types“%{uuid_t}.16P"

Page 87: Unified Logging and Activity Tracing

Example Type Formatters

Type Format String Example Outputtime_t %{time_t}d 2016-01-12 19:41:37

timeval %{timeval}.*P 2016-01-12 19:41:37.774236

timespec %{timespec}.*P 2016-01-12 19:41:37.774236823

errno %{errno}d Broken pipe

uuid_t %{uuid_t}.16P %{uuid_t}.*P 10742E39-0657-41F8-AB99-878C5EC2DCAA

sockaddr %{network:sockaddr}.*P fe80::f:86ff:fee9:5c16 17.43.23.87

in_addr %{network:in_addr}d 17.43.23.87

in6_addr %{network:in6_addr}.16P fe80::f:86ff:fee9:5c16

NEW

Page 88: Unified Logging and Activity Tracing

Per Parameter Privacy NEW

Page 89: Unified Logging and Activity Tracing

Per Parameter Privacy

Privacy is handled on a parameter by parameter basis

NEW

Page 90: Unified Logging and Activity Tracing

Per Parameter Privacy

Privacy is handled on a parameter by parameter basisScalars and static strings are assumed to be public

NEW

Page 91: Unified Logging and Activity Tracing

Per Parameter Privacy

Privacy is handled on a parameter by parameter basisScalars and static strings are assumed to be publicDynamic strings, collections, and objects are assumed to be private

NEW

Page 92: Unified Logging and Activity Tracing

Per Parameter Privacy

Privacy is handled on a parameter by parameter basisScalars and static strings are assumed to be publicDynamic strings, collections, and objects are assumed to be private

NEW

Can be overridden on a per-parameter basis

"%{public}@" or "%{private}d"

Page 93: Unified Logging and Activity Tracing

Per Parameter Privacy

Privacy is handled on a parameter by parameter basisScalars and static strings are assumed to be publicDynamic strings, collections, and objects are assumed to be private

NEW

Can be overridden on a per-parameter basis

"%{public}@" or "%{private}d"Combine privacy and formatting

"%{public, uuid_t}.16P”

Page 94: Unified Logging and Activity Tracing

/*

* Log Message Simplification

*/

Page 95: Unified Logging and Activity Tracing

/*

* Log Message Simplification

*/

// Old way:

Page 96: Unified Logging and Activity Tracing

/*

* Log Message Simplification

*/

// Old way:

if (LogLevelEnabled(info)) {

Page 97: Unified Logging and Activity Tracing

/*

* Log Message Simplification

*/

// Old way:

if (LogLevelEnabled(info)) {

uuid_string_t uuid_str;

uuid_unparse_upper(uuid, uuid_str);

Page 98: Unified Logging and Activity Tracing

/*

* Log Message Simplification

*/

// Old way:

if (LogLevelEnabled(info)) {

uuid_string_t uuid_str;

uuid_unparse_upper(uuid, uuid_str);

char *addr_desc = _convert_sockaddr(&sa);

Page 99: Unified Logging and Activity Tracing

/*

* Log Message Simplification

*/

// Old way:

if (LogLevelEnabled(info)) {

uuid_string_t uuid_str;

uuid_unparse_upper(uuid, uuid_str);

char *addr_desc = _convert_sockaddr(&sa);

NSLog(@"%s (%s:%d) - fd: %d, uuid: %s, IP: %s”,

__PRETTY_FUNCTION__, __FILE__, __LINE__, fd, uuid_st, addr_desc);

Page 100: Unified Logging and Activity Tracing

/*

* Log Message Simplification

*/

// Old way:

if (LogLevelEnabled(info)) {

uuid_string_t uuid_str;

uuid_unparse_upper(uuid, uuid_str);

char *addr_desc = _convert_sockaddr(&sa);

NSLog(@"%s (%s:%d) - fd: %d, uuid: %s, IP: %s”,

__PRETTY_FUNCTION__, __FILE__, __LINE__, fd, uuid_st, addr_desc);

free(addr_desc);

Page 101: Unified Logging and Activity Tracing

/*

* Log Message Simplification

*/

// Old way:

if (LogLevelEnabled(info)) {

uuid_string_t uuid_str;

uuid_unparse_upper(uuid, uuid_str);

char *addr_desc = _convert_sockaddr(&sa);

NSLog(@"%s (%s:%d) - fd: %d, uuid: %s, IP: %s”,

__PRETTY_FUNCTION__, __FILE__, __LINE__, fd, uuid_st, addr_desc);

free(addr_desc);

}

Page 102: Unified Logging and Activity Tracing

/*

* Log Message Simplification

*/

// Old way:

if (LogLevelEnabled(info)) {

uuid_string_t uuid_str;

uuid_unparse_upper(uuid, uuid_str);

char *addr_desc = _convert_sockaddr(&sa);

NSLog(@"%s (%s:%d) - fd: %d, uuid: %s, IP: %s”,

__PRETTY_FUNCTION__, __FILE__, __LINE__, fd, uuid_st, addr_desc);

free(addr_desc);

}

// New way:

Page 103: Unified Logging and Activity Tracing

/*

* Log Message Simplification

*/

// Old way:

if (LogLevelEnabled(info)) {

uuid_string_t uuid_str;

uuid_unparse_upper(uuid, uuid_str);

char *addr_desc = _convert_sockaddr(&sa);

NSLog(@"%s (%s:%d) - fd: %d, uuid: %s, IP: %s”,

__PRETTY_FUNCTION__, __FILE__, __LINE__, fd, uuid_st, addr_desc);

free(addr_desc);

}

// New way:

os_log_info(OS_LOG_DEFAULT,

Page 104: Unified Logging and Activity Tracing

/*

* Log Message Simplification

*/

// Old way:

if (LogLevelEnabled(info)) {

uuid_string_t uuid_str;

uuid_unparse_upper(uuid, uuid_str);

char *addr_desc = _convert_sockaddr(&sa);

NSLog(@"%s (%s:%d) - fd: %d, uuid: %s, IP: %s”,

__PRETTY_FUNCTION__, __FILE__, __LINE__, fd, uuid_st, addr_desc);

free(addr_desc);

}

// New way:

os_log_info(OS_LOG_DEFAULT,

"fd: %d, uuid: %{uuid_t}.16P, IP: %{network:sockaddr}.*P”,

Page 105: Unified Logging and Activity Tracing

/*

* Log Message Simplification

*/

// Old way:

if (LogLevelEnabled(info)) {

uuid_string_t uuid_str;

uuid_unparse_upper(uuid, uuid_str);

char *addr_desc = _convert_sockaddr(&sa);

NSLog(@"%s (%s:%d) - fd: %d, uuid: %s, IP: %s”,

__PRETTY_FUNCTION__, __FILE__, __LINE__, fd, uuid_st, addr_desc);

free(addr_desc);

}

// New way:

os_log_info(OS_LOG_DEFAULT,

"fd: %d, uuid: %{uuid_t}.16P, IP: %{network:sockaddr}.*P”,

fd, uuid, sa->sa_len, &sa);

Page 106: Unified Logging and Activity Tracing

/* * Example Code */

Page 107: Unified Logging and Activity Tracing

/* * Example Code */

os_log_t general_log = os_log_create("com.apple.logging.example", "general");

Page 108: Unified Logging and Activity Tracing

/* * Example Code */

os_log_t general_log = os_log_create("com.apple.logging.example", "general");os_log_t time_log = os_log_create("com.apple.logging.example", "timestamp");

Page 109: Unified Logging and Activity Tracing

/* * Example Code */

os_log_t general_log = os_log_create("com.apple.logging.example", "general");os_log_t time_log = os_log_create("com.apple.logging.example", "timestamp");

os_log(general_log, "running example code”);

Page 110: Unified Logging and Activity Tracing

/* * Example Code */

os_log_t general_log = os_log_create("com.apple.logging.example", "general");os_log_t time_log = os_log_create("com.apple.logging.example", "timestamp");

os_log(general_log, "running example code”);

os_log_info(general_log, "processing file %{public}s”, filename);

Page 111: Unified Logging and Activity Tracing

/* * Example Code */

os_log_t general_log = os_log_create("com.apple.logging.example", "general");os_log_t time_log = os_log_create("com.apple.logging.example", "timestamp");

os_log(general_log, "running example code”);

os_log_info(general_log, "processing file %{public}s”, filename);

int fd = open(filename, O_RDONLY);if (fd < 0) { os_log_error(general_log, "Cannot open file %{public}s - %{errno}d", filename, errno); . . . }

Page 112: Unified Logging and Activity Tracing

/* * Example Code */

os_log_t general_log = os_log_create("com.apple.logging.example", "general");os_log_t time_log = os_log_create("com.apple.logging.example", "timestamp");

os_log(general_log, "running example code”);

os_log_info(general_log, "processing file %{public}s”, filename);

int fd = open(filename, O_RDONLY);if (fd < 0) { os_log_error(general_log, "Cannot open file %{public}s - %{errno}d", filename, errno); . . . }

struct stat sb;if (fstat(fd, &sb) < 0) { os_log_fault(general_log, "Failed to fstat %{public}s - %{errno}d", filename, errno); . . . }

Page 113: Unified Logging and Activity Tracing

/* * Example Code */

os_log_t general_log = os_log_create("com.apple.logging.example", "general");os_log_t time_log = os_log_create("com.apple.logging.example", "timestamp");

os_log(general_log, "running example code”);

os_log_info(general_log, "processing file %{public}s”, filename);

int fd = open(filename, O_RDONLY);if (fd < 0) { os_log_error(general_log, "Cannot open file %{public}s - %{errno}d", filename, errno); . . . }

struct stat sb;if (fstat(fd, &sb) < 0) { os_log_fault(general_log, "Failed to fstat %{public}s - %{errno}d", filename, errno); . . . }

os_log_info(time_log, "status for file %{public}s, atime:%{timespec}.*P, mtime:%{timespec}.*P, ctime:%{timespec}.*P",

filename, sizeof(struct timespec), &sb.st_atimespec, sizeof(struct timespec), &sb.st_mtimespec, sizeof(struct timespec), &sb.st_ctimespec);

Page 114: Unified Logging and Activity Tracing

Activity API Improvements

Page 115: Unified Logging and Activity Tracing

Activity API Improvements

Activities are now objects that can be stored and re-used

Page 116: Unified Logging and Activity Tracing

Activity API Improvements

Activities are now objects that can be stored and re-used• Direct control of activity relationships during creation

Page 117: Unified Logging and Activity Tracing

Activity API Improvements

Activities are now objects that can be stored and re-used• Direct control of activity relationships during creation

New API to auto-scope activities within your code

Page 118: Unified Logging and Activity Tracing

Improved Activity APIs

os_activity_create Creates a new activity object

os_activity_scope Makes an execution block a part of an activity

os_activity_apply Invokes a block scoped to a given activity

os_activity_label_useraction Label an activity as a user action (UI-based activities)

NEW

Page 119: Unified Logging and Activity Tracing

/* * New Activity API Example */

Page 120: Unified Logging and Activity Tracing

/* * New Activity API Example */

os_activity_t init_activity = os_activity_create("Init", OS_ACTIVITY_CURRENT, OS_ACTIVITY_FLAG_DEFAULT);

Page 121: Unified Logging and Activity Tracing

/* * New Activity API Example */

os_activity_t init_activity = os_activity_create("Init", OS_ACTIVITY_CURRENT, OS_ACTIVITY_FLAG_DEFAULT);

os_activity_t verify_activity = os_activity_create("Verify", init_activity, OS_ACTIVITY_FLAG_DEFAULT);

Page 122: Unified Logging and Activity Tracing

/* * New Activity API Example */

os_activity_t init_activity = os_activity_create("Init", OS_ACTIVITY_CURRENT, OS_ACTIVITY_FLAG_DEFAULT);

os_activity_t verify_activity = os_activity_create("Verify", init_activity, OS_ACTIVITY_FLAG_DEFAULT);

if (isReady) { os_activity_scope(verify_activity); // All of the following work is done under “verification activity scope” . . .}

Page 123: Unified Logging and Activity Tracing

/* * New Activity API Example */

os_activity_t init_activity = os_activity_create("Init", OS_ACTIVITY_CURRENT, OS_ACTIVITY_FLAG_DEFAULT);

os_activity_t verify_activity = os_activity_create("Verify", init_activity, OS_ACTIVITY_FLAG_DEFAULT);

if (isReady) { os_activity_scope(verify_activity); // All of the following work is done under “verification activity scope” . . .} // This is not part of that activity

Page 124: Unified Logging and Activity Tracing

/* * New Activity API Example */

os_activity_t init_activity = os_activity_create("Init", OS_ACTIVITY_CURRENT, OS_ACTIVITY_FLAG_DEFAULT);

os_activity_t verify_activity = os_activity_create("Verify", init_activity, OS_ACTIVITY_FLAG_DEFAULT);

if (isReady) { os_activity_scope(verify_activity); // All of the following work is done under “verification activity scope” . . .} // This is not part of that activity

os_activity_apply(init_activity, ^{ // do some work based on the “init_activity” . . .});

Page 125: Unified Logging and Activity Tracing

Tools

Page 126: Unified Logging and Activity Tracing

Console NEW

Page 127: Unified Logging and Activity Tracing

Console

View live content from a system

NEW

Page 128: Unified Logging and Activity Tracing

Console

View live content from a systemOpen log archives

NEW

Page 129: Unified Logging and Activity Tracing

Console

View live content from a systemOpen log archivesNew Activity centric view of logging and tracing

NEW

Page 130: Unified Logging and Activity Tracing

Console

View live content from a systemOpen log archivesNew Activity centric view of logging and tracingAdvanced filtering and searching

NEW

Page 131: Unified Logging and Activity Tracing

Console

View live content from a systemOpen log archivesNew Activity centric view of logging and tracingAdvanced filtering and searchingDevice support

NEW

Page 132: Unified Logging and Activity Tracing

log Command Line Tool NEW

Page 133: Unified Logging and Activity Tracing

log Command Line Tool

Same functionality as Console from the command line

NEW

Page 134: Unified Logging and Activity Tracing

log Command Line Tool

Same functionality as Console from the command lineStream live log messages

NEW

Page 135: Unified Logging and Activity Tracing

log Command Line Tool

Same functionality as Console from the command lineStream live log messages

$ log stream

NEW

Page 136: Unified Logging and Activity Tracing

log Command Line Tool

Same functionality as Console from the command lineStream live log messages

$ log stream$ log stream --predicate 'eventMessage contains "my message"'

NEW

Page 137: Unified Logging and Activity Tracing

log Command Line Tool

Same functionality as Console from the command lineStream live log messages

$ log stream$ log stream --predicate 'eventMessage contains "my message"'Display a log file or archive

NEW

Page 138: Unified Logging and Activity Tracing

log Command Line Tool

Same functionality as Console from the command lineStream live log messages

$ log stream$ log stream --predicate 'eventMessage contains "my message"'Display a log file or archive

$ log show system_logs.logarchive

NEW

Page 139: Unified Logging and Activity Tracing

log Command Line Tool

Same functionality as Console from the command lineStream live log messages

$ log stream$ log stream --predicate 'eventMessage contains "my message"'Display a log file or archive

$ log show system_logs.logarchiveEnable debug for your subsystem on macOS

NEW

Page 140: Unified Logging and Activity Tracing

log Command Line Tool

Same functionality as Console from the command lineStream live log messages

$ log stream$ log stream --predicate 'eventMessage contains "my message"'Display a log file or archive

$ log show system_logs.logarchiveEnable debug for your subsystem on macOS

$ log config --mode "level:debug" --subsystem com.mycorp.myapp

NEW

Page 141: Unified Logging and Activity Tracing

log Command Line Tool

Same functionality as Console from the command lineStream live log messages

$ log stream$ log stream --predicate 'eventMessage contains "my message"'Display a log file or archive

$ log show system_logs.logarchiveEnable debug for your subsystem on macOS

$ log config --mode "level:debug" --subsystem com.mycorp.myapp

NEW

Page 142: Unified Logging and Activity Tracing

Coming Soon NEW

Page 143: Unified Logging and Activity Tracing

Coming Soon

Tools for accessing new log information from 10.11 are coming soon

NEW

Page 144: Unified Logging and Activity Tracing

Coming Soon

Tools for accessing new log information from 10.11 are coming soonBut… in the meantime

NEW

Page 145: Unified Logging and Activity Tracing

Coming Soon

Tools for accessing new log information from 10.11 are coming soonBut… in the meantime

$ xcrun simctl spawn booted log show system_logs.logarchive

NEW

Page 146: Unified Logging and Activity Tracing

Coming Soon

Tools for accessing new log information from 10.11 are coming soonBut… in the meantime

$ xcrun simctl spawn booted log show system_logs.logarchive

NEW

Page 147: Unified Logging and Activity Tracing

Best Practices

Page 148: Unified Logging and Activity Tracing

Logging Etiquette

Page 149: Unified Logging and Activity Tracing

Logging Etiquette

Ensure messages contain only information useful for debugging

Page 150: Unified Logging and Activity Tracing

Logging Etiquette

Ensure messages contain only information useful for debuggingLet us do the formatting for you—leverage built-in formatters

Page 151: Unified Logging and Activity Tracing

Logging Etiquette

Ensure messages contain only information useful for debuggingLet us do the formatting for you—leverage built-in formattersAvoid creating wrapper functions for os_log* APIs

Page 152: Unified Logging and Activity Tracing

Logging Etiquette

Ensure messages contain only information useful for debuggingLet us do the formatting for you—leverage built-in formattersAvoid creating wrapper functions for os_log* APIsLog only what you need from collections (Dictionaries, Arrays, etc.)

Page 153: Unified Logging and Activity Tracing

Logging Etiquette

Ensure messages contain only information useful for debuggingLet us do the formatting for you—leverage built-in formattersAvoid creating wrapper functions for os_log* APIsLog only what you need from collections (Dictionaries, Arrays, etc.)Avoid logging in tight code loops

Page 154: Unified Logging and Activity Tracing

Using os_log Family of APIs

Page 155: Unified Logging and Activity Tracing

Using os_log Family of APIs

Use os_log to log critical details to help debug issues

Page 156: Unified Logging and Activity Tracing

Using os_log Family of APIs

Use os_log to log critical details to help debug issuesUse os_log_info for additional info that will be captured during error or fault

Page 157: Unified Logging and Activity Tracing

Using os_log Family of APIs

Use os_log to log critical details to help debug issuesUse os_log_info for additional info that will be captured during error or faultUse os_log_debug for high-volume debugging during development

Page 158: Unified Logging and Activity Tracing

Using os_log Family of APIs

Use os_log to log critical details to help debug issuesUse os_log_info for additional info that will be captured during error or faultUse os_log_debug for high-volume debugging during developmentUse os_log_error to cause additional information capture from app

Page 159: Unified Logging and Activity Tracing

Using os_log Family of APIs

Use os_log to log critical details to help debug issuesUse os_log_info for additional info that will be captured during error or faultUse os_log_debug for high-volume debugging during developmentUse os_log_error to cause additional information capture from appUse os_log_fault to cause additional information capture from system

Page 160: Unified Logging and Activity Tracing

Gathering Logs

Page 161: Unified Logging and Activity Tracing

Using sysdiagnose NEW

Page 162: Unified Logging and Activity Tracing

Using sysdiagnose

sysdiagnose is the preferred method to capture data for bug reports

NEW

Page 163: Unified Logging and Activity Tracing

Using sysdiagnose

sysdiagnose is the preferred method to capture data for bug reports• Unified Logging data in system_logs.archive

NEW

Page 164: Unified Logging and Activity Tracing

Using sysdiagnose

sysdiagnose is the preferred method to capture data for bug reports• Unified Logging data in system_logs.archive

You can use key-chord to trigger

NEW

Page 165: Unified Logging and Activity Tracing

Using sysdiagnose

sysdiagnose is the preferred method to capture data for bug reports• Unified Logging data in system_logs.archive

You can use key-chord to triggersysdiagnose on Apple Watch will trigger on both Apple Watch and iPhone

NEW

Page 166: Unified Logging and Activity Tracing

Using sysdiagnose

sysdiagnose is the preferred method to capture data for bug reports• Unified Logging data in system_logs.archive

You can use key-chord to triggersysdiagnose on Apple Watch will trigger on both Apple Watch and iPhoneTransfer from device using iTunes

NEW

Page 167: Unified Logging and Activity Tracing

Using sysdiagnose

sysdiagnose is the preferred method to capture data for bug reports• Unified Logging data in system_logs.archive

You can use key-chord to triggersysdiagnose on Apple Watch will trigger on both Apple Watch and iPhoneTransfer from device using iTunesThis is the file to send to Apple (either Radar or Developer Technical Support)

NEW

Page 168: Unified Logging and Activity Tracing

Key-chords for sysdiagnose NEW

Mac OS Shift + Control + Option + Command + Period (.)

iOS Volume Up + Volume Down + Power Slight vibration on iPhone to indicate start

watchOS Press and hold Digital Crown + Side Button for 1 second A screen shot is triggered if not held long enough. Slight haptic to indicate start.

tvOS Play/Pause + Volume Down On older remote controls for Apple TV, must be held for 5 seconds

Page 169: Unified Logging and Activity Tracing

Deprecations

Page 170: Unified Logging and Activity Tracing

Deprecation of Legacy logging APIs

Page 171: Unified Logging and Activity Tracing

Deprecation of Legacy logging APIs

All ASL logging APIs are superseded by the new APIs

Page 172: Unified Logging and Activity Tracing

Deprecation of Legacy logging APIs

All ASL logging APIs are superseded by the new APIsNew APIs for searching new log data will not be made public this release

Page 173: Unified Logging and Activity Tracing

Deprecation of Legacy logging APIs

All ASL logging APIs are superseded by the new APIsNew APIs for searching new log data will not be made public this release• No equivalent asl_search functionality

Page 174: Unified Logging and Activity Tracing

os_activity_start Use os_activity_create and os_activity_scope / os_activity_apply

os_activity_end Use os_activity_create and os_activity_scope / os_activity_apply

os_activity_set_breadcrumb Use os_activity_label_useraction

os_trace_with_payload Use os_log*

Deprecated Activity APIs

Page 175: Unified Logging and Activity Tracing

Summary

Page 176: Unified Logging and Activity Tracing

Summary

The new Unified Logging system is

Page 177: Unified Logging and Activity Tracing

Summary

The new Unified Logging system is• Faster

Page 178: Unified Logging and Activity Tracing

Summary

The new Unified Logging system is• Faster• Easier to use

Page 179: Unified Logging and Activity Tracing

Summary

The new Unified Logging system is• Faster• Easier to use• Gives you more control

Page 180: Unified Logging and Activity Tracing

Summary

The new Unified Logging system is• Faster• Easier to use• Gives you more control

But requires using new APIs and new tools

Page 181: Unified Logging and Activity Tracing

Related Sessions

Fix Bugs Faster Using Activity Tracing WWDC 2014

Page 182: Unified Logging and Activity Tracing

More Information

https://developer.apple.com/wwdc16/721

Page 183: Unified Logging and Activity Tracing