# swift4自定义Log的简单实现

**URL:** <https://www.shiqidu.com/t/topic/267>\
**Category:** 开发调优\
**Tags:** Swift\
**Created:** [2017 年11 月 5 日 16:45 UTC](https://www.shiqidu.com/t/topic/267 "2017-11-05T16:45:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

作者： ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
发布日期： [2017 年11 月 5 日 16:45 UTC](https://www.shiqidu.com/t/topic/267/1 "2017-11-05T16:45:20Z")

</div>

要求：

- 显示文件名
- 显示行号
- debug模式下打印log，release模式不打印

首先创建一个flag

![071e2e30ad4d53d700eed112594c3ad0.png](https://discourse.shiqidu.com/uploads_nj/article/20171106/071e2e30ad4d53d700eed112594c3ad0.png)

代码就非常简单啦

```swift
func myLog&lt;T&gt;(_ message: T, file: String = #file, lineNumber: Int = #line) {  
    #if DEBUG  
          
    let file = (file as NSString).lastPathComponent  
    print(&quot;\(file)[\(lineNumber)]: - \(message)&quot;)  
          
    #endif  
}  

```
