about summary refs log tree commit diff stats
path: root/internal/log/log.go
diff options
context:
space:
mode:
authorAlan Pearce2024-05-19 15:42:36 +0200
committerAlan Pearce2024-05-19 15:53:32 +0200
commit09ee2de72936ea0b0edc5d3360ce1c72803a817b (patch)
tree427a590fba06e58554586aad8995fcb6e7a559f3 /internal/log/log.go
parent4064f933a965f2fb2ce43d3c2d5bd54ccd33978c (diff)
downloadwebsite-09ee2de72936ea0b0edc5d3360ce1c72803a817b.tar.lz
website-09ee2de72936ea0b0edc5d3360ce1c72803a817b.tar.zst
website-09ee2de72936ea0b0edc5d3360ce1c72803a817b.zip
log in logfmt via zap, with nicer console output in dev
Diffstat (limited to 'internal/log/log.go')
-rw-r--r--internal/log/log.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/internal/log/log.go b/internal/log/log.go
new file mode 100644
index 0000000..e16d7bb
--- /dev/null
+++ b/internal/log/log.go
@@ -0,0 +1,48 @@
+package log
+
+import (
+	"os"
+
+	zaplogfmt "github.com/sykesm/zap-logfmt"
+	prettyconsole "github.com/thessem/zap-prettyconsole"
+	"go.uber.org/zap"
+	"go.uber.org/zap/zapcore"
+)
+
+var logger *zap.SugaredLogger
+
+func DPanic(msg string, rest ...any) {
+	logger.DPanicw(msg, rest...)
+}
+func Debug(msg string, rest ...any) {
+	logger.Debugw(msg, rest...)
+}
+func Info(msg string, rest ...any) {
+	logger.Infow(msg, rest...)
+}
+func Warn(msg string, rest ...any) {
+	logger.Warnw(msg, rest...)
+}
+func Error(msg string, rest ...any) {
+	logger.Errorw(msg, rest...)
+}
+func Panic(msg string, rest ...any) {
+	logger.Panicw(msg, rest...)
+}
+func Fatal(msg string, rest ...any) {
+	logger.Fatalw(msg, rest...)
+}
+
+func Configure(isProduction bool) {
+	var l *zap.Logger
+	if isProduction {
+		cfg := zap.NewProductionEncoderConfig()
+		cfg.TimeKey = ""
+		l = zap.New(zapcore.NewCore(zaplogfmt.NewEncoder(cfg), os.Stderr, zapcore.InfoLevel))
+	} else {
+		cfg := prettyconsole.NewConfig()
+		cfg.EncoderConfig.TimeKey = ""
+		l = zap.Must(cfg.Build())
+	}
+	logger = l.WithOptions(zap.AddCallerSkip(1)).Sugar()
+}