Logger, OSLog


Logger의 사용

개발단계에서 사용한 print()를 릴리즈 빌드에서 제외하고 싶다면 다음과 같이 오버라이드해서 사용가능.

// global print(_ message: String) {    // 외뷰 패키지에서도 참조 가능
func print(_ message: String) {
    #if DEBUG
    Swift.print(message)
    #endif
}

print()의 사용은 생각보다 퍼포먼스 페널티가 크기 때문에 릴리즈 빌드에서는 제외해야 한다.
대신 Swift에 내장된 OSLog, Logger를 사용하면 개발 페이즈별로 로그를 편리하게 관리할 수 있는데
in memory만 사용하는 .debug 레벨을 제외하면 모두 레벨의 로그들은 Binary 로 압축저장되어
오버헤드가 적고 .debug 레벨은 릴리즈 빌드에서 자동으로 실행제외 된다.
Binary 파일이지만 Xcode(ver12 or later) 나 Console에서는 로그를 위한 뷰를 당연히 지원.
macOS 11 이하 버전에서 사용되던 OSLog 에 약간의 편의성과 높은 추상화를 적용한것이
바로 Logger. 둘의 차이점은 크지 않고 OSLog가 조금더 디테일한 세팅환경을 제공한다.

로그 레벨

on-disk storage 저장 되는 경우 storage limit 을 초과하면 과거기록부터 자동으로 삭제된다.
fault 에 가까울 수록 가장 늦게 삭제, 퍼포먼스는 debug 에 가까울 수록 빠름.
debug 레벨은 현재 스트리밍 상태가 아니라면 저장되지 않고 모두 버려지며
이때 release 빌드에서는 로그들이 실행되지 않도록 컴파일러가 최적화 해준다.

debug in-memory only (fastest)
info persisted, only during log collect
notice default. in-memory, on-disk storage
error both
fault both (slowest)


사용예시

import Foundation
import OSLog


let firstName = "Michale"
let lastName = "Choi"
let address = "Seoul South Korea"
let mobile = "82-10-1234-5678"
let accountNumber = 5446879855

// subsystem, category 사용
let logger = Logger(subsystem: "com.logger.test", category: "test")
logger.info(" .info level log with its subsystem and category")

// debug 환경에서는 보이지만 release 에서는 값이 마스킹되거나 숨겨짐
logger.log("First Name: \(firstName, privacy: .private)")
logger.log("Mobile : \(mobile, privacy: .private(mask: .hash))")
logger.log("Address : \(firstName, privacy: .sensitive)")
logger.log("Address : \(firstName, privacy: .public)")  // visible

// numeric type (invisible)
logger.log("Paid with bank account \(accountNumber)")


let defaultLogger = Logger(.default)
defaultLogger.debug("test")

Logger().debug("name : \(lastName)")
Logger().info("name : \(lastName)")
Logger().notice("name : \(lastName, privacy: .public)")
Logger().log("name : \(lastName, privacy: .public)")   // .notice
Logger().error("Error")
Logger().fault("Fault")

os_log(.default, "os_log .default")
os_log("log")
os_log(.debug, "os_log .debug")


로그확인

저장되는 위치

open ~/Library/Developer/Xcode/DerivedData

터미널 (subsystem 필터링)

log stream --predicate 'subsystem == "com.logger.test"' -- info


Console (process 필터링)



Xcode debug area




터미널을 사용한 iPhone의 로깅과 파일저장

현재 연결되어 있는 device상의 스트리밍 로그를 –start <날짜>부터 --output <파일명> 저장

log collect --device --start '2020-06-22 9:41:00' --output frutta.logarchive

  • --device 디바이스를 명시하지 않아도 시스템이 자동으로 감지함
  • device-name="Maxs iPhone" 를 사용하면 연결된 디바이스를 특정할 수 있음
  • --ouput 파라미터를 제외하면 ‘system_logs.logarchive’ 파일로 저장됨





Preprocess를 사용한 방법

  • TARGETS >Build Settings >Swift Compiler >Custom Flags >Active Compilation Conditions

기본적으로 Debug 모드에 세팅되어있는 ‘DEBUG’ 플래그와 여기에 ‘test_0’를 추가하는 예시

Reference

https://developer.apple.com/videos/play/wwdc2020/10168/ https://developer.apple.com/videos/play/wwdc2023/10226/ https://developer.apple.com/documentation/xcode/configuring-the-build-settings-of-a-target https://betterprogramming.pub/lets-parse-xcode-logs-dea4e5fb1de4

results matching ""

    No results matching ""