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