View in English

  • Apple Developer
    • Get Started

    Explore Get Started

    • Overview
    • Learn
    • Apple Developer Program

    Stay Updated

    • Latest News
    • Hello Developer
    • Platforms

    Explore Platforms

    • Apple Platforms
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store

    Featured

    • Design
    • Distribution
    • Games
    • Accessories
    • Web
    • Home
    • CarPlay
    • Technologies

    Explore Technologies

    • Overview
    • Xcode
    • Swift
    • SwiftUI

    Featured

    • Accessibility
    • App Intents
    • Apple Intelligence
    • Games
    • Machine Learning & AI
    • Security
    • Xcode Cloud
    • Community

    Explore Community

    • Overview
    • Meet with Apple events
    • Community-driven events
    • Developer Forums
    • Open Source

    Featured

    • WWDC
    • Swift Student Challenge
    • Developer Stories
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Centers
    • Documentation

    Explore Documentation

    • Documentation Library
    • Technology Overviews
    • Sample Code
    • Human Interface Guidelines
    • Videos

    Release Notes

    • Featured Updates
    • iOS
    • iPadOS
    • macOS
    • watchOS
    • visionOS
    • tvOS
    • Xcode
    • Downloads

    Explore Downloads

    • All Downloads
    • Operating Systems
    • Applications
    • Design Resources

    Featured

    • Xcode
    • TestFlight
    • Fonts
    • SF Symbols
    • Icon Composer
    • Support

    Explore Support

    • Overview
    • Help Guides
    • Developer Forums
    • Feedback Assistant
    • Contact Us

    Featured

    • Account Help
    • App Review Guidelines
    • App Store Connect Help
    • Upcoming Requirements
    • Agreements and Guidelines
    • System Status
  • Quick Links

    • Events
    • News
    • Forums
    • Sample Code
    • Videos
 

Vídeos

Abrir menu Fechar menu
  • Coleções
  • Todos os vídeos
  • Sobre

Mais vídeos

  • Sobre
  • Código
  • Explore logging in Swift

    Meet the latest generation of Swift unified logging APIs. Learn how to log events and errors in your app while preserving privacy. Take advantage of powerful yet readable options for formatting data — all without sacrificing performance. And we'll show you how you can gather and process log messages to help you understand and debug unexpected behavior in your apps.

    Recursos

      • Vídeo HD
      • Vídeo SD

    Vídeos relacionados

    WWDC23

    • Debug with structured logging

    WWDC20

    • What's new in Swift
  • Buscar neste vídeo...
    • 2:44 - Example illustrating how to add logging to your app in three simple steps

      // Add logging to your app in three simple steps
       
      import os
      
      let logger = Logger(subsystem: "com.example.Fruta", category: "giftcards")
      
      func beginTask(url: URL, handler: (Data) -> Void) {
          launchTask(with: url) {
             handler($0)
          }
      
          logger.log("Started a task")
      }
    • 3:32 - An example code that logs a message with run-time data

      // Add runtime data to the log messsage using string interpolation
      
      import os
      
      let logger = Logger(subsystem: "com.example.Fruta", category: "giftcards")
      
      func beginTask(url: URL, handler: (Data) -> Void) {
          launchTask(with: url) {
             handler($0)
          }
          logger.log("Started a task \(taskId)")
      }
    • 4:28 - Example illustrating why nonnumeric types are redacted in the logs by default

      logger.log("Paid with bank account \(accountNumber)")
    • 5:01 - Code that shows how to mark public data so that it is displayed in the logs

      logger.log("Ordered smoothie \(smoothieName, privacy: .public)")
    • 6:03 - Code shown during first demo

      import SwiftUI
      import os
      
      let logger = Logger(subsystem: "com.example.Fruta", category: "giftcards")
      
      struct GiftCardView: View {
          // Denotes whether there is an active task for loading gift cards.
          @State private var taskRunning: Bool = false
          
          // A UUID that uniquely identifies a task.
          @State private var currentTaskID: UUID = UUID()
          
          // An unrecoverable error seen during execution.
          @State private var error: Error? = nil
      
          // A model that stores information about gift cards.
          @ObservedObject var model: GiftCardModel
          
          var body: some View {
              // Display a list of gifts which can be tapped on and scrolled through.
              GiftCardList(model: model, taskRunning: $taskRunning, currentTaskID: $currentTaskID, error: $error, downloadAction: beginTask, stopAction: endTask)
                  .navigationTitle("Gift Cards")
          }
          
          // Start a task to download gift cards from a server.
          func beginTask(serverURL: URL, cardDownloadHandler: @escaping (Data) -> Void) {
              logger.log("Starting a new task for loading cards \(currentTaskID, privacy: .public)")        
              launchTask(with: serverURL) {
                  cardDownloadHandler($0)
              }
          }
          
          // Stop the currently running task for downloading cards from a server.
          func endTask() {
              guard taskRunning else {
                  logger.fault("Task \(currentTaskID, privacy: .public) is not runinng, cannot be stopped!")
                  error = TaskError.noActiveTask
                  return
              }
              taskRunning = false
              logger.log("Task \(currentTaskID, privacy: .public) interrupted")
          }
          
          // Start a URLSession dataTask with the given URL.
          func launchTask(with url: URL, handler: @escaping (Data) -> Void) {
              guard error == nil else {
                  return
              }
              taskRunning = true
              let task = URLSession.shared.dataTask(with: url) {
                  data, response, error in
                  if let error = error {
                      self.error = ConnectionError.other(error)
                  }
                  if let data = data {
                      handler(data)
                  }
              }
              task.resume()
          }
      
      }
    • 11:51 - Illustration of how debug-level logging will not evaluate the code that constructs log message

      logger.debug("\(slowFunction(data))")
    • 12:58 - Code that shows how to display run-time data with a fixed width and how to set precision of a floating-point value using formatting options

      import SwiftUI
      import os
      
      let statisticsLogger = Logger(subsystem: "com.example.Fruta", category: "statistics")
      
      // Log statistics about communication with a server.
      func logStatistics(taskID: UUID, giftCardID: String, serverID: Int, seconds: Double) {
          statisticsLogger.log("\(taskID) \(giftCardID, align: .left(columns: GiftCard.maxIDLength)) \(serverID) \(seconds, format: .fixed(precision: 2))")
      }
    • 15:00 - Example of formatting log messages

      logger.log("\(data, format: .hex, align: .right(columns: width))")
    • 16:05 - Example illustrating the use of a hash to redact private data, which ensures that identical data get the same hash

      logger.log("Paid with bank account: \(accountNumber, privacy: .private(mask: .hash))")

Developer Footer

  • Vídeos
  • WWDC20
  • Explore logging in Swift
  • Open Menu Close Menu
    • iOS
    • iPadOS
    • macOS
    • tvOS
    • visionOS
    • watchOS
    • App Store
    Open Menu Close Menu
    • Swift
    • SwiftUI
    • Swift Playground
    • TestFlight
    • Xcode
    • Xcode Cloud
    • Icon Composer
    • SF Symbols
    Open Menu Close Menu
    • Accessibility
    • Accessories
    • Apple Intelligence
    • Audio & Video
    • Augmented Reality
    • Business
    • Design
    • Distribution
    • Education
    • Games
    • Health & Fitness
    • In-App Purchase
    • Localization
    • Maps & Location
    • Machine Learning & AI
    • Security
    • Safari & Web
    Open Menu Close Menu
    • Documentation
    • Downloads
    • Sample Code
    • Videos
    Open Menu Close Menu
    • Help Guides & Articles
    • Contact Us
    • Forums
    • Feedback & Bug Reporting
    • System Status
    Open Menu Close Menu
    • Apple Developer
    • App Store Connect
    • Certificates, IDs, & Profiles
    • Feedback Assistant
    Open Menu Close Menu
    • Apple Developer Program
    • Apple Developer Enterprise Program
    • App Store Small Business Program
    • MFi Program
    • Mini Apps Partner Program
    • News Partner Program
    • Video Partner Program
    • Security Bounty Program
    • Security Research Device Program
    Open Menu Close Menu
    • Meet with Apple
    • Apple Developer Centers
    • App Store Awards
    • Apple Design Awards
    • Apple Developer Academies
    • WWDC
    Read the latest news.
    Get the Apple Developer app.
    Copyright © 2026 Apple Inc. All rights reserved.
    Terms of Use Privacy Policy Agreements and Guidelines