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

Enable Color-Coded Logs for SpectacularAPIView and Improve Logging Customization via Django Logger #1292

Open
ashwanthbalakrishnan5 opened this issue Sep 11, 2024 · 3 comments

Comments

@ashwanthbalakrishnan5
Copy link

I just read through thread #866 and I’m relatively new to this codebase.

My Issue

When I call the drf_spectacular.views.SpectacularAPIView view, the error and warning messages are logged in my console. I wanted to enable color coding for these logs.

I found that the drf_spectacular.drainage.GeneratorStats.enable_color method enables this option. However, it’s not enabled by default, and I also noticed that this function is only called in the spectacular management command. How can I enable color coding for console logs outside of this context?

Idea to Improve Customizability for Users

Would it be possible to use the Django logger to emit logs in the drf_spectacular.drainage.GeneratorStats.emit method? This would allow users to customize log formats via Django's logging settings.

@ashwanthbalakrishnan5
Copy link
Author

I just tried this out in my local codebase. @tfranzel Let me know your thoughts on this and I would try to draft a PR based on that.

@tfranzel
Copy link
Owner

Hi,

there is no color option for the views because those messages usually end up in the logs and are harder to read then. Line numbers are not enabled by default because that is an expensive operation.

Its hacky but you can import the generator instance somewhere in your code and enable it by hand since its a global state.

GENERATOR_STATS.enable_color()
GENERATOR_STATS.enable_trace_lineno()

Nobody ever asked for color in the logs of the views in all this time. I do not think it is worth adding options and complicating the view for this rare use-case. For development, if you want nice logs with useful traces, use the manage.py spectacular --color command because that is the usecase it was designed for.

I would entertain the idea of adding logger in addition to print but it has to be generic enough to be useful for most people.

@ashwanthbalakrishnan5
Copy link
Author

Hi,

I would entertain the idea of adding logger in addition to print.

I don’t think we can add a logger in addition to print. We could either replace print with a logger or provide a setting that allows users to switch between them. If we add the logger in addition to print, the log messages would be printed twice in the console, especially on the development server.

Could we add an extra setting in drf_spectacular.settings.spectacular_settings to let users switch between print and a logger? We could also allow users to enable options like colored logs or line number tracing within these settings.

For colored logs, perhaps we can enable them by default when DEBUG=True in the Django settings?

I understand this might not be directly related to the core functionality of this package, and adding these options to the settings could be seen as irrelevant. These are just some ideas I wanted to share.

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