Skip to content

Commit

Permalink
feat. update
Browse files Browse the repository at this point in the history
  • Loading branch information
PotatoCloud committed Sep 29, 2024
1 parent 7387001 commit 6b156c4
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 27 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## structured logging
_All usage of this package is the same as `log/slog`_

### install
```shell
go get github.com/RealFax/slog@latest
```

### performance
you can use build tags to declare which JSON serialization method to use

e.g., use the jsoniter flag to declare the use of `json-iterator/go`

### environment variables

#### LOG_PURE
type: boolean

This variable declares whether colorful output is disabled.

e.g., LOG_PURE=true

#### LOG_LEVEL
type: string

- debug
- info
- warn
- error

default: info
e.g., LOG_LEVEL=debug || LOG_LEVEL=DEBUG
27 changes: 5 additions & 22 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package log

import (
"bytes"
"encoding/json"
"fmt"
"log/slog"
"sync"
Expand All @@ -27,22 +26,6 @@ func buildOutput(bs ...[]byte) []byte {
return b
}

//func suppressDefaults(
// next func([]string, slog.Attr) slog.Attr,
//) func([]string, slog.Attr) slog.Attr {
// return func(groups []string, a slog.Attr) slog.Attr {
// if a.Method == slog.TimeKey ||
// a.Method == slog.LevelKey ||
// a.Method == slog.MessageKey {
// return slog.Attr{}
// }
// if next == nil {
// return a
// }
// return next(groups, a)
// }
//}

func putAttr(attrs Attrs, attr slog.Attr) {
var v any
switch attr.Value.Kind() {
Expand All @@ -62,19 +45,19 @@ func putAttr(attrs Attrs, attr slog.Attr) {
v = attr.Value.Time()
case slog.KindAny:
switch x := attr.Value.Any().(type) {
case json.Marshaler:
b, _ := json.Marshal(x)
v = json.RawMessage(b)
case Marshaler:
b, _ := Marshal(x)
v = RawMessage(b)
case fmt.Stringer:
v = x.String()
case error:
v = x.Error()
default:
b, err := json.Marshal(x)
b, err := Marshal(x)
if err != nil {
panic(fmt.Sprintf("bad kind any: %s", attr.Value.Kind()))
}
v = json.RawMessage(b)
v = RawMessage(b)
}
default:
panic(fmt.Sprintf("bad kind: %s", attr.Value.Kind()))
Expand Down
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
module github.com/RealFax/slog

go 1.23

require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
7 changes: 3 additions & 4 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package log

import (
"context"
"encoding/json"
"io"
"log/slog"
"runtime"
Expand Down Expand Up @@ -47,16 +46,16 @@ func (a Attrs) String() string {
}

if ReleaseMode {
b, _ := json.Marshal(a)
b, _ := Marshal(a)
return string(b)
}
b, _ := json.MarshalIndent(a, "", " ")
b, _ := MarshalIndent(a, "", " ")
return string(b)
}

func (r Record) Bytes() []byte {
if ReleaseMode {
b, _ := json.Marshal(r)
b, _ := Marshal(r)
return append(b, '\n')
}
bs := [][]byte{
Expand Down
20 changes: 20 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build jsoniter

package log

import (
jsoniter "github.com/json-iterator/go"
)

type (
Marshaler = jsoniter.Marshaler
RawMessage = jsoniter.RawMessage
)

func Marshal(v any) ([]byte, error) {
return jsoniter.ConfigFastest.Marshal(v)
}

func MarshalIndent(v any, prefix, indent string) ([]byte, error) {
return jsoniter.ConfigFastest.MarshalIndent(v, prefix, indent)
}
18 changes: 18 additions & 0 deletions json_std.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build !jsoniter

package log

import "encoding/json"

type (
Marshaler = json.Marshaler
RawMessage = json.RawMessage
)

func Marshal(v any) ([]byte, error) {
return json.Marshal(v)
}

func MarshalIndent(v any, prefix, indent string) ([]byte, error) {
return json.MarshalIndent(v, prefix, indent)
}
2 changes: 1 addition & 1 deletion log.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
)

func init() {
ReleaseMode, _ = strconv.ParseBool(os.Getenv("LOG_RELEASE"))
ReleaseMode, _ = strconv.ParseBool(os.Getenv("LOG_PURE"))

opts := &slog.HandlerOptions{
Level: &logLoggerLevel,
Expand Down

0 comments on commit 6b156c4

Please sign in to comment.