Browse Source

use dependency format log

pull/16/head
andrey 7 months ago
parent
commit
16d3e04fd9
  1. 2
      writer_example_test.go
  2. 21
      writter.go

2
writer_example_test.go

@ -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),

21
writter.go

@ -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() {

Loading…
Cancel
Save