Skip to content

Commit

Permalink
Improve code quality all around
Browse files Browse the repository at this point in the history
  • Loading branch information
nhooyr committed Dec 6, 2019
1 parent a6ca229 commit cb158be
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
7 changes: 1 addition & 6 deletions export_test.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package slog

import (
"io"
)

var Exits = 0
var Errors = 0

func init() {
exit = func(code int) {
Exits++
}
ferrorf = func(io.Writer, string, ...interface{}) (int, error) {
errorf = func(string, ...interface{}) {
Errors++
return 0, nil
}
}
5 changes: 1 addition & 4 deletions internal/humanfmt/humanfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ func Entry(w io.Writer, ent slog.SinkEntry) string {
fields, _ := json.MarshalIndent(ent.Fields, "", "")
fields = bytes.ReplaceAll(fields, []byte(",\n"), []byte(", "))
fields = bytes.ReplaceAll(fields, []byte("\n"), []byte(""))
fields, err := highlightJSON(w, fields)
if err != nil {
println("humanfmt: failed to colorize fields JSON: " + err.Error())
}
fields = formatJSON(w, fields)
ents += "\t" + string(fields)
}

Expand Down
19 changes: 14 additions & 5 deletions internal/humanfmt/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,29 @@ var style = chroma.MustNewStyle("slog", chroma.StyleEntries{

var jsonLexer = chroma.Coalesce(jlexers.JSON)

func highlightJSON(w io.Writer, buf []byte) ([]byte, error) {
func formatJSON(w io.Writer, buf []byte) []byte {
if !shouldColor(w) {
return buf, nil
return buf
}

highlighted, err := colorizeJSON(buf)
if err != nil {
println("humanfmt: failed to colorize fields JSON: " + err.Error())
return buf
}
return highlighted
}

func colorizeJSON(buf []byte) ([]byte, error) {
it, err := jsonLexer.Tokenise(nil, string(buf))
if err != nil {
return buf, err
return nil, err
}

b := bytes.NewBuffer(buf[:0])
b := &bytes.Buffer{}
err = formatters.TTY8.Format(b, style, it)
if err != nil {
return buf, err
return nil, err
}
return b.Bytes(), nil
}
8 changes: 5 additions & 3 deletions slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ func (l Logger) log(ctx context.Context, level Level, msg string, fields Map) {
}
err := s.sink.LogEntry(ctx, s.entry(ctx, ent))
if err != nil {
ferrorf(os.Stderr, "slog: sink with name %v and type %T failed to log entry: %+v", s.name, s.sink, err)
errorf("slog: sink with name %v and type %T failed to log entry: %+v", s.name, s.sink, err)
continue
}
}
Expand All @@ -282,15 +282,17 @@ func (l Logger) log(ctx context.Context, level Level, msg string, fields Map) {
}

var exit = os.Exit
var ferrorf = fmt.Fprintf
var errorf = func(f string, v ...interface{}) {
println(fmt.Sprintf(f, v...))
}

// Sync calls Sync on the sinks underlying the logger.
// Used it to ensure all logs are flushed during exit.
func (l Logger) Sync() {
for _, s := range l.sinks {
err := s.sink.Sync()
if err != nil {
ferrorf(os.Stderr, "slog: sink with name %v and type %T failed to sync: %+v\n", s.name, s.sink, err)
errorf("slog: sink with name %v and type %T failed to sync: %+v\n", s.name, s.sink, err)
continue
}
}
Expand Down

0 comments on commit cb158be

Please sign in to comment.