move kv to label
This commit is contained in:
@@ -4,11 +4,12 @@ import (
|
||||
"context"
|
||||
|
||||
"gitoa.ru/go-4devs/console/output/formatter"
|
||||
"gitoa.ru/go-4devs/console/output/label"
|
||||
"gitoa.ru/go-4devs/console/output/verbosity"
|
||||
)
|
||||
|
||||
func Format(out Output, format *formatter.Formatter) Output {
|
||||
return func(ctx context.Context, v verbosity.Verbosity, msg string, kv ...KeyValue) (int, error) {
|
||||
return func(ctx context.Context, v verbosity.Verbosity, msg string, kv ...label.KeyValue) (int, error) {
|
||||
return out(ctx, v, format.Format(ctx, msg), kv...)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package output
|
||||
package label
|
||||
|
||||
type Key string
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package output
|
||||
package label
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -1,4 +1,4 @@
|
||||
package output
|
||||
package label
|
||||
|
||||
import "fmt"
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"gitoa.ru/go-4devs/console/output/label"
|
||||
"gitoa.ru/go-4devs/console/output/verbosity"
|
||||
)
|
||||
|
||||
@@ -15,13 +16,13 @@ func writeError(_ int, err error) {
|
||||
}
|
||||
}
|
||||
|
||||
type Output func(ctx context.Context, verb verbosity.Verbosity, msg string, args ...KeyValue) (int, error)
|
||||
type Output func(ctx context.Context, verb verbosity.Verbosity, msg string, args ...label.KeyValue) (int, error)
|
||||
|
||||
func (o Output) Print(ctx context.Context, args ...interface{}) {
|
||||
writeError(o(ctx, verbosity.Norm, fmt.Sprint(args...)))
|
||||
}
|
||||
|
||||
func (o Output) PrintKV(ctx context.Context, msg string, kv ...KeyValue) {
|
||||
func (o Output) PrintKV(ctx context.Context, msg string, kv ...label.KeyValue) {
|
||||
writeError(o(ctx, verbosity.Norm, msg, kv...))
|
||||
}
|
||||
|
||||
@@ -37,7 +38,7 @@ func (o Output) Info(ctx context.Context, args ...interface{}) {
|
||||
writeError(o(ctx, verbosity.Info, fmt.Sprint(args...)))
|
||||
}
|
||||
|
||||
func (o Output) InfoKV(ctx context.Context, msg string, kv ...KeyValue) {
|
||||
func (o Output) InfoKV(ctx context.Context, msg string, kv ...label.KeyValue) {
|
||||
writeError(o(ctx, verbosity.Info, msg, kv...))
|
||||
}
|
||||
|
||||
@@ -45,7 +46,7 @@ func (o Output) Debug(ctx context.Context, args ...interface{}) {
|
||||
writeError(o(ctx, verbosity.Debug, fmt.Sprint(args...)))
|
||||
}
|
||||
|
||||
func (o Output) DebugKV(ctx context.Context, msg string, kv ...KeyValue) {
|
||||
func (o Output) DebugKV(ctx context.Context, msg string, kv ...label.KeyValue) {
|
||||
writeError(o(ctx, verbosity.Debug, msg, kv...))
|
||||
}
|
||||
|
||||
@@ -53,7 +54,7 @@ func (o Output) Trace(ctx context.Context, args ...interface{}) {
|
||||
writeError(o(ctx, verbosity.Trace, fmt.Sprint(args...)))
|
||||
}
|
||||
|
||||
func (o Output) TraceKV(ctx context.Context, msg string, kv ...KeyValue) {
|
||||
func (o Output) TraceKV(ctx context.Context, msg string, kv ...label.KeyValue) {
|
||||
writeError(o(ctx, verbosity.Trace, msg, kv...))
|
||||
}
|
||||
|
||||
|
||||
@@ -3,11 +3,12 @@ package output
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitoa.ru/go-4devs/console/output/label"
|
||||
"gitoa.ru/go-4devs/console/output/verbosity"
|
||||
)
|
||||
|
||||
func Quiet() Output {
|
||||
return func(context.Context, verbosity.Verbosity, string, ...KeyValue) (int, error) {
|
||||
return func(context.Context, verbosity.Verbosity, string, ...label.KeyValue) (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,12 @@ package output
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitoa.ru/go-4devs/console/output/label"
|
||||
"gitoa.ru/go-4devs/console/output/verbosity"
|
||||
)
|
||||
|
||||
func Verbosity(out Output, verb verbosity.Verbosity) Output {
|
||||
return func(ctx context.Context, v verbosity.Verbosity, msg string, kv ...KeyValue) (int, error) {
|
||||
return func(ctx context.Context, v verbosity.Verbosity, msg string, kv ...label.KeyValue) (int, error) {
|
||||
if verb >= v {
|
||||
return out(ctx, v, msg, kv...)
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gitoa.ru/go-4devs/console/output/label"
|
||||
"gitoa.ru/go-4devs/console/output/verbosity"
|
||||
)
|
||||
|
||||
@@ -25,21 +26,21 @@ func Buffer(buf *bytes.Buffer) Output {
|
||||
return New(buf, FormatString)
|
||||
}
|
||||
|
||||
func FormatString(_ verbosity.Verbosity, msg string, kv ...KeyValue) string {
|
||||
func FormatString(_ verbosity.Verbosity, msg string, kv ...label.KeyValue) string {
|
||||
if len(kv) > 0 {
|
||||
nline := ""
|
||||
if msg[len(msg)-1:] == newline {
|
||||
nline = newline
|
||||
}
|
||||
|
||||
return "msg=\"" + strings.TrimSpace(msg) + "\", " + KeyValues(kv).String() + nline
|
||||
return "msg=\"" + strings.TrimSpace(msg) + "\", " + label.KeyValues(kv).String() + nline
|
||||
}
|
||||
|
||||
return msg
|
||||
}
|
||||
|
||||
func New(w io.Writer, format func(verb verbosity.Verbosity, msg string, kv ...KeyValue) string) Output {
|
||||
return func(ctx context.Context, verb verbosity.Verbosity, msg string, kv ...KeyValue) (int, error) {
|
||||
func New(w io.Writer, format func(verb verbosity.Verbosity, msg string, kv ...label.KeyValue) string) Output {
|
||||
return func(ctx context.Context, verb verbosity.Verbosity, msg string, kv ...label.KeyValue) (int, error) {
|
||||
return fmt.Fprint(w, format(verb, msg, kv...))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"gitoa.ru/go-4devs/console/output"
|
||||
"gitoa.ru/go-4devs/console/output/label"
|
||||
)
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
@@ -15,23 +16,23 @@ func TestNew(t *testing.T) {
|
||||
|
||||
cases := map[string]struct {
|
||||
ex string
|
||||
kv []output.KeyValue
|
||||
kv []label.KeyValue
|
||||
}{
|
||||
"message": {
|
||||
ex: "message",
|
||||
},
|
||||
"msg with kv": {
|
||||
ex: "msg=\"msg with kv\", string key=\"string value\", bool key=\"false\", int key=\"42\"",
|
||||
kv: []output.KeyValue{
|
||||
output.String("string key", "string value"),
|
||||
output.Bool("bool key", false),
|
||||
output.Int("int key", 42),
|
||||
kv: []label.KeyValue{
|
||||
label.String("string key", "string value"),
|
||||
label.Bool("bool key", false),
|
||||
label.Int("int key", 42),
|
||||
},
|
||||
},
|
||||
"msg with newline \n": {
|
||||
ex: "msg=\"msg with newline\", int=\"42\"\n",
|
||||
kv: []output.KeyValue{
|
||||
output.Int("int", 42),
|
||||
kv: []label.KeyValue{
|
||||
label.Int("int", 42),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user