tfranzel / drf-spectacular

Sane and flexible OpenAPI 3 schema generation for Django REST framework.
https://drf-spectacular.readthedocs.io
BSD 3-Clause "New" or "Revised" License
2.41k stars 266 forks source link

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

Open ashwanthbalakrishnan5 opened 2 months ago

ashwanthbalakrishnan5 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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.