Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writer: Loggo emoji writer #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import (
"os"

"github.com/juju/loggo"
"github.com/juju/loggo/loggoemoji"
)

var logger = loggo.GetLogger("main")
var rootLogger = loggo.GetLogger("")

func main() {
loggo.ResetWriters()
loggo.RegisterWriter("emoji", loggoemoji.NewWriter(os.Stdout))

args := os.Args
if len(args) > 1 {
loggo.ConfigureLoggers(args[1])
Expand Down
14 changes: 14 additions & 0 deletions loggoemoji/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2021 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package loggoemoji

import (
"testing"

gc "gopkg.in/check.v1"
)

func Test(t *testing.T) {
gc.TestingT(t)
}
67 changes: 67 additions & 0 deletions loggoemoji/writer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2021 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package loggoemoji

import (
"fmt"
"io"
"path/filepath"

"github.com/juju/ansiterm"
"github.com/juju/loggo"
)

type levelContext struct {
Emoji string
Style *ansiterm.Context
}

var (
// SeverityEmoji defines the colors for the levels output by the ColorWriter.
SeverityEmoji = map[loggo.Level]levelContext{
loggo.TRACE: {Emoji: "✏️", Style: ansiterm.Foreground(ansiterm.Default)},
loggo.DEBUG: {Emoji: "🐞", Style: ansiterm.Foreground(ansiterm.Green)},
loggo.INFO: {Emoji: "🧐", Style: ansiterm.Foreground(ansiterm.BrightBlue)},
loggo.WARNING: {Emoji: "⚠️ ", Style: ansiterm.Foreground(ansiterm.Yellow)},
loggo.ERROR: {Emoji: "😱", Style: ansiterm.Foreground(ansiterm.BrightRed)},
loggo.CRITICAL: {Emoji: "💥", Style: &ansiterm.Context{
Foreground: ansiterm.White,
Background: ansiterm.Red,
}},
}
// LocationColor defines the colors for the location output by the ColorWriter.
LocationColor = ansiterm.Foreground(ansiterm.BrightBlue)
)

type emojiWriter struct {
writer *ansiterm.Writer
}

// NewWriter will write out colored severity levels if the writer is
// outputting to a terminal.
func NewWriter(writer io.Writer) loggo.Writer {
return &emojiWriter{ansiterm.NewWriter(writer)}
}

// NewColorWriter will write out colored severity levels whether or not the
// writer is outputting to a terminal.
func NewColorWriter(writer io.Writer) loggo.Writer {
w := ansiterm.NewWriter(writer)
w.SetColorCapable(true)
return &emojiWriter{w}
}

// Write implements Writer.
func (w *emojiWriter) Write(entry loggo.Entry) {
ts := entry.Timestamp.Format(loggo.TimeFormat)
// Just get the basename from the filename
filename := filepath.Base(entry.Filename)

fmt.Fprintf(w.writer, "%s ", ts)
SeverityEmoji[entry.Level].Style.Fprintf(w.writer, entry.Level.Short())
fmt.Fprintf(w.writer, " %s", SeverityEmoji[entry.Level].Emoji)
fmt.Fprintf(w.writer, " %s ", entry.Module)
LocationColor.Fprintf(w.writer, "%s:%d ", filename, entry.Line)
fmt.Fprintln(w.writer, entry.Message)
}
53 changes: 53 additions & 0 deletions loggoemoji/writer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2021 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.

package loggoemoji

import (
"bytes"
"fmt"

"github.com/juju/loggo"
gc "gopkg.in/check.v1"
)

type WriterSuite struct{}

var _ = gc.Suite(&WriterSuite{})

func (s *WriterSuite) TestWriteEmoji(c *gc.C) {
tests := []struct {
Level loggo.Level
Expected string
}{{
Level: loggo.TRACE,
Expected: "✏️",
}, {
Level: loggo.DEBUG,
Expected: "🐞",
}, {
Level: loggo.INFO,
Expected: "🧐",
}, {
Level: loggo.WARNING,
Expected: "⚠️ ",
}, {
Level: loggo.ERROR,
Expected: "😱",
}, {
Level: loggo.CRITICAL,
Expected: "💥",
}}
for i, test := range tests {
c.Logf("test %d level %s", i, test.Level.Short())

buf := new(bytes.Buffer)
writer := NewWriter(buf)
writer.Write(loggo.Entry{
Level: test.Level,
Message: "Hello",
})

c.Assert(buf.String(), gc.Equals, fmt.Sprintf("00:00:00 %s %s .:0 Hello\n", test.Level.Short(), test.Expected))
}
}