https://github.com/crspybits/swift-log-file
A simple file logger based on swift-log
https://github.com/crspybits/swift-log-file
filelogger ios
Last synced: 5 months ago
JSON representation
A simple file logger based on swift-log
- Host: GitHub
- URL: https://github.com/crspybits/swift-log-file
- Owner: crspybits
- License: mit
- Created: 2020-10-03T16:12:58.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2024-02-13T19:01:20.000Z (over 2 years ago)
- Last Synced: 2026-02-14T16:40:41.332Z (5 months ago)
- Topics: filelogger, ios
- Language: Swift
- Homepage:
- Size: 19.5 KB
- Stars: 48
- Watchers: 1
- Forks: 17
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# swift-log-file
[SwiftLog](https://github.com/apple/swift-log) compatible file log handler.
This currently only supports iOS.
## Example: Just logging to a file
```swift
let logFileURL = URL(/* your local log file here */)
let logger = try FileLogging.logger(label: "Foobar", localFile: logFileURL)
logger.error("Test Test Test")
```
## Example: Logging to both the standard output (Xcode console if using Xcode) and a file.
```swift
let logFileURL = try getDocumentsDirectory().appendingPathComponent(logFileName)
let fileLogger = try FileLogging(to: logFileURL)
LoggingSystem.bootstrap { label in
let handlers:[LogHandler] = [
FileLogHandler(label: label, fileLogger: fileLogger),
StreamLogHandler.standardOutput(label: label)
]
return MultiplexLogHandler(handlers)
}
let logger = Logger(label: "Test")
```
Note in that last example, if you use `LoggingSystem.bootstrap`, make sure to create your `Logger` *after* the `LoggingSystem.bootstrap` usage (or you won't get the effects of the `LoggingSystem.bootstrap`).
## Example: Using XCGLogger
[XCGLogger](https://github.com/DaveWoodCom/XCGLogger.git) supports rotating file logs amongst other features.
```swift
let logFileURL = URL(/* your local log file here */)
let xcgLogger = /* Make your XCGLogger, using logFileURL */
let logger = XCGLogging.logger(label: "Test", logger: xcgLogger)
logger.error("Test Test Test")
```
## Example: Logging to both the standard output (Xcode console if using Xcode) and a file using XCGLogger.
```swift
let logFileURL = try getDocumentsDirectory().appendingPathComponent(logFileName)
let xcgLogger = /* Make your XCGLogger, using logFileURL */
LoggingSystem.bootstrap { label in
let handlers:[LogHandler] = [
XCGLoggerHandler(label: label, logger: xcgLogger),
StreamLogHandler.standardOutput(label: label)
]
return MultiplexLogHandler(handlers)
}
let logger = Logger(label: "Test")
```
For more examples, see the unit tests and refer to [apple/swift-log's README](https://github.com/apple/swift-log#the-core-concepts)