Compare commits
3 Commits
6a7223ea4a
...
v4.1.11
Author | SHA1 | Date | |
---|---|---|---|
03ee33040c | |||
0144f175f0 | |||
b3539a32ab |
1
go.mod
1
go.mod
@@ -6,7 +6,6 @@ require (
|
||||
dario.cat/mergo v1.0.1
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.2
|
||||
github.com/KimMachineGun/automemlimit v0.7.0
|
||||
github.com/ash3in/uuidv8 v1.2.0
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/matoous/go-nanoid v1.5.1
|
||||
github.com/patrickmn/go-cache v2.1.0+incompatible
|
||||
|
2
go.sum
2
go.sum
@@ -4,8 +4,6 @@ github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7Oputl
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
|
||||
github.com/KimMachineGun/automemlimit v0.7.0 h1:7G06p/dMSf7G8E6oq+f2uOPuVncFyIlDI/pBWK49u88=
|
||||
github.com/KimMachineGun/automemlimit v0.7.0/go.mod h1:QZxpHaGOQoYvFhv/r4u3U0JTC2ZcOwbSr11UZF46UBM=
|
||||
github.com/ash3in/uuidv8 v1.2.0 h1:2oogGdtCPwaVtyvPPGin4TfZLtOGE5F+W++E880G6SI=
|
||||
github.com/ash3in/uuidv8 v1.2.0/go.mod h1:BnU0wJBxnzdEKmVg4xckBkD+VZuecTFTUP3M0dWgyY4=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||
|
@@ -4,18 +4,20 @@ package logger
|
||||
type Level int8
|
||||
|
||||
const (
|
||||
// TraceLevel level usually used to find bugs, very verbose
|
||||
// TraceLevel usually used to find bugs, very verbose
|
||||
TraceLevel Level = iota - 2
|
||||
// DebugLevel level used only when enabled debugging
|
||||
// DebugLevel used only when enabled debugging
|
||||
DebugLevel
|
||||
// InfoLevel level used for general info about what's going on inside the application
|
||||
// InfoLevel used for general info about what's going on inside the application
|
||||
InfoLevel
|
||||
// WarnLevel level used for non-critical entries
|
||||
// WarnLevel used for non-critical entries
|
||||
WarnLevel
|
||||
// ErrorLevel level used for errors that should definitely be noted
|
||||
// ErrorLevel used for errors that should definitely be noted
|
||||
ErrorLevel
|
||||
// FatalLevel level used for critical errors and then calls `os.Exit(1)`
|
||||
// FatalLevel used for critical errors and then calls `os.Exit(1)`
|
||||
FatalLevel
|
||||
// NoneLevel used to disable logging
|
||||
NoneLevel
|
||||
)
|
||||
|
||||
// String returns logger level string representation
|
||||
@@ -33,6 +35,8 @@ func (l Level) String() string {
|
||||
return "error"
|
||||
case FatalLevel:
|
||||
return "fatal"
|
||||
case NoneLevel:
|
||||
return "none"
|
||||
}
|
||||
return "info"
|
||||
}
|
||||
@@ -58,6 +62,8 @@ func ParseLevel(lvl string) Level {
|
||||
return ErrorLevel
|
||||
case FatalLevel.String():
|
||||
return FatalLevel
|
||||
case NoneLevel.String():
|
||||
return NoneLevel
|
||||
}
|
||||
return InfoLevel
|
||||
}
|
||||
|
@@ -34,6 +34,7 @@ var (
|
||||
warnValue = slog.StringValue("warn")
|
||||
errorValue = slog.StringValue("error")
|
||||
fatalValue = slog.StringValue("fatal")
|
||||
noneValue = slog.StringValue("none")
|
||||
)
|
||||
|
||||
type wrapper struct {
|
||||
@@ -85,6 +86,8 @@ func (s *slogLogger) renameAttr(_ []string, a slog.Attr) slog.Attr {
|
||||
a.Value = errorValue
|
||||
case lvl >= logger.FatalLevel:
|
||||
a.Value = fatalValue
|
||||
case lvl >= logger.NoneLevel:
|
||||
a.Value = noneValue
|
||||
default:
|
||||
a.Value = infoValue
|
||||
}
|
||||
@@ -316,6 +319,8 @@ func loggerToSlogLevel(level logger.Level) slog.Level {
|
||||
return slog.LevelDebug - 1
|
||||
case logger.FatalLevel:
|
||||
return slog.LevelError + 1
|
||||
case logger.NoneLevel:
|
||||
return slog.LevelError + 2
|
||||
default:
|
||||
return slog.LevelInfo
|
||||
}
|
||||
@@ -333,6 +338,8 @@ func slogToLoggerLevel(level slog.Level) logger.Level {
|
||||
return logger.TraceLevel
|
||||
case slog.LevelError + 1:
|
||||
return logger.FatalLevel
|
||||
case slog.LevelError + 2:
|
||||
return logger.NoneLevel
|
||||
default:
|
||||
return logger.InfoLevel
|
||||
}
|
||||
|
@@ -36,6 +36,24 @@ func TestStacktrace(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoneLevel(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
buf := bytes.NewBuffer(nil)
|
||||
l := NewLogger(logger.WithLevel(logger.NoneLevel), logger.WithOutput(buf),
|
||||
WithHandlerFunc(slog.NewTextHandler),
|
||||
logger.WithAddStacktrace(true),
|
||||
)
|
||||
if err := l.Init(logger.WithFields("key1", "val1")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
l.Error(ctx, "msg1", errors.New("err"))
|
||||
|
||||
if buf.Len() != 0 {
|
||||
t.Fatalf("logger none level not works, buf contains: %s", buf.Bytes())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDelayedBuffer(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
buf := bytes.NewBuffer(nil)
|
||||
|
@@ -69,7 +69,8 @@ type Service struct {
|
||||
type Node struct {
|
||||
Metadata metadata.Metadata `json:"metadata,omitempty"`
|
||||
ID string `json:"id,omitempty"`
|
||||
Address string `json:"address,omitempty"`
|
||||
// Address also prefixed with scheme like grpc://xx.xx.xx.xx:1234
|
||||
Address string `json:"address,omitempty"`
|
||||
}
|
||||
|
||||
// Option func signature
|
||||
|
@@ -2,12 +2,8 @@ package id
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
uuidv8 "github.com/ash3in/uuidv8"
|
||||
"github.com/google/uuid"
|
||||
nanoid "github.com/matoous/go-nanoid"
|
||||
)
|
||||
@@ -25,6 +21,7 @@ type Type int
|
||||
const (
|
||||
TypeUnspecified Type = iota
|
||||
TypeNanoid
|
||||
TypeUUIDv7
|
||||
TypeUUIDv8
|
||||
)
|
||||
|
||||
@@ -58,14 +55,14 @@ func (g *Generator) New() (string, error) {
|
||||
}
|
||||
|
||||
return nanoid.Generate(g.opts.NanoidAlphabet, g.opts.NanoidSize)
|
||||
case TypeUUIDv8:
|
||||
timestamp := uint64(time.Now().UnixNano())
|
||||
clockSeq := make([]byte, 2)
|
||||
if _, err := rand.Read(clockSeq); err != nil {
|
||||
return "", fmt.Errorf("failed to generate random clock sequence: %w", err)
|
||||
case TypeUUIDv7:
|
||||
uid, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
clockSeqValue := binary.BigEndian.Uint16(clockSeq) & 0x0FFF // Mask to 12 bits
|
||||
return uuidv8.NewWithParams(timestamp, clockSeqValue, g.opts.UUIDNode[:], uuidv8.TimestampBits48)
|
||||
return uid.String(), nil
|
||||
case TypeUUIDv8:
|
||||
return "", errors.New("unsupported uuid version v8")
|
||||
}
|
||||
return "", errors.New("invalid option, Type unspecified")
|
||||
}
|
||||
@@ -82,16 +79,15 @@ func New(opts ...Option) (string, error) {
|
||||
if options.NanoidSize <= 0 {
|
||||
return "", errors.New("invalid option, NanoidSize must be positive integer")
|
||||
}
|
||||
|
||||
return nanoid.Generate(options.NanoidAlphabet, options.NanoidSize)
|
||||
case TypeUUIDv8:
|
||||
timestamp := uint64(time.Now().UnixNano())
|
||||
clockSeq := make([]byte, 2)
|
||||
if _, err := rand.Read(clockSeq); err != nil {
|
||||
return "", fmt.Errorf("failed to generate random clock sequence: %w", err)
|
||||
case TypeUUIDv7:
|
||||
uid, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
clockSeqValue := binary.BigEndian.Uint16(clockSeq) & 0x0FFF // Mask to 12 bits
|
||||
return uuidv8.NewWithParams(timestamp, clockSeqValue, options.UUIDNode[:], uuidv8.TimestampBits48)
|
||||
return uid.String(), nil
|
||||
case TypeUUIDv8:
|
||||
return "", errors.New("unsupported uuid version v8")
|
||||
}
|
||||
|
||||
return "", errors.New("invalid option, Type unspecified")
|
||||
@@ -145,7 +141,7 @@ func WithUUIDNode(node [6]byte) Option {
|
||||
// NewOptions returns new Options struct filled by opts
|
||||
func NewOptions(opts ...Option) Options {
|
||||
options := Options{
|
||||
Type: TypeUUIDv8,
|
||||
Type: TypeUUIDv7,
|
||||
NanoidAlphabet: DefaultNanoidAlphabet,
|
||||
NanoidSize: DefaultNanoidSize,
|
||||
UUIDNode: generatedNode,
|
||||
|
Reference in New Issue
Block a user