6 Commits

Author SHA1 Message Date
859a8d88f7 Merge pull request 'update ling and go version' (#17) from format into master
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Reviewed-on: #17
2024-04-04 12:19:45 +03:00
4baf4b36e7 set golang version
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2024-04-04 12:17:02 +03:00
andrey
2fd0c1f9ec update ling and go version
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone/pr Build is failing
2024-04-04 12:12:09 +03:00
c7090b5067 Merge pull request 'use dependency format log' (#16) from format into master
Some checks failed
continuous-integration/drone/push Build is failing
Reviewed-on: #16
2024-04-04 12:08:06 +03:00
andrey
16d3e04fd9 use dependency format log 2024-04-04 12:04:45 +03:00
57a908f894 add bracket format (#15)
All checks were successful
continuous-integration/drone/tag Build is passing
continuous-integration/drone/push Build is passing
Reviewed-on: #15
Co-authored-by: andrey <andrey@4devs.io>
Co-committed-by: andrey <andrey@4devs.io>
2024-01-03 21:35:21 +03:00
8 changed files with 24 additions and 21 deletions

View File

@@ -5,7 +5,7 @@ name: logger
steps: steps:
- name: test - name: test
image: golang:1.21.5 image: golang:1.22.2
volumes: volumes:
- name: deps - name: deps
path: /go/src/mod path: /go/src/mod
@@ -13,7 +13,7 @@ steps:
- go test - go test
- name: golangci-lint - name: golangci-lint
image: golangci/golangci-lint:v1.55 image: golangci/golangci-lint:v1.57
commands: commands:
- golangci-lint run - golangci-lint run

View File

@@ -166,7 +166,9 @@ func BenchmarkDisabledAccumulatedContext(b *testing.B) {
b.Run("4devs/log.Context", func(b *testing.B) { b.Run("4devs/log.Context", func(b *testing.B) {
b.ResetTimer() b.ResetTimer()
logger := NewLogger().With(log.GoVersion("goversion")) logger := NewLogger().With(log.GoVersion("goversion"))
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
for pb.Next() { for pb.Next() {
logger.InfoKV(ctx, getMessage(0), fakeFields()...) logger.InfoKV(ctx, getMessage(0), fakeFields()...)

View File

@@ -21,7 +21,7 @@ func main() {
logger.AlertKV(ctx, "alert message new logger", field.String("string", "value")) logger.AlertKV(ctx, "alert message new logger", field.String("string", "value"))
service(ctx, logger) service(ctx, logger)
strLogger := log.New(log.WithFormat(log.FormatWithBracket())).With(log.WithSource(10, log.TrimPath), log.WithTime(log.KeyTime, time.RFC3339)) strLogger := log.New(log.WithFormat(log.FormatWithBracket(field.NewEncoderText()))).With(log.WithSource(10, log.TrimPath), log.WithTime(log.KeyTime, time.RFC3339))
strLogger.AlertKV(ctx, "alert message new txt logger", field.String("string", "value")) strLogger.AlertKV(ctx, "alert message new txt logger", field.String("string", "value"))
service(ctx, strLogger) service(ctx, strLogger)
} }

View File

@@ -174,6 +174,7 @@ func needsQuoting(in string) bool {
if char != '\\' && (char == ' ' || char == '=' || !safeSet[char]) { if char != '\\' && (char == ' ' || char == '=' || !safeSet[char]) {
return true return true
} }
i++ i++
continue continue

View File

@@ -213,8 +213,8 @@ func ExampleNew_jsonFormat() {
logger.Err(ctx, "same error message") logger.Err(ctx, "same error message")
logger.WarnKVs(ctx, "same warn message", "obj", Obj{Name: "obj name"}) logger.WarnKVs(ctx, "same warn message", "obj", Obj{Name: "obj name"})
// Output: // Output:
// {"msg":"same error message","level":"error","go-version":"go1.21.5"} // {"msg":"same error message","level":"error","go-version":"go1.22.2"}
// {"msg":"same warn message","obj":{"Name":"obj name","IsEnable":false},"level":"warning","go-version":"go1.21.5"} // {"msg":"same warn message","obj":{"Name":"obj name","IsEnable":false},"level":"warning","go-version":"go1.22.2"}
} }
func ExampleNew_textEncoding() { func ExampleNew_textEncoding() {
@@ -227,8 +227,8 @@ func ExampleNew_textEncoding() {
logger.InfoKVs(ctx, "same info message", "api-version", 0.1, "obj", Obj{Name: "text value", IsEnable: true}) logger.InfoKVs(ctx, "same info message", "api-version", 0.1, "obj", Obj{Name: "text value", IsEnable: true})
// Output: // Output:
// msg="same error message" level=error go-version=go1.21.5 // msg="same error message" level=error go-version=go1.22.2
// msg="same info message" api-version=0.1 obj={Name:text value IsEnable:true} level=info go-version=go1.21.5 // msg="same info message" api-version=0.1 obj={Name:text value IsEnable:true} level=info go-version=go1.22.2
} }
type ctxKey string type ctxKey string
@@ -252,7 +252,7 @@ func ExampleWith() {
log.GoVersion("go"), log.GoVersion("go"),
) )
logger.Info(vctx, "same message") logger.Info(vctx, "same message")
// Output: msg="same message" level=info requestID=6a5fa048-7181-11ea-bc55-0242ac130003 api=0.1.0 go=go1.21.5 // Output: msg="same message" level=info requestID=6a5fa048-7181-11ea-bc55-0242ac130003 api=0.1.0 go=go1.22.2
} }
func ExampleLogger_Print() { func ExampleLogger_Print() {
@@ -263,7 +263,7 @@ func ExampleLogger_Print() {
log.GoVersion("go"), log.GoVersion("go"),
) )
logger.Print("same message") logger.Print("same message")
// Output: msg="same message" level=info client=http api=0.1.0 go=go1.21.5 // Output: msg="same message" level=info client=http api=0.1.0 go=go1.22.2
} }
func ExamplePrint() { func ExamplePrint() {

View File

@@ -38,6 +38,7 @@ func With(logger Logger, mw ...Middleware) Logger {
if curI == lastI { if curI == lastI {
return logger(currentCtx, currentEntry) return logger(currentCtx, currentEntry)
} }
curI++ curI++
n, err := mw[curI](currentCtx, currentEntry, chainHandler) n, err := mw[curI](currentCtx, currentEntry, chainHandler)
curI-- curI--

View File

@@ -20,7 +20,7 @@ func exampleWithTime(key, format string) log.Middleware {
func ExampleFormatWithBracket() { func ExampleFormatWithBracket() {
ctx := context.Background() ctx := context.Background()
logger := log.New(log.WithFormat(log.FormatWithBracket()), log.WithStdout()).With( logger := log.New(log.WithFormat(log.FormatWithBracket(field.NewEncoderText())), log.WithStdout()).With(
log.WithSource(10, filepath.Base), log.WithSource(10, filepath.Base),
// log.WithTime(log.KeyTime, time.RFC3339), // log.WithTime(log.KeyTime, time.RFC3339),
exampleWithTime(log.KeyTime, time.RFC3339), exampleWithTime(log.KeyTime, time.RFC3339),

View File

@@ -43,12 +43,12 @@ func WithStdout() func(*option) {
// WithStringFormat sets format as simple string. // WithStringFormat sets format as simple string.
func WithStringFormat() func(*option) { func WithStringFormat() func(*option) {
return WithFormat(formatString()) return WithFormat(FormatString(field.NewEncoderText()))
} }
// WithJSONFormat sets json output format. // WithJSONFormat sets json output format.
func WithJSONFormat() func(*option) { func WithJSONFormat() func(*option) {
return WithFormat(formatJSON()) return WithFormat(FormatJSON(field.NewEncoderJSON()))
} }
// WithFormat sets custom output format. // WithFormat sets custom output format.
@@ -66,7 +66,7 @@ type option struct {
// New creates standart logger. // New creates standart logger.
func New(opts ...func(*option)) Logger { func New(opts ...func(*option)) Logger {
log := option{ log := option{
format: formatString(), format: FormatString(field.NewEncoderText()),
out: os.Stderr, out: os.Stderr,
} }
@@ -79,9 +79,12 @@ func New(opts ...func(*option)) Logger {
} }
} }
func FormatWithBracket() func(io.Writer, *entry.Entry) (int, error) { type Encoder interface {
enc := field.NewEncoderText() AppendValue(dst []byte, val field.Value) []byte
AppendField(dst []byte, val field.Field) []byte
}
func FormatWithBracket(enc Encoder) func(io.Writer, *entry.Entry) (int, error) {
appendValue := func(buf *buffer.Buffer, data field.Fields, key, prefix, suffix string) *buffer.Buffer { appendValue := func(buf *buffer.Buffer, data field.Fields, key, prefix, suffix string) *buffer.Buffer {
data.Fields( data.Fields(
func(f field.Field) bool { func(f field.Field) bool {
@@ -133,9 +136,7 @@ func FormatWithBracket() func(io.Writer, *entry.Entry) (int, error) {
} }
} }
func formatString() func(io.Writer, *entry.Entry) (int, error) { func FormatString(enc Encoder) func(io.Writer, *entry.Entry) (int, error) {
enc := field.NewEncoderText()
return func(w io.Writer, entry *entry.Entry) (int, error) { return func(w io.Writer, entry *entry.Entry) (int, error) {
buf := buffer.New() buf := buffer.New()
defer func() { defer func() {
@@ -159,9 +160,7 @@ func formatString() func(io.Writer, *entry.Entry) (int, error) {
} }
} }
func formatJSON() func(w io.Writer, entry *entry.Entry) (int, error) { func FormatJSON(enc Encoder) func(w io.Writer, entry *entry.Entry) (int, error) {
enc := field.NewEncoderJSON()
return func(w io.Writer, entry *entry.Entry) (int, error) { return func(w io.Writer, entry *entry.Entry) (int, error) {
buf := buffer.New() buf := buffer.New()
defer func() { defer func() {