Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add instructions for setting up with Vapor web framework #68

Closed
neilhamilton opened this issue Nov 7, 2022 · 4 comments
Closed

Add instructions for setting up with Vapor web framework #68

neilhamilton opened this issue Nov 7, 2022 · 4 comments

Comments

@neilhamilton
Copy link

There is a bit of a gap with Vapor logging to a file on the server. If you search logging in the Vapor Discord, there are quite a few people over the years asking for a way to log to a file and the answer is usually to use any swift-log backend (Vapor's logging is built on top of swift-log)... Unfortunately most of that list is only built for iOS or is too big a dependency to add for simple file logging. After some trial and error with different packages, Puppy was the package that was straight forward and just worked. This has been tested on Xcode 14.1 and Ubuntu 18.04.6 with no issues.

In a Vapor 4 project, add the following to your Sources/Run/main.swift file:

import App
import Vapor
import Puppy

class fileRotationDelegate: FileRotationLoggerDelegate {
    func fileRotationLogger(
        _ fileRotationLogger: FileRotationLogger,
        didArchiveFileURL: URL,
        toFileURL: URL
    ) {
        print("didArchiveFileURL: \(didArchiveFileURL), toFileURL: \(toFileURL)")
    }

    func fileRotationLogger(
        _ fileRotationLogger: FileRotationLogger,
        didRemoveArchivedFileURL: URL
    ) {
        print("didRemoveArchivedFileURL: \(didRemoveArchivedFileURL)")
    }
}

let fileURL = URL(fileURLWithPath: "./Logs/info.log").absoluteURL
let fileRotation = try FileRotationLogger(
    "com.example.yourapp.file",
    fileURL: fileURL,
    filePermission: "600"
)

let delegate = fileRotationDelegate()
fileRotation.delegate = delegate

var puppy = Puppy.default
puppy.add(fileRotation)

var env = try Environment.detect()

try LoggingSystem.bootstrap(from: &env) { (logLevel) -> (String) -> LogHandler in
    return { label -> LogHandler in
        var handler = PuppyLogHandler(label: label, puppy: puppy)
        handler.logLevel = .info
        return handler
    }
}

let app = Application(env)
defer { app.shutdown() }
try configure(app)
try app.run()
@sushichop
Copy link
Owner

Thanks for sharing this.
Yes, Puppy not only works alone, but aslo as a backend for apple/swift-log.
So it should work with any library that uses apple/swift-log as well, not just with Vapor🙂

@sushichop
Copy link
Owner

I have released Puppy 0.6.0.
And I have added instructions to README for setting up with Vapor.

@sushichop
Copy link
Owner

Then I'm going to close this issue.
Please feel free to reopen it if you have any questions.

@neilhamilton
Copy link
Author

Awesome! I'll do a post in the Vapor Discord to spread the word!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants