Skip to content

Commit

Permalink
Default logger per driver
Browse files Browse the repository at this point in the history
  • Loading branch information
maxence-charriere committed May 26, 2018
1 parent dbb9d6c commit 6b3c57b
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 24 deletions.
4 changes: 2 additions & 2 deletions app_test.go
Expand Up @@ -26,7 +26,7 @@ func TestApp(t *testing.T) {
var newPage func(c app.PageConfig) (app.Page, error)

output := &bytes.Buffer{}
app.Loggers = []app.Logger{app.NewLogger(output, output, true)}
app.Loggers = []app.Logger{app.NewLogger(output, output, true, true)}

app.Import(&tests.Foo{})
app.Import(&tests.Bar{})
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestAppError(t *testing.T) {
var d app.Driver

output := &bytes.Buffer{}
app.Loggers = []app.Logger{app.NewLogger(output, output, true)}
app.Loggers = []app.Logger{app.NewLogger(output, output, true, true)}

ctx, cancel := context.WithCancel(context.Background())

Expand Down
6 changes: 6 additions & 0 deletions drivers/mac/driver.go
Expand Up @@ -29,6 +29,12 @@ var (
driver *Driver
)

func init() {
app.Loggers = []app.Logger{
app.NewLogger(os.Stdout, os.Stderr, true, true),
}
}

// Driver is the app.Driver implementation for MacOS.
type Driver struct {
app.BaseDriver
Expand Down
7 changes: 7 additions & 0 deletions drivers/web/client.go
Expand Up @@ -5,6 +5,7 @@ package web

import (
"context"
"os"
"path"

"github.com/gopherjs/gopherjs/js"
Expand All @@ -15,6 +16,12 @@ var (
driver *Driver
)

func init() {
app.Loggers = []app.Logger{
app.NewLogger(os.Stdout, os.Stderr, true, false),
}
}

// Run satisfies the app.Driver interface.
func (d *Driver) Run(f app.Factory) error {
d.factory = f
Expand Down
7 changes: 7 additions & 0 deletions drivers/web/server.go
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"html/template"
"net/http"
"os"
"path"

"github.com/murlokswarm/app/appjs"
Expand All @@ -15,6 +16,12 @@ import (
"github.com/murlokswarm/app/html"
)

func init() {
app.Loggers = []app.Logger{
app.NewLogger(os.Stdout, os.Stderr, true, true),
}
}

// Run satisfies the app.Driver interface.
func (d *Driver) Run(f app.Factory) error {
d.factory = f
Expand Down
5 changes: 0 additions & 5 deletions examples/test/bin/test-mac/main.go
Expand Up @@ -4,18 +4,13 @@ package main

import (
"fmt"
"os"

"github.com/murlokswarm/app"
"github.com/murlokswarm/app/drivers/mac"
"github.com/murlokswarm/app/examples/test"
)

func main() {
app.Loggers = []app.Logger{
app.NewLogger(os.Stdout, os.Stderr, true),
}

app.Import(&test.Webview{})
app.Import(&test.Menu{})

Expand Down
6 changes: 0 additions & 6 deletions examples/test/bin/test-web/main.go
@@ -1,18 +1,12 @@
package main

import (
"os"

"github.com/murlokswarm/app"
"github.com/murlokswarm/app/drivers/web"
"github.com/murlokswarm/app/examples/test"
)

func main() {
app.Loggers = []app.Logger{
app.NewLogger(os.Stdout, os.Stderr, true),
}

app.Import(&test.Webview{})
app.Import(&test.Menu{})

Expand Down
20 changes: 12 additions & 8 deletions log.go
Expand Up @@ -3,7 +3,6 @@ package app
import (
"fmt"
"io"
"os"
"time"
)

Expand All @@ -22,9 +21,7 @@ type Logger interface {

var (
// Loggers is the loggers used by the app.
Loggers = []Logger{
NewLogger(os.Stdout, os.Stderr, false),
}
Loggers []Logger
)

// Log logs a message according to a format specifier.
Expand Down Expand Up @@ -56,7 +53,7 @@ func WhenDebug(f func()) {

// NewLogger creates a logger that writes on the given writers.
// Logs that contain errors are logged on werr.
func NewLogger(wout, werr io.Writer, debug bool) Logger {
func NewLogger(wout, werr io.Writer, debug, colors bool) Logger {
whenDebug := func(f func()) {}

if debug {
Expand All @@ -69,14 +66,15 @@ func NewLogger(wout, werr io.Writer, debug bool) Logger {
wout: wout,
werr: wout,
whenDebug: whenDebug,
colors: colors,
}
}

type logger struct {
wout io.Writer
werr io.Writer
whenDebug func(func())
indent string
colors bool
}

func (l *logger) Log(format string, v ...interface{}) {
Expand Down Expand Up @@ -114,6 +112,7 @@ func (l *logger) print(level int, format string, v ...interface{}) {
func (l *logger) prefix(level int) string {
logLevel := "INFO "
color := infoColor
endColor := defaultColor

switch level {
case levelError:
Expand All @@ -125,13 +124,18 @@ func (l *logger) prefix(level int) string {
color = debugColor
}

if !l.colors {
color = ""
endColor = ""
}

return fmt.Sprintf("%s%s%s %s %s|>%s ",
color,
logLevel,
defaultColor,
endColor,
time.Now().Format("2006/01/02 15:04:05"),
color,
defaultColor,
endColor,
)
}

Expand Down
6 changes: 3 additions & 3 deletions log_test.go
Expand Up @@ -11,7 +11,7 @@ import (
func TestLogs(t *testing.T) {
buff := &bytes.Buffer{}
Loggers = []Logger{
NewLogger(buff, buff, true),
NewLogger(buff, buff, true, true),
}

Log("hello world")
Expand All @@ -24,7 +24,7 @@ func TestLogs(t *testing.T) {

func TestLogger(t *testing.T) {
buffer := &bytes.Buffer{}
logger := NewLogger(buffer, buffer, true)
logger := NewLogger(buffer, buffer, true, false)

logger.Log("a message")
logger.Log("a message with args: %v", 42)
Expand All @@ -38,7 +38,7 @@ func TestLogger(t *testing.T) {
})
assert.Contains(t, buffer.String(), "yoda is strong")

logger = NewLogger(buffer, buffer, false)
logger = NewLogger(buffer, buffer, false, true)
logger.WhenDebug(func() {
logger.Debug("vader is strong")
})
Expand Down

0 comments on commit 6b3c57b

Please sign in to comment.