@@ -53,6 +53,7 @@ import (
5353 "github.com/dagu-org/dagu/internal/service/audit"
5454 authservice "github.com/dagu-org/dagu/internal/service/auth"
5555 "github.com/dagu-org/dagu/internal/service/coordinator"
56+ "github.com/dagu-org/dagu/internal/service/frontend/api/pathutil"
5657 apiv1 "github.com/dagu-org/dagu/internal/service/frontend/api/v1"
5758 "github.com/dagu-org/dagu/internal/service/frontend/auth"
5859 "github.com/dagu-org/dagu/internal/service/frontend/metrics"
@@ -633,28 +634,65 @@ func redactTokenFromRequest(r *http.Request) *http.Request {
633634 return redacted
634635}
635636
637+ // buildPublicPaths returns the set of public endpoint paths that should be
638+ // excluded from access logging in "non-public" mode.
639+ func buildPublicPaths (basePath string , metrics config.MetricsAccess ) map [string ]struct {} {
640+ paths := []string {
641+ pathutil .BuildPublicEndpointPath (basePath , "api/v1/health" ),
642+ pathutil .BuildPublicEndpointPath (basePath , "api/v1/auth/login" ),
643+ pathutil .BuildPublicEndpointPath (basePath , "api/v1/auth/setup" ),
644+ }
645+ if metrics == config .MetricsAccessPublic {
646+ paths = append (paths , pathutil .BuildPublicEndpointPath (basePath , "api/v1/metrics" ))
647+ }
648+ set := make (map [string ]struct {}, len (paths ))
649+ for _ , p := range paths {
650+ set [p ] = struct {}{}
651+ }
652+ return set
653+ }
654+
655+ // skipPathsMiddleware wraps a middleware to skip it for requests matching any of the given paths.
656+ func skipPathsMiddleware (mw func (http.Handler ) http.Handler , skip map [string ]struct {}) func (http.Handler ) http.Handler {
657+ return func (next http.Handler ) http.Handler {
658+ wrapped := mw (next )
659+ return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
660+ if _ , ok := skip [r .URL .Path ]; ok {
661+ next .ServeHTTP (w , r )
662+ return
663+ }
664+ wrapped .ServeHTTP (w , r )
665+ })
666+ }
667+ }
668+
636669// Serve starts the HTTP server and configures routes.
637670func (srv * Server ) Serve (ctx context.Context ) error {
638- logLevel := slog .LevelInfo
639- if srv .config .Core .Debug {
640- logLevel = slog .LevelDebug
641- }
642-
643- requestLogger := httplog .NewLogger ("http" , httplog.Options {
644- LogLevel : logLevel ,
645- JSON : srv .config .Core .LogFormat == "json" ,
646- Concise : true ,
647- RequestHeaders : srv .config .Core .Debug ,
648- MessageFieldName : "msg" ,
649- ResponseHeaders : false ,
650- QuietDownRoutes : []string {"/api/v1/events" },
651- QuietDownPeriod : 10 * time .Second ,
652- })
653-
654671 r := chi .NewMux ()
655672 r .Use (middleware .RealIP )
656673 r .Use (middleware .Compress (5 ))
657- r .Use (sanitizedRequestLogger (requestLogger ))
674+ if srv .config .Server .AccessLog != config .AccessLogNone {
675+ logLevel := slog .LevelInfo
676+ if srv .config .Core .Debug {
677+ logLevel = slog .LevelDebug
678+ }
679+ requestLogger := httplog .NewLogger ("http" , httplog.Options {
680+ LogLevel : logLevel ,
681+ JSON : srv .config .Core .LogFormat == "json" ,
682+ Concise : true ,
683+ RequestHeaders : srv .config .Core .Debug ,
684+ MessageFieldName : "msg" ,
685+ ResponseHeaders : false ,
686+ QuietDownRoutes : []string {"/api/v1/events" },
687+ QuietDownPeriod : 10 * time .Second ,
688+ })
689+ logMiddleware := sanitizedRequestLogger (requestLogger )
690+ if srv .config .Server .AccessLog == config .AccessLogNonPublic {
691+ skipPaths := buildPublicPaths (srv .config .Server .BasePath , srv .config .Server .Metrics )
692+ logMiddleware = skipPathsMiddleware (logMiddleware , skipPaths )
693+ }
694+ r .Use (logMiddleware )
695+ }
658696 r .Use (middleware .Recoverer )
659697 r .Use (cors .Handler (cors.Options {
660698 AllowedOrigins : []string {"*" },
0 commit comments