> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/swift/debugging/log.md).

# log()

{% tabs %}
{% tab title="👔 log()" %}
💾 程式： [replit](https://replit.com/@pegasusroe/Swift-Playground#Logs/log.swift)        ⬆️ 需要： [HasMirrors](/ios/swift/debugging/hasmirrors.md)

```swift
// ────────────────────────────────────────────────
// 2022.01.27 * (v.1)        + log()
// 2022.01.28 / log()        use Logger instead
// 2022.01.30 + log()        + param `items: [HasMirrors]`
//            / log(_ item:) delegate to `HasMirrors` method
// ────────────────────────────────────────────────

// ⭐ require `HasMirrors`

/// `log(msg)`
public func log(_ item: HasMirrors) {
    _ = item.log()
}

/// `log(items)`
/// ⭐ benefit: 
/// - needn't declare `items` as `HasMirrors`,
///   compiler will infer it.
public func log(_ items: [HasMirrors]) {
    items.log()
}
```

{% endtab %}

{% tab title="💈範例" %}
💾 程式： [replit](https://replit.com/@pegasusroe/Swift-Playground#test/test_log.swift)

```swift
// log(item)
log(true)
log(1.23)

// log(items)
log([
    123, "string", 1.23, true,
])
```

#### Result:

```swift
// ✅
// 🍄 1.23
// 🍄 123
// 🍄 string
// 🍄 1.23
// ✅
```

{% endtab %}

{% tab title="📘 手冊" %}

* Swift Ref ⟩ Expressions ⟩ [Primary Expressions](https://docs.swift.org/swift-book/ReferenceManual/Expressions.html#ID389) ⟩ Literal Expression
  {% endtab %}

{% tab title="📗 參考" %}

* [x] Ficow Shen ⟩ [Swift 中的 #function 到底是什么？](https://blog.ficowshen.com/page/post/60)
  {% endtab %}

{% tab title="🗣 討論" %}

* [Weird value of #function literal in Swift 3.1](https://stackoverflow.com/a/45091446/5409815)
* [Macros in Swift?](https://stackoverflow.com/a/24116621/5409815) - custom log function using [#file, #line, #function](/ios/appendix/xcode/directives/file-line-function.md) ⭐️&#x20;
  {% endtab %}

{% tab title="👥 相關" %}

* [#file, #line, #function](/ios/appendix/xcode/directives/file-line-function.md)
* uses [HasMirrors](/ios/swift/debugging/hasmirrors.md).
* [.logType()](/ios/swiftui/view/view/view.md) - inspect a view's type.
  {% endtab %}
  {% endtabs %}

## History

{% tabs %}
{% tab title="contents" %}

1. (2022.01.27) - first version
2. (2022.01.28) - use Logger.
   {% endtab %}

{% tab title="1" %}
💾 程式： [replit](https://replit.com/@pegasusroe/function-3#main.swift)

```swift
// ----------------------
//     ⭐ log function 
// ----------------------

func log(
    _ message: String  = "",
    file     : String  = #file,         // ⭐ file name
    line     : Int     = #line,         // ⭐ line number
    function : String  = #function,     // ⭐ function name
    note     : String? = nil            // additional info
) {
    let noteString = (note == nil) ? "" : "\n \(note!)" 
    print("\n📍 \(message)\n ( func: '\(function)', line: \(line), file: '\(file)' )\(noteString)")
}
```

{% endtab %}

{% tab title="2" %}

```swift
// ─────────────────────────────────────
// 2022.01.27 * (v.1)
// 2022.01.28 / use Logger instead
// ─────────────────────────────────────

/// `log(msg)`
public func log(
    _ message: String  = "", 
    padding n: Int     = 0,
    dot      : String? = nil
) {
    Logger.log(message, padding: n, dot: dot)
}
```

{% endtab %}
{% endtabs %}
