add bracket format
This commit is contained in:
@@ -73,3 +73,4 @@ issues:
|
|||||||
- path: example/*
|
- path: example/*
|
||||||
linters:
|
linters:
|
||||||
- gomnd
|
- gomnd
|
||||||
|
- lll
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gitoa.ru/go-4devs/log"
|
"gitoa.ru/go-4devs/log"
|
||||||
"gitoa.ru/go-4devs/log/field"
|
"gitoa.ru/go-4devs/log/field"
|
||||||
@@ -16,9 +17,13 @@ func main() {
|
|||||||
log.Err(ctx, "error message", 42)
|
log.Err(ctx, "error message", 42)
|
||||||
service(ctx, log.Log())
|
service(ctx, log.Log())
|
||||||
|
|
||||||
logger := log.New(log.WithJSONFormat()).With(log.WithSource(10, log.TrimPath))
|
logger := log.New(log.WithJSONFormat()).With(log.WithSource(10, log.TrimPath), log.WithTime(log.KeyTime, time.RFC3339))
|
||||||
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.AlertKV(ctx, "alert message new txt logger", field.String("string", "value"))
|
||||||
|
service(ctx, strLogger)
|
||||||
}
|
}
|
||||||
|
|
||||||
func service(ctx context.Context, logger log.Logger) {
|
func service(ctx context.Context, logger log.Logger) {
|
||||||
|
|||||||
@@ -501,3 +501,14 @@ type Field struct {
|
|||||||
func (f Field) String() string {
|
func (f Field) String() string {
|
||||||
return fmt.Sprintf("%s=%+v", f.Key, f.Value)
|
return fmt.Sprintf("%s=%+v", f.Key, f.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// String implent stringer.
|
||||||
|
func (f Field) IsKey(keys ...string) bool {
|
||||||
|
for _, key := range keys {
|
||||||
|
if key == f.Key {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package log
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gitoa.ru/go-4devs/log/field"
|
"gitoa.ru/go-4devs/log/field"
|
||||||
"gitoa.ru/go-4devs/log/level"
|
"gitoa.ru/go-4devs/log/level"
|
||||||
@@ -10,6 +11,7 @@ import (
|
|||||||
|
|
||||||
//nolint:gochecknoglobals,gomnd
|
//nolint:gochecknoglobals,gomnd
|
||||||
var global = With(New(),
|
var global = With(New(),
|
||||||
|
WithTime(KeyTime, time.RFC3339),
|
||||||
WithSource(2, TrimPath),
|
WithSource(2, TrimPath),
|
||||||
WithLevel(KeyLevel, level.Debug),
|
WithLevel(KeyLevel, level.Debug),
|
||||||
WithExit(level.Alert),
|
WithExit(level.Alert),
|
||||||
|
|||||||
36
writer_example_test.go
Normal file
36
writer_example_test.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package log_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"math"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitoa.ru/go-4devs/log"
|
||||||
|
"gitoa.ru/go-4devs/log/entry"
|
||||||
|
"gitoa.ru/go-4devs/log/field"
|
||||||
|
"gitoa.ru/go-4devs/log/level"
|
||||||
|
)
|
||||||
|
|
||||||
|
func exampleWithTime(key, format string) log.Middleware {
|
||||||
|
return func(ctx context.Context, e *entry.Entry, handler log.Logger) (int, error) {
|
||||||
|
return handler(ctx, e.Add(field.FormatTime(key, format, time.Unix(math.MaxInt32, 0))))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleFormatWithBracket() {
|
||||||
|
ctx := context.Background()
|
||||||
|
logger := log.New(log.WithFormat(log.FormatWithBracket()), log.WithStdout()).With(
|
||||||
|
log.WithSource(10, filepath.Base),
|
||||||
|
exampleWithTime(log.KeyTime, time.RFC3339), //log.WithTime(log.KeyTime, time.RFC3339),
|
||||||
|
log.WithLevel(log.KeyLevel, level.Info),
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.InfoKV(ctx, "imfo message", field.Int64("num", 42))
|
||||||
|
|
||||||
|
serviceLogger := logger.With(log.WithName("service_name"))
|
||||||
|
serviceLogger.Err(ctx, "error message")
|
||||||
|
// Output:
|
||||||
|
// 2038-01-19T06:14:07+03:00 [info] writer_example_test.go:29 "imfo message" num=42
|
||||||
|
// 2038-01-19T06:14:07+03:00 [error][service_name] writer_example_test.go:32 "error message"
|
||||||
|
}
|
||||||
69
writter.go
69
writter.go
@@ -43,15 +43,18 @@ 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 func(o *option) {
|
return WithFormat(formatString())
|
||||||
o.format = formatText()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithJSONFormat sets json output format.
|
// WithJSONFormat sets json output format.
|
||||||
func WithJSONFormat() func(*option) {
|
func WithJSONFormat() func(*option) {
|
||||||
|
return WithFormat(formatJSON())
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithFormat sets custom output format.
|
||||||
|
func WithFormat(format func(io.Writer, *entry.Entry) (int, error)) func(*option) {
|
||||||
return func(o *option) {
|
return func(o *option) {
|
||||||
o.format = formatJSON()
|
o.format = format
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,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: formatText(),
|
format: formatString(),
|
||||||
out: os.Stderr,
|
out: os.Stderr,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,7 +79,61 @@ func New(opts ...func(*option)) Logger {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatText() func(io.Writer, *entry.Entry) (int, error) {
|
func FormatWithBracket() func(io.Writer, *entry.Entry) (int, error) {
|
||||||
|
enc := field.NewEncoderText()
|
||||||
|
|
||||||
|
appendValue := func(buf *buffer.Buffer, data field.Fields, key, prefix, suffix string) *buffer.Buffer {
|
||||||
|
data.Fields(
|
||||||
|
func(f field.Field) bool {
|
||||||
|
if f.IsKey(key) {
|
||||||
|
_, _ = buf.WriteString(prefix)
|
||||||
|
*buf = enc.AppendValue(*buf, f.Value)
|
||||||
|
_, _ = buf.WriteString(suffix)
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
return buf
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(w io.Writer, data *entry.Entry) (int, error) {
|
||||||
|
buf := buffer.New()
|
||||||
|
defer func() {
|
||||||
|
buf.Free()
|
||||||
|
}()
|
||||||
|
|
||||||
|
fields := data.Fields()
|
||||||
|
buf = appendValue(buf, fields, KeyTime, "", " ")
|
||||||
|
_, _ = buf.WriteString("[")
|
||||||
|
*buf = enc.AppendValue(*buf, field.StringValue(data.Level().String()))
|
||||||
|
_, _ = buf.WriteString("]")
|
||||||
|
buf = appendValue(buf, fields, KeyName, "[", "]")
|
||||||
|
buf = appendValue(buf, fields, KeySource, " ", " ")
|
||||||
|
*buf = enc.AppendValue(*buf, field.StringValue(data.Message()))
|
||||||
|
|
||||||
|
fields.Fields(func(f field.Field) bool {
|
||||||
|
if !f.IsKey(KeyTime, KeySource, KeyName, KeyLevel) {
|
||||||
|
*buf = enc.AppendField(*buf, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
_, _ = buf.WriteString("\n")
|
||||||
|
|
||||||
|
n, err := w.Write(*buf)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("format text:%w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return n, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatString() func(io.Writer, *entry.Entry) (int, error) {
|
||||||
enc := field.NewEncoderText()
|
enc := field.NewEncoderText()
|
||||||
|
|
||||||
return func(w io.Writer, entry *entry.Entry) (int, error) {
|
return func(w io.Writer, entry *entry.Entry) (int, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user