Skip to content

Commit

Permalink
feat: export format.Write function. closes #30
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentfiestada committed May 23, 2020
1 parent ea0289e commit 82bec5a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
16 changes: 8 additions & 8 deletions format/flat.go
Expand Up @@ -14,26 +14,26 @@ func Flat(msg *msg.Message) {
colorize = fmt.Sprintf
}

write(stream, colorize("%6s", level))
Write(stream, colorize("%6s", level))
separate(stream)
write(stream, msg.Time)
Write(stream, msg.Time)
separate(stream)
write(stream, colorize(msg.Name))
Write(stream, colorize(msg.Name))
if len(msg.Data) > 0 {
separate(stream)
for i := 0; i < len(msg.Data)-1; i += 2 {
if i > 0 {
write(stream, ", ")
Write(stream, ", ")
}
write(stream, fmt.Sprintf("%s=%#v", msg.Data[i], msg.Data[i+1]))
Write(stream, fmt.Sprintf("%s=%#v", msg.Data[i], msg.Data[i+1]))
}
}
separate(stream)
write(stream, msg.Text)
write(stream, "\n")
Write(stream, msg.Text)
Write(stream, "\n")
}

// separate prints out a separator between parts of the message
func separate(stream *os.File) {
write(stream, " :: ")
Write(stream, " :: ")
}
6 changes: 3 additions & 3 deletions format/json.go
Expand Up @@ -10,11 +10,11 @@ import (
func JSON(msg *msg.Message) {
stream, level, _ := msg.Props()

write(stream, fmt.Sprintf(`{"level":"%s","time":"%s","caller":"%s",`, level, msg.Time, msg.Name))
Write(stream, fmt.Sprintf(`{"level":"%s","time":"%s","caller":"%s",`, level, msg.Time, msg.Name))
if len(msg.Data) > 0 {
for i := 0; i < len(msg.Data)-1; i += 2 {
write(stream, fmt.Sprintf(`"%s":%#v,`, msg.Data[i], msg.Data[i+1]))
Write(stream, fmt.Sprintf(`"%s":%#v,`, msg.Data[i], msg.Data[i+1]))
}
}
write(stream, fmt.Sprintf("\"message\":\"%s\"}\n", msg.Text))
Write(stream, fmt.Sprintf("\"message\":\"%s\"}\n", msg.Text))
}
10 changes: 3 additions & 7 deletions format/write.go
@@ -1,14 +1,10 @@
package format

import (
"log"
"os"
)

// write a string to a file
func write(stream *os.File, str string) {
_, err := stream.WriteString(str)
if err != nil {
log.Println("captainslog error!", err)
}
// Write a string to a file
func Write(stream *os.File, str string) {
_, _ = stream.WriteString(str)
}

0 comments on commit 82bec5a

Please sign in to comment.