forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/logx-support-rotation-by-size-limit
- Loading branch information
Showing
75 changed files
with
1,594 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# To get started with Dependabot version updates, you'll need to specify which | ||
# package ecosystems to update and where the package manifests are located. | ||
# Please see the documentation for all configuration options: | ||
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates | ||
|
||
version: 2 | ||
updates: | ||
- package-ecosystem: "gomod" # See documentation for possible values | ||
directory: "/" # Location of package manifests | ||
schedule: | ||
interval: "daily" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package errorx | ||
|
||
import "fmt" | ||
|
||
// Wrap returns an error that wraps err with given message. | ||
func Wrap(err error, message string) error { | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("%s: %w", message, err) | ||
} | ||
|
||
// Wrapf returns an error that wraps err with given format and args. | ||
func Wrapf(err error, format string, args ...interface{}) error { | ||
if err == nil { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("%s: %w", fmt.Sprintf(format, args...), err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package errorx | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWrap(t *testing.T) { | ||
assert.Nil(t, Wrap(nil, "test")) | ||
assert.Equal(t, "foo: bar", Wrap(errors.New("bar"), "foo").Error()) | ||
|
||
err := errors.New("foo") | ||
assert.True(t, errors.Is(Wrap(err, "bar"), err)) | ||
} | ||
|
||
func TestWrapf(t *testing.T) { | ||
assert.Nil(t, Wrapf(nil, "%s", "test")) | ||
assert.Equal(t, "foo bar: quz", Wrapf(errors.New("quz"), "foo %s", "bar").Error()) | ||
|
||
err := errors.New("foo") | ||
assert.True(t, errors.Is(Wrapf(err, "foo %s", "bar"), err)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package logx | ||
|
||
import "context" | ||
|
||
var fieldsContextKey contextKey | ||
|
||
type contextKey struct{} | ||
|
||
// WithFields returns a new context with the given fields. | ||
func WithFields(ctx context.Context, fields ...LogField) context.Context { | ||
if val := ctx.Value(fieldsContextKey); val != nil { | ||
if arr, ok := val.([]LogField); ok { | ||
return context.WithValue(ctx, fieldsContextKey, append(arr, fields...)) | ||
} | ||
} | ||
|
||
return context.WithValue(ctx, fieldsContextKey, fields) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package logx | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWithFields(t *testing.T) { | ||
ctx := WithFields(context.Background(), Field("a", 1), Field("b", 2)) | ||
vals := ctx.Value(fieldsContextKey) | ||
assert.NotNil(t, vals) | ||
fields, ok := vals.([]LogField) | ||
assert.True(t, ok) | ||
assert.EqualValues(t, []LogField{Field("a", 1), Field("b", 2)}, fields) | ||
} | ||
|
||
func TestWithFieldsAppend(t *testing.T) { | ||
var dummyKey struct{} | ||
ctx := context.WithValue(context.Background(), dummyKey, "dummy") | ||
ctx = WithFields(ctx, Field("a", 1), Field("b", 2)) | ||
ctx = WithFields(ctx, Field("c", 3), Field("d", 4)) | ||
vals := ctx.Value(fieldsContextKey) | ||
assert.NotNil(t, vals) | ||
fields, ok := vals.([]LogField) | ||
assert.True(t, ok) | ||
assert.Equal(t, "dummy", ctx.Value(dummyKey)) | ||
assert.EqualValues(t, []LogField{ | ||
Field("a", 1), | ||
Field("b", 2), | ||
Field("c", 3), | ||
Field("d", 4), | ||
}, fields) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.