Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"

"github.com/compliance-framework/api/internal/config"
"github.com/compliance-framework/api/internal/logging"
"github.com/compliance-framework/api/internal/service"
"github.com/compliance-framework/api/internal/service/digest"
"github.com/compliance-framework/api/internal/service/email"
Expand Down Expand Up @@ -53,7 +54,7 @@ func runDigestTest(cmd *cobra.Command, args []string) {
}

defer func() {
if err := sugar.Sync(); err != nil {
if err := sugar.Sync(); !logging.IgnoreSyncError(err) {
log.Printf("failed to sync zap logger: %v", err)
}
}()
Expand Down Expand Up @@ -144,7 +145,7 @@ func runDigestPreview(cmd *cobra.Command, args []string) {
}

defer func() {
if err := sugar.Sync(); err != nil {
if err := sugar.Sync(); !logging.IgnoreSyncError(err) {
log.Printf("failed to sync zap logger: %v", err)
}
}()
Expand Down
3 changes: 2 additions & 1 deletion cmd/riskdigest.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/compliance-framework/api/internal/config"
"github.com/compliance-framework/api/internal/logging"
"github.com/compliance-framework/api/internal/service"
"github.com/compliance-framework/api/internal/service/email"
slacksvc "github.com/compliance-framework/api/internal/service/slack"
Expand Down Expand Up @@ -47,7 +48,7 @@ func runRiskDigestTest(cmd *cobra.Command, args []string) {
}

defer func() {
if err := sugar.Sync(); err != nil {
if err := sugar.Sync(); !logging.IgnoreSyncError(err) {
log.Printf("failed to sync zap logger: %v", err)
}
}()
Expand Down
3 changes: 2 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/compliance-framework/api/internal/api/handler/auth"
"github.com/compliance-framework/api/internal/api/handler/oscal"
"github.com/compliance-framework/api/internal/config"
"github.com/compliance-framework/api/internal/logging"
"github.com/compliance-framework/api/internal/service"
"github.com/compliance-framework/api/internal/service/digest"
"github.com/compliance-framework/api/internal/service/email"
Expand Down Expand Up @@ -45,7 +46,7 @@ func RunServer(cmd *cobra.Command, args []string) {
}

defer func() {
if err := sugar.Sync(); err != nil {
if err := sugar.Sync(); !logging.IgnoreSyncError(err) {
log.Printf("failed to sync zap logger: %v", err)
}
}()
Expand Down
3 changes: 2 additions & 1 deletion cmd/users/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/big"

"github.com/compliance-framework/api/internal/config"
"github.com/compliance-framework/api/internal/logging"
"github.com/compliance-framework/api/internal/service"
"github.com/compliance-framework/api/internal/service/relational"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -45,7 +46,7 @@ func addUser(cmd *cobra.Command, args []string) {
cobra.CheckErr(err)
defer func() {
err := logger.Sync()
if err != nil {
if !logging.IgnoreSyncError(err) {
println("failed to sync logger:", err.Error())
}
}()
Expand Down
3 changes: 2 additions & 1 deletion cmd/users/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/compliance-framework/api/internal/config"
"github.com/compliance-framework/api/internal/logging"
"github.com/compliance-framework/api/internal/service"
"github.com/compliance-framework/api/internal/service/relational"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -40,7 +41,7 @@ func updateUser(cmd *cobra.Command, args []string) {
cobra.CheckErr(err)
defer func() {
err := logger.Sync()
if err != nil {
if !logging.IgnoreSyncError(err) {
println("failed to sync logger:", err.Error())
}
}()
Expand Down
14 changes: 14 additions & 0 deletions internal/logging/sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package logging

import (
"errors"
"syscall"
)

// IgnoreSyncError filters the known benign sync errors returned when zap flushes
// stdout/stderr-backed loggers on some platforms and runtimes.
func IgnoreSyncError(err error) bool {
return err == nil ||
errors.Is(err, syscall.EINVAL) ||
errors.Is(err, syscall.ENOTTY)
}
30 changes: 30 additions & 0 deletions internal/logging/sync_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package logging

import (
"errors"
"syscall"
"testing"
)

// Test the catching of sync errors
func TestIgnoreSyncError(t *testing.T) {
tests := []struct {
name string
err error
want bool
}{
{name: "nil", err: nil, want: true},
{name: "EINVAL", err: syscall.EINVAL, want: true},
{name: "wrapped EINVAL", err: errors.New("sync: " + syscall.EINVAL.Error()), want: false},
{name: "ENOTTY", err: syscall.ENOTTY, want: true},
{name: "other", err: syscall.EBADF, want: false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IgnoreSyncError(tt.err); got != tt.want {
t.Fatalf("IgnoreSyncError(%v) = %v, want %v", tt.err, got, tt.want)
}
})
}
}
Loading