Unified structured logging
This preview release entirely revamps how logging is handled in a Goyave application. Instead of having multiple std *log.Logger
, a Goyave server now uses a unified *slog.Logger
with some added features. This implementation is based on the standard library log/slog
and maintains compatibility with it. All logs, including the logs generated by Gorm, will all be processed by this new single logger.
By default, the handler used depends on the configuration entry app.debug
. If the debug is enabled, a dev mode log handler is used. This will format logs in a human-readable way. Otherwise the standard JSON handler is used.
Custom handlers are supported: the logger can be replaced entirely when initializing the server.
This change further improves the error handling done in v5.0.0-preview2
. All errors will have additional attributes added into the log entry.
Access logging (using Goyave's log
package and middleware) was also changed to support structured logging and provide useful information.
Breaking changes
Component.Logger()
returns*slog.Logger
instead of*log.Logger
. This means thatPrintf
,Println
, etc are not available anymore. You can now useDebug
,Info
,Warn
,Error
instead.Component.AccessLogger()
andComponent.ErrLogger()
are removed.database.New()
now takes afunc() *slog.Logger
as a parameter. It is a function returning a logger instead of a logger directly, allowing the server logger to be replaced.- Validation components also only have one
*slog.Logger
.
Architecture
The service/repository architecture has been slightly changed.
- The
Init()
function has been removed. - It is encouraged to use interfaces to represent repositories inside services instead of the actual implementation. This removes coupling between the two and makes testing easier. This also completely detaches the domain logic and database layer from the HTTP layer.
- Repositories and services are now initialized in
main.go
. This makes for a more verbose main function, but is a good improvements for code quality. - It is encouraged to initialize services and repositories using a
New()
function.
Misc
- Fixed a bug in model mapping when copying
typeutil.Undefined
into a normal field. - The error logger of the underlying
http.Server
now always prints to the new unified logger, even if it is replaced. - The built-in
auth.JWTService
has been updated to the new service architecture. - You may need to add the following replace directive in your go.mod for the project to compile:
replace github.com/jinzhu/copier v0.3.6-0.20230809063302-70b1d4e41a98 => github.com/System-Glitch/copier v0.0.0-20230810143212-ca7f7fa385a2
This is temporary and will not be needed on future releases.
Full Changelog: v5.0.0-preview2...v5.0.0-preview3