Skip to content

Commit

Permalink
File logging: stdout and stderr (#105)
Browse files Browse the repository at this point in the history
Use the OS stdout and stderr for /dev/stdout and
/dev/stderr.
I case of systemd stdout and stderr might be redirected
to a unix socket and the file open fails.
Using the OS stdout and stderr avoids that problem.

Signed-off-by: Sven Auhagen <[email protected]>
  • Loading branch information
svenauhagen committed Apr 16, 2024
1 parent 5ef1e9d commit 63b6a5e
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions internal/spoa.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"strings"
"time"
"io"

"github.com/bluele/gcache"
"github.com/corazawaf/coraza-spoa/config"
Expand Down Expand Up @@ -161,10 +162,21 @@ func New(conf *config.Config) (*SPOA, error) {
if err != nil {
level = zap.InfoLevel
}
f, err := os.OpenFile(cfg.LogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
return nil, err

var f io.Writer
switch cfg.LogFile {
case "/dev/stdout":
f = os.Stdout
case "/dev/stderr":
f = os.Stderr
default:
ff, err := os.OpenFile(cfg.LogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
return nil, err
}
f = ff
}

core := zapcore.NewTee(
zapcore.NewCore(fileEncoder, zapcore.AddSync(f), level),
)
Expand Down

0 comments on commit 63b6a5e

Please sign in to comment.