Skip to content

Commit

Permalink
Add log.
Browse files Browse the repository at this point in the history
  • Loading branch information
googollee committed Apr 15, 2024
1 parent cbca37a commit 16d770b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
19 changes: 19 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package espresso_test

import (
"bytes"
"context"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"net/http/httptest"
"os"

"github.com/googollee/go-espresso"
"github.com/googollee/go-espresso/codec"
Expand All @@ -28,6 +31,18 @@ func ExampleEspresso() {
}

espo := espresso.New()
// Log to stdout for Output
espo.AddModule(espresso.LogModule.ProvideWithFunc(func(ctx context.Context) (*slog.Logger, error) {
return slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
// Remove time from the output for predictable test output.
if a.Key == slog.TimeKey {
return slog.Attr{}
}
return a
},
})), nil
}))
espo.AddModule(codec.Provider)

espo.HandleFunc(func(ctx espresso.Context) error {
Expand Down Expand Up @@ -122,6 +137,10 @@ func ExampleEspresso() {
}()

// Output:
// level=INFO msg="receive http" method=GET path=/book/1
// level=INFO msg="finish http" method=GET path=/book/1
// Book 1 title: The Espresso Book
// level=INFO msg="receive http" method=POST path=/book/1
// level=INFO msg="finish http" method=POST path=/book/1
// The New Book id: 2
}
69 changes: 69 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package espresso

import (
"context"
"log/slog"
"os"

"github.com/googollee/go-espresso/module"
)

var (
LogModule = module.New[*slog.Logger]()
LogText = LogModule.ProvideWithFunc(func(ctx context.Context) (*slog.Logger, error) {
handler := slog.NewTextHandler(os.Stderr, nil)
return slog.New(handler), nil
})
LogJSON = LogModule.ProvideWithFunc(func(ctx context.Context) (*slog.Logger, error) {
handler := slog.NewJSONHandler(os.Stderr, nil)
return slog.New(handler), nil
})
)

func DEBUG(ctx context.Context, msg string, args ...any) {
logger := LogModule.Value(ctx)
if logger == nil {
return
}

logger.DebugContext(ctx, msg, args...)
}

func INFO(ctx context.Context, msg string, args ...any) {
logger := LogModule.Value(ctx)
if logger == nil {
return
}

logger.InfoContext(ctx, msg, args...)
}

func WARN(ctx context.Context, msg string, args ...any) {
logger := LogModule.Value(ctx)
if logger == nil {
return
}

logger.WarnContext(ctx, msg, args...)
}

func ERROR(ctx context.Context, msg string, args ...any) {
logger := LogModule.Value(ctx)
if logger == nil {
return
}

logger.ErrorContext(ctx, msg, args...)
}

func logHandling(ctx Context) error {
method := ctx.Request().Method
path := ctx.Request().URL.String()

INFO(ctx, "receive http", "method", method, "path", path)
defer INFO(ctx, "finish http", "method", method, "path", path)

ctx.Next()

return nil
}
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func New() *Espresso {
mux: ret.mux,
}

ret.Use(cacheAllError)
ret.Use(logHandling, cacheAllError)

return ret
}
Expand Down

0 comments on commit 16d770b

Please sign in to comment.