DaveWoodCom / XCGLogger

A debug log framework for use in Swift projects. Allows you to log details to the console (and optionally a file), just like you would have with NSLog() or print(), but with additional information, such as the date, function name, filename and line number.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AutoRotatingFileDestination uses default value of 10 for targetMaxLogFiles during init()

jalsh opened this issue · comments

commented

Using the below code to set the targetMaxLogFiles to 20, XCGLogger will use a targetMaxLogFiles of 10 in certain conditions and clean up too many log files.

let autoRotatingFileDestination = AutoRotatingFileDestination(writeToFile: logPath, identifier: "advancedLogger.fileDestination", shouldAppend: false)
autoRotatingFileDestination.targetMaxLogFiles = 20

To reproduce: create a scenario where you create 20 log files in an iOS app. The next time you start the logger and run the above code, only 10 log files will be left after the init().

Cause: This is due to entering the rotateFile() (on line 148 of AutoRotatingFileDestination.swift). At the end of rotateFile() it calls into cleanUpLogFiles(). At this point targetMaxLogFiles is still unset and has the default value of 10.

Solution: Making targetMaxLogFiles exclusively a parameter to the init() would resolve this, but I'm sure there's a better solution to be found somewhere.

Notes: This issue will also occur if shouldAppend is true and your current log file would return true on the shouldRotate() call on line 147 (think the conditions are larger than 1MB or being an old log file). This was only tested on iOS but it likely applies to other platforms as well.

I've fixed this by adding a targetMaxLogFiles value to the init(). Seems to be the simplest option.

Thanks.