-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log Coordinator's http internal errors to zap logger
Signed-off-by: Daniel Weiße <[email protected]>
- Loading branch information
1 parent
21c4dca
commit f16b632
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package logging | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/edgelesssys/marblerun/coordinator/constants" | ||
"github.com/edgelesssys/marblerun/util" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// New creates a new [*zap.Logger]. | ||
func New() *zap.Logger { | ||
var cfg zap.Config | ||
if util.Getenv(constants.DevMode, constants.DevModeDefault) == "1" { | ||
cfg = zap.NewDevelopmentConfig() | ||
} else { | ||
cfg = zap.NewProductionConfig() | ||
cfg.DisableStacktrace = true // Disable stacktraces in production | ||
} | ||
log, err := cfg.Build() | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "failed to create logger: %s\n", err) | ||
os.Exit(1) | ||
} | ||
return log | ||
} | ||
|
||
// NewWrapper creates a new [*log.Logger] that writes to the given [*zap.Logger]. | ||
func NewWrapper(zapLogger *zap.Logger) *log.Logger { | ||
return log.New(logWrapper{zapLogger}, "", 0) | ||
} | ||
|
||
// loggerWrapper implements [io.Writer] by writing any data to the error level of the embedded [*zap.Logger]. | ||
type logWrapper struct { | ||
*zap.Logger | ||
} | ||
|
||
func (l logWrapper) Write(p []byte) (n int, err error) { | ||
l.Error(string(p)) | ||
return len(p), nil | ||
} |