-
-
Notifications
You must be signed in to change notification settings - Fork 93
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
feat: Added integration with the built-in logging package #440
base: main
Are you sure you want to change the base?
Conversation
Hey, first of all, thank you for your contribution. A couple of things to start with:
|
Thanks for the feedback, I'll look into everything you said and update my branch accordingly. I'll let you know when I have something worth reviewing! |
…ogging.py, and renamed Handler subclass to CleoHandler
The non-automatic integration is giving me a bit of a headache. The CleoHandler class needs a reference to the application's Any thoughts on how to proceed? I have two approaches in mind:
|
Erm forget that last comment, I'm dumb, obviously I can just create my Output object and pass it to the Application.run function. Duh 🙃 |
Integration is now fully handled on the user side. I've updated the tests to reflect this, and this is a working example of user code: import logging
import sys
from cleo.application import Application
from cleo.io.outputs.stream_output import StreamOutput
from cleo.logging.cleo_handler import CleoHandler
from tests.fixtures.foo4_command import Foo4Command
if __name__ == "__main__":
# Configure a cleo single-command application
app = Application()
cmd = Foo4Command()
app.add(cmd)
app._default_command = cmd.name
# Create the custom handler that'll redirect logging's prints to be run through Cleo's output
output = StreamOutput(sys.stdout)
handler = CleoHandler(output)
# The user can set their format as usual, albeit with support for cleo format strings!
fmt = logging.Formatter(
"<fg=light_gray;options=reverse>%(asctime)s</> - %(levelname)s: %(message)s",
datefmt='%m/%d/%Y %I:%M:%S %p',
)
handler.setFormatter(fmt)
# Register the handler
root = logging.getLogger()
root.addHandler(handler)
root.setLevel(logging.NOTSET) # IMPORTANT so that the root logger respects cleo's verbosity levels
# run the cleo application
app.run(output=output, error_output=output) I'll start looking into what Rich do for formatting and continue updating this PR |
Thanks for working on that @dylan-robins, it looks great so far. |
Closes: #49
This PR integrates the built-in
logging
package, as listed in the Cleo 3.0 writeup (#415 ). Implementation is of course subject to change, but I thought I'd get the ball rolling :)Here's what I did:
LogRecords
to acleo.io.outputs.output.Output
cleo.application.Application
which attaches anOutputHandler
to the root logger, and configures it to have a log level that is coherent with Cleo's one.Here are some open points that I know we need to discuss:
logging
handler have some sort of prefix or something to distinguish them from what is coming directly fromoutput.write_line()
?