Skip to content

2. Advanced Usage

Damon东哥 edited this page Mar 25, 2025 · 2 revisions

1. Display Logs in Xcode Console

DDLoggerSwift.isSyncConsole = true
  • Default is true.
  • When set to false, logs will only be written to the database and will not appear in Xcode’s debug console.

2. Separate Log Folders for Different Users

DDLoggerSwift.userID = "0"
  • Assigning different userIDs allows logs to be stored in separate folders.
  • The default userID is 0.

3. Clear Logs Displayed in Floating Window

DDLoggerSwift.cleanLog()
  • Clears only the displayed logs without deleting log files in the database.

4. Hide Log Floating Window

DDLoggerSwift.hide()

5. Completely Close the Log Window

DDLoggerSwift.close()

6. Set Log File Retention Period

DDLoggerSwift.logExpiryDay = 90
  • Default is 90 days.
  • Set to 0 for unlimited retention.
  • Log database files are saved per day, and expired logs are automatically deleted.

7. Delete Log Database Files

DDLoggerSwift.deleteLogFile()
  • Can delete logs for a specific date.
  • If no date is specified, all logs for the current user will be deleted.

8. Configure Log Levels for Storage

Only logs of the selected levels will be saved in the database.
By default, debug logs are not stored.

DDLoggerSwift.storageLevels = [.info, .warn, .error, .privacy]

9. Change Log File Storage Directory

DDLoggerSwift.DBParentFolder = DDUtils.shared.getFileDirectory(type: .documents)
  • By default, logs are stored in the Documents folder.
  • To change the storage location (e.g., Caches folder):
DDLoggerSwift.DBParentFolder = DDUtils.shared.getFileDirectory(type: .caches)

10. Retrieve Stored Log Data

DDLoggerSwift.getAllLog()
  • Can fetch logs based on date, keywords, log type, pagination, etc.

11. Share Log Files

DDLoggerSwift.showShare()
  • Generates a DDLoggerSwift.log file containing the selected date’s logs.
  • Opens the system share menu to easily share logs via Twitter, WeChat, etc.

12. Upload Log Database Files

1. Let Users Upload Logs Manually

Similar to the sharing function:

DDLoggerSwift.showUpload()
  • When users confirm the upload, the callback uploadComplete is triggered:
DDLoggerSwift.uploadComplete = { file in
     print(file)
     // Handle file upload
}

2. Custom File Retrieval

1) Retrieve a Log File for a Specific Date
DDLoggerSwift.getDBFile(date: Date)

This allows retrieving a log file for a specific date, which can then be uploaded manually.

2) Retrieve All Log Files

To access all logs, get the folder containing log database files:

DDLoggerSwift.getDBFolder()

Sorting log files:

let dbFolder = DDLoggerSwift.getDBFolder()
        
if let enumer = FileManager.default.enumerator(atPath: dbFolder.path) {
    while let file = enumer.nextObject() {
         if let file: String = file as? String {
            if file.hasSuffix(".db") {
                // Retrieve the specific log file path
                let logFilePath = dbFolder.appendingPathComponent(file, isDirectory: false)
             }
         }
    }
}

13. Throttling Log Writes

SQLite supports high-concurrency read/write operations without additional configuration.
However, if logs are generated at a very high frequency (e.g., millions per second), throttling can improve efficiency.

DDLoggerSwift.throttleTime = 2
  • Default is 0 (no throttling).
  • A higher value (e.g., 2 seconds) allows batch writing, which improves performance but introduces a slight delay.

14. Paginate Logs in Floating Window

DDLoggerSwift.maxPageSize = 10000
  • Default: 10,000 logs per page.
  • Set to 0 to disable pagination.
  • If logs exceed millions per day, loading all logs at once can cause delays.
  • Enabling pagination improves performance by fetching more logs as the user scrolls.

15. Limit Text Display in Log Window Cells

DDLoggerSwift.cellDisplayCount = 600
  • The log viewer automatically adjusts cell height.
  • However, very large JSON logs may cause UI lag.
  • This setting truncates long logs and provides a "View More" button.