Skip to content

Commit

Permalink
zaplog store encoders are not hard coded to a specific format.
Browse files Browse the repository at this point in the history
pprof is enabled with logall.
  • Loading branch information
bengarrett committed Dec 6, 2024
1 parent 3075c4c commit 645b1e8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 34 deletions.
4 changes: 4 additions & 0 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/Defacto2/server/handler/html3"
"github.com/Defacto2/server/handler/htmx"
"github.com/Defacto2/server/internal/config"
"github.com/labstack/echo-contrib/pprof"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"go.uber.org/zap"
Expand Down Expand Up @@ -74,6 +75,9 @@ func (c Configuration) Controller(db *sql.DB, logger *zap.SugaredLogger) *echo.E
}

e := echo.New()
if configs.LogAll {
pprof.Register(e)
}
e.HideBanner = true
e.HTTPErrorHandler = configs.CustomErrorHandler

Expand Down
4 changes: 2 additions & 2 deletions handler/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func (c Configuration) configZapLogger() middleware.RequestLoggerConfig {

logger := zaplog.Status().Sugar()
if c.Environment.ProdMode {
absPath := c.Environment.AbsLog
logger = zaplog.Store(absPath).Sugar()
logPath := c.Environment.AbsLog
logger = zaplog.Store(zaplog.Text(), logPath).Sugar()
}
defer func() {
_ = logger.Sync()
Expand Down
2 changes: 1 addition & 1 deletion internal/config/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (c Config) CustomErrorHandler(err error, ctx echo.Context) {
logger := zaplog.Debug().Sugar()
if c.ProdMode {
root := c.AbsLog
logger = zaplog.Store(root).Sugar()
logger = zaplog.Store(zaplog.Text(), root).Sugar()
}
defer func() {
_ = logger.Sync()
Expand Down
50 changes: 27 additions & 23 deletions internal/zaplog/zaplog.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const (

// Status logger prints all log levels to stdout but without callers.
func Status() *zap.Logger {
enc := consoleNoTime()
enc := TextNoTime()
defaultLogLevel := zapcore.InfoLevel
core := zapcore.NewTee(
zapcore.NewCore(
Expand All @@ -78,7 +78,7 @@ func Status() *zap.Logger {

// Timestamp logger prints all log levels to stdout but without callers.
func Timestamp() *zap.Logger {
enc := consoleWithTime()
enc := Text()
defaultLogLevel := zapcore.InfoLevel
core := zapcore.NewTee(
zapcore.NewCore(
Expand All @@ -92,7 +92,7 @@ func Timestamp() *zap.Logger {

// Debug logger prints all log levels to stdout.
func Debug() *zap.Logger {
enc := consoleWithTime()
enc := Text()
defaultLogLevel := zapcore.DebugLevel
core := zapcore.NewTee(
zapcore.NewCore(
Expand All @@ -104,17 +104,14 @@ func Debug() *zap.Logger {
return zap.New(core, zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel))
}

// Store logger prints all info and higher log levels to files.
// Store logger prints all info and higher log levels to files using the encoder.
// Either the zaplog.JSON, zaplog.Text, or zaplog.TextNoTime encoders can be used.
// Fatal and Panics are also returned to os.Stderr.
func Store(absPath string) *zap.Logger {
config := zap.NewProductionEncoderConfig()
config.EncodeTime = zapcore.TimeEncoderOfLayout("Jan-02-15:04:05.00")
jsonEnc := zapcore.NewJSONEncoder(config)
enc := consoleWithTime()

func Store(enc zapcore.Encoder, logPath string) *zap.Logger {
errs := Text()
// server breakage log
serverWr := zapcore.AddSync(&lumberjack.Logger{
Filename: filepath.Join(absPath, ServerLog),
Filename: filepath.Join(logPath, ServerLog),
MaxSize: MaxSizeMB,
MaxBackups: MaxBackups,
MaxAge: MaxDays,
Expand All @@ -124,37 +121,44 @@ func Store(absPath string) *zap.Logger {

// information and warning log
infoWr := zapcore.AddSync(&lumberjack.Logger{
Filename: filepath.Join(absPath, InfoLog),
Filename: filepath.Join(logPath, InfoLog),
MaxSize: MaxSizeMB,
MaxBackups: MaxBackups,
MaxAge: MaxDays,
})

core := zapcore.NewTee(
// log to stderr
zapcore.NewCore(enc, errWr, zapcore.FatalLevel),
zapcore.NewCore(enc, errWr, zapcore.PanicLevel),
zapcore.NewCore(errs, errWr, zapcore.FatalLevel),
zapcore.NewCore(errs, errWr, zapcore.PanicLevel),
// log to "server.log"
zapcore.NewCore(jsonEnc, serverWr, zapcore.FatalLevel),
zapcore.NewCore(jsonEnc, serverWr, zapcore.PanicLevel),
zapcore.NewCore(jsonEnc, serverWr, zapcore.ErrorLevel),
zapcore.NewCore(enc, serverWr, zapcore.FatalLevel),
zapcore.NewCore(enc, serverWr, zapcore.PanicLevel),
zapcore.NewCore(enc, serverWr, zapcore.ErrorLevel),
// log to "info.log"
zapcore.NewCore(jsonEnc, infoWr, zapcore.WarnLevel),
zapcore.NewCore(jsonEnc, infoWr, zapcore.InfoLevel),
zapcore.NewCore(enc, infoWr, zapcore.WarnLevel),
zapcore.NewCore(enc, infoWr, zapcore.InfoLevel),
)
return zap.New(core, zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel))
}

// console returns a logger in color and time.
func consoleWithTime() zapcore.Encoder {
// Json returns a logger in JSON format.
func Json() zapcore.Encoder {
config := zap.NewProductionEncoderConfig()
config.EncodeTime = zapcore.TimeEncoderOfLayout("Jan-02-15:04:05.00")
return zapcore.NewJSONEncoder(config)
}

// Text returns a logger in color and time.
func Text() zapcore.Encoder {
config := zap.NewDevelopmentEncoderConfig()
config.EncodeTime = zapcore.TimeEncoderOfLayout("15:04:05")
config.EncodeLevel = zapcore.CapitalColorLevelEncoder
return zapcore.NewConsoleEncoder(config)
}

// consoleNoTime returns a logger in color but without the time.
func consoleNoTime() zapcore.Encoder {
// TextNoTime returns a logger in color but without the time.
func TextNoTime() zapcore.Encoder {
config := zap.NewDevelopmentEncoderConfig()
// config.EncodeTime = nil // use nil to remove the leading console separator
config.EncodeTime = zapcore.TimeEncoderOfLayout("")
Expand Down
2 changes: 1 addition & 1 deletion internal/zaplog/zaplog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ func TestLog(t *testing.T) {
}

func TestProduction(t *testing.T) {
logr := zaplog.Store("")
logr := zaplog.Store(zaplog.Json(), "")
assert.NotNil(t, logr)
}
8 changes: 1 addition & 7 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"errors"
"fmt"
"io"

//_ "net/http/pprof" // pprof is used for profiling and can be commented out.
"os"
"runtime"
"slices"
Expand Down Expand Up @@ -109,10 +107,6 @@ func main() {
fmt.Fprintf(w, "%s\n", localIPs)
}()

// go func() {
// log.Println(http.ListenAndServe("localhost:6060", nil))
// }()

// shutdown the web server after a signal is received.
website.ShutdownHTTP(router, logger)
}
Expand Down Expand Up @@ -192,7 +186,7 @@ func serverLog(configs config.Config, count int) *zap.SugaredLogger {
if err := configs.LogStore(); err != nil {
logger.Fatalf("%w using server log: %s", ErrLog, err)
}
logger = zaplog.Store(configs.AbsLog).Sugar()
logger = zaplog.Store(zaplog.Text(), configs.AbsLog).Sugar()
}
return logger
}

0 comments on commit 645b1e8

Please sign in to comment.