{"id":13465864,"url":"https://github.com/sushichop/Puppy","last_synced_at":"2025-03-25T20:34:41.517Z","repository":{"id":41838675,"uuid":"305063565","full_name":"sushichop/Puppy","owner":"sushichop","description":"A flexible logging library written in Swift","archived":false,"fork":false,"pushed_at":"2024-04-20T04:45:25.000Z","size":199,"stargazers_count":148,"open_issues_count":8,"forks_count":28,"subscribers_count":5,"default_branch":"main","last_synced_at":"2024-05-30T00:04:18.967Z","etag":null,"topics":["cross-platform","darwin","ios","linux","logging","macos","server-side-swift","swift","swift-log","windows"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sushichop.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-10-18T09:14:17.000Z","updated_at":"2024-07-31T15:02:00.272Z","dependencies_parsed_at":"2024-07-31T15:12:01.242Z","dependency_job_id":null,"html_url":"https://github.com/sushichop/Puppy","commit_stats":{"total_commits":99,"total_committers":6,"mean_commits":16.5,"dds":0.07070707070707072,"last_synced_commit":"b5af02a72a5a1f92a68e6eceee19cac804067ad9"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sushichop%2FPuppy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sushichop%2FPuppy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sushichop%2FPuppy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sushichop%2FPuppy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sushichop","download_url":"https://codeload.github.com/sushichop/Puppy/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222091638,"owners_count":16929668,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cross-platform","darwin","ios","linux","logging","macos","server-side-swift","swift","swift-log","windows"],"created_at":"2024-07-31T15:00:36.384Z","updated_at":"2024-10-29T18:30:14.137Z","avatar_url":"https://github.com/sushichop.png","language":"Swift","funding_links":[],"categories":["Libs","Swift","Logging [🔝](#readme)"],"sub_categories":["Logging"],"readme":"# Puppy\n\n![Swift5.6+](https://img.shields.io/badge/Swift-5.6%2B-orange.svg?style=flat)\n[![release](https://img.shields.io/github/v/release/sushichop/Puppy.svg?color=blue)](https://github.com/sushichop/Puppy/releases)\n[![CocoaPods](https://img.shields.io/cocoapods/v/Puppy.svg?color=blue)](https://cocoapods.org/pods/Puppy)\n![CI](https://github.com/sushichop/Puppy/workflows/CI/badge.svg)\n[![codecov](https://codecov.io/gh/sushichop/Puppy/branch/main/graph/badge.svg)](https://codecov.io/gh/sushichop/Puppy)\n[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/sushichop/Puppy/blob/master/LICENSE)\n\n![platforms](https://img.shields.io/badge/Platforms-macOS%20%7C%20iOS%20%7C%20tvOS%20%7C%20watchOS%20%7C%20Linux%20%7C%20Windows-orange.svg?style=flat)\n![SwiftPM|CMake|Bazel|Carthage](https://img.shields.io/badge/SwiftPM%20%7C%20CMake%20%7C%20Bazel%20%7C%20Carthage-compatible-4BC51D.svg?style=flat)\n\n### **Puppy is a flexible logging library written in Swift** 🐶\n\nIt supports multiple transports(console, file, syslog, and oslog) as loggers. It not only works alone, but also as a backend for [apple/swift-log](https://github.com/apple/swift-log/).\n\nFurthermore, it has file log rotation feature and you can also customize the log format as you like. And it supports **cross-platform(Darwin, Linux, and Windows)**.\n\n## Features\n\n- Written in Swift.\n- Supports cross-platform(Darwin, Linux, and Windows).\n- Supports console, file, syslog, and oslog as loggers.\n- Supports automatic log rotation about file logger.\n- Also Works as a backend for `apple/swift-log`.\n\n## Examples\n\n### Basic Usage\n\nLogging to mutliple transports(e.g. console and file). It is recommended that the first argument of each logger be a unique reverse-order FQDN since it is also used internally for a `DispatchQueue`'s label.\n\n```Swift\nimport Puppy\n\nlet console = ConsoleLogger(\"com.example.yourapp.console\", logLevel: .info)\nlet fileURL = URL(fileURLWithPath: \"./foo.log\").absoluteURL\nlet file = FileLogger(\"com.example.yourapp.file\",\n                      logLevel: .info,\n                      fileURL: fileURL,\n                      filePermission: \"600\")  // Default permission is \"640\". \n\nvar log = Puppy()\nlog.add(console)\nlog.add(file)\n\nlog.debug(\"DEBUG message\")  // Will NOT be logged.\nlog.info(\"INFO message\")    // Will be logged.\nlog.error(\"ERROR message\")  // Will be logged.\n```\n\n### Use file log rotation\n\nLogging to file and use log rotation feature.\n\n```swift\nimport Puppy\n\nclass ViewController: UIViewController {\n    let fileURL = URL(fileURLWithPath: \"./logs/foo.log\").absoluteURL\n    let rotationConfig = RotationConfig(suffixExtension: .date_uuid,\n                                        maxFileSize: 10 * 1024 * 1024,\n                                        maxArchivedFilesCount: 3)\n    let delegate = SampleFileRotationDelegate()\n    let fileRotation = try! FileRotationLogger(\"com.example.yourapp.filerotation\",\n                                                fileURL: fileURL,\n                                                rotationConfig: rotationConfig,\n                                                delegate: delegate)\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        var log = Puppy()\n        log.add(fileRotation)\n        log.info(\"INFO message\")\n        log.warning(\"WARNING message\")\n    }\n}\n\nclass SampleFileRotationDelegate: FileRotationLoggerDelegate {\n    func fileRotationLogger(_ fileRotationLogger: FileRotationLogger,\n                            didArchiveFileURL: URL, toFileURL: URL) {\n        print(\"didArchiveFileURL: \\(didArchiveFileURL), toFileURL: \\(toFileURL)\")\n    }\n    func fileRotationLogger(_ fileRotationLogger: FileRotationLogger,\n                            didRemoveArchivedFileURL: URL) {\n        print(\"didRemoveArchivedFileURL: \\(didRemoveArchivedFileURL)\")\n    }\n}\n```\n\n### Use with [apple/swift-log](https://github.com/apple/swift-log/)\n\nLogging to multiple transports(e.g. console and syslog) as a backend for `apple/swift-log`.\n\n```swift\nimport Puppy\n\nlet console = ConsoleLogger(\"com.example.yourapp.console\")\nlet syslog = SystemLogger(\"com.example.yourapp.syslog\")\n\nvar puppy = Puppy()\npuppy.add(console)\npuppy.add(syslog)\n\nLoggingSystem.bootstrap {\n    var handler = PuppyLogHandler(label: $0, puppy: puppy)\n    // Set the logging level.\n    handler.logLevel = .trace\n    return handler\n}\n\nvar log = Logger(label: \"com.example.yourapp.swiftlog\")\n\nlog.trace(\"TRACE message\")  // Will be logged.\nlog.debug(\"DEBUG message\")  // Will be logged.\n```\n\nHere is a practical example of using `Puppy` with [Vapor](https://vapor.codes), which uses `apple/swift-log` internally.\n\n```swift\nimport App\nimport Vapor  // Vapor 4.67.4\nimport Puppy\n\nlet fileURL = URL(fileURLWithPath: \"./server-logs/bar.log\").absoluteURL\nlet rotationConfig = RotationConfig(suffixExtension: .numbering,\n                                    maxFileSize: 30 * 1024 * 1024,\n                                    maxArchivedFilesCount: 5)\nlet fileRotation = try FileRotationLogger(\"com.example.yourapp.server\",\n                                          fileURL: fileURL,\n                                          rotationConfig: rotationConfig)\nvar puppy = Puppy()\npuppy.add(fileRotation)\n\n// https://docs.vapor.codes/basics/logging/\nvar env = try Environment.detect()\ntry LoggingSystem.bootstrap(from: \u0026env) { (logLevel) -\u003e (String) -\u003e LogHandler in\n    return { label -\u003e LogHandler in\n        var handler = PuppyLogHandler(label: label, puppy: puppy)\n        handler.logLevel = .info\n        return handler\n    }\n}\nlet app = Application(env)\ndefer { app.shutdown() }\ntry configure(app)\ntry app.run()\n```\n\n### Customize the logging format\n\nCustomize the log format using `Formattable` protocol. Logging to oslog for example.\n\n```swift\nimport Puppy\n\nclass ViewController: UIViewController {\n    let logFormat = LogFormatter()\n    let oslog = OSLogger(\"com.yourapp.oslog\", logFormat: logFormat)\n\n    override func viewDidLoad() {\n        super.viewDidLoad()\n        var log = Puppy()\n        log.add(oslog)\n        log.info(\"INFO message\")\n        log.warning(\"WARNING message\")\n    }\n}\n\nstruct LogFormatter: LogFormattable {\n    private let dateFormat = DateFormatter()\n\n    init() {\n        dateFormat.dateFormat = \"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ\"\n    }\n\n    func formatMessage(_ level: LogLevel, message: String, tag: String, function: String,\n                       file: String, line: UInt, swiftLogInfo: [String : String],\n                       label: String, date: Date, threadID: UInt64) -\u003e String {\n        let date = dateFormatter(date, withFormatter: dateFormat)\n        let fileName = fileName(file)\n        let moduleName = moduleName(file)\n        return \"\\(date) \\(threadID) [\\(level.emoji) \\(level)] \\(swiftLogInfo) \\(moduleName)/\\(fileName)#L.\\(line) \\(function) \\(message)\".colorize(level.color)\n    }\n}\n```\n\n### Create a custom logger\n\nYou can also create your own custom logger. The custom logger needs to conform to `Loggerable` protocol.\n\n```swift\n@preconcurrency import Dispatch\nimport Puppy\n\npublic struct CustomLogger: Loggerable {\n    public let label: String\n    public let queue: DispatchQueue\n    public let logLevel: LogLevel\n    public let logFormat: LogFormattable?\n\n    public init(_ label: String, logLevel: LogLevel = .trace, logFormat: LogFormattable? = nil) {\n        self.label = label\n        self.queue = DispatchQueue(label: label)\n        self.logLevel = logLevel\n        self.logFormat = logFormat\n    }\n\n    public func log(_ level: LogLevel, string: String) {\n        // Implements the logging feature here.\n    }\n}\n```\n\n## License\n\nPuppy is available under the [MIT license](http://www.opensource.org/licenses/mit-license). See the LICENSE file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsushichop%2FPuppy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsushichop%2FPuppy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsushichop%2FPuppy/lists"}