Skip to content

Commit

Permalink
update log to use new ansi library to reduce duplicate code in multip…
Browse files Browse the repository at this point in the history
…le projects.
  • Loading branch information
Dean Karn authored and Dean Karn committed Oct 14, 2016
1 parent 3995d42 commit a2ed974
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 87 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## log
<img align="right" src="https://raw.githubusercontent.com/go-playground/log/master/logo.png">
![Project status](https://img.shields.io/badge/version-4.0.1-green.svg)
![Project status](https://img.shields.io/badge/version-4.1.0-green.svg)
[![Build Status](https://semaphoreci.com/api/v1/joeybloggs/log/branches/master/badge.svg)](https://semaphoreci.com/joeybloggs/log)
[![Coverage Status](https://coveralls.io/repos/github/go-playground/log/badge.svg?branch=master)](https://coveralls.io/github/go-playground/log?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/log)](https://goreportcard.com/report/github.com/go-playground/log)
Expand Down
29 changes: 0 additions & 29 deletions colors.go

This file was deleted.

33 changes: 17 additions & 16 deletions handlers/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"

"github.com/go-playground/ansi"
"github.com/go-playground/log"
)

Expand All @@ -34,7 +35,7 @@ const (
type Console struct {
buffer uint
numWorkers uint
colors [9]log.ANSIEscSeq
colors [9]ansi.EscSeq
writer io.Writer
formatFunc FormatFunc
timestampFormat string
Expand All @@ -45,16 +46,16 @@ type Console struct {
}

// Colors mapping.
var defaultColors = [...]log.ANSIEscSeq{
log.DebugLevel: log.Green,
log.TraceLevel: log.White,
log.InfoLevel: log.Blue,
log.NoticeLevel: log.LightCyan,
log.WarnLevel: log.Yellow,
log.ErrorLevel: log.LightRed,
log.PanicLevel: log.Red,
log.AlertLevel: log.Red + log.Underscore,
log.FatalLevel: log.Red + log.Underscore + log.Blink,
var defaultColors = [...]ansi.EscSeq{
log.DebugLevel: ansi.Green,
log.TraceLevel: ansi.White,
log.InfoLevel: ansi.Blue,
log.NoticeLevel: ansi.LightCyan,
log.WarnLevel: ansi.Yellow,
log.ErrorLevel: ansi.LightRed,
log.PanicLevel: ansi.Red,
log.AlertLevel: ansi.Red + ansi.Underline,
log.FatalLevel: ansi.Red + ansi.Underline + ansi.Blink,
}

// New returns a new instance of the console logger
Expand Down Expand Up @@ -99,7 +100,7 @@ func (c *Console) DisplayColor() bool {
}

// GetDisplayColor returns the color for the given log level
func (c *Console) GetDisplayColor(level log.Level) log.ANSIEscSeq {
func (c *Console) GetDisplayColor(level log.Level) ansi.EscSeq {
return c.colors[level]
}

Expand Down Expand Up @@ -229,7 +230,7 @@ func defaultFormatFunc(c *Console) Formatter {

if c.DisplayColor() {

var color log.ANSIEscSeq
var color ansi.EscSeq

return func(e *log.Entry) []byte {
b = b[0:0]
Expand All @@ -247,7 +248,7 @@ func defaultFormatFunc(c *Console) Formatter {
b = append(b, space)
}
b = append(b, lvl...)
b = append(b, log.Reset...)
b = append(b, ansi.Reset...)
b = append(b, space)
b = append(b, e.Message...)

Expand Down Expand Up @@ -277,7 +278,7 @@ func defaultFormatFunc(c *Console) Formatter {
}

b = append(b, lvl...)
b = append(b, log.Reset...)
b = append(b, ansi.Reset...)
b = append(b, space)
b = append(b, file...)
b = append(b, colon)
Expand All @@ -290,7 +291,7 @@ func defaultFormatFunc(c *Console) Formatter {
b = append(b, space)
b = append(b, color...)
b = append(b, f.Key...)
b = append(b, log.Reset...)
b = append(b, ansi.Reset...)
b = append(b, equals)

switch f.Value.(type) {
Expand Down
33 changes: 17 additions & 16 deletions handlers/syslog/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

syslog "github.com/RackSec/srslog"

"github.com/go-playground/ansi"
"github.com/go-playground/log"
)

Expand All @@ -34,7 +35,7 @@ const (
type Syslog struct {
buffer uint
numWorkers uint
colors [9]log.ANSIEscSeq
colors [9]ansi.EscSeq
writer *syslog.Writer
formatFunc FormatFunc
timestampFormat string
Expand All @@ -45,16 +46,16 @@ type Syslog struct {

var (
// Colors mapping.
defaultColors = [...]log.ANSIEscSeq{
log.DebugLevel: log.Green,
log.TraceLevel: log.White,
log.InfoLevel: log.Blue,
log.NoticeLevel: log.LightCyan,
log.WarnLevel: log.Yellow,
log.ErrorLevel: log.LightRed,
log.PanicLevel: log.Red,
log.AlertLevel: log.Red + log.Underscore,
log.FatalLevel: log.Red + log.Underscore + log.Blink,
defaultColors = [...]ansi.EscSeq{
log.DebugLevel: ansi.Green,
log.TraceLevel: ansi.White,
log.InfoLevel: ansi.Blue,
log.NoticeLevel: ansi.LightCyan,
log.WarnLevel: ansi.Yellow,
log.ErrorLevel: ansi.LightRed,
log.PanicLevel: ansi.Red,
log.AlertLevel: ansi.Red + ansi.Underline,
log.FatalLevel: ansi.Red + ansi.Underline + ansi.Blink,
}
)

Expand Down Expand Up @@ -112,7 +113,7 @@ func (s *Syslog) DisplayColor() bool {
}

// GetDisplayColor returns the color for the given log level
func (s *Syslog) GetDisplayColor(level log.Level) log.ANSIEscSeq {
func (s *Syslog) GetDisplayColor(level log.Level) ansi.EscSeq {
return s.colors[level]
}

Expand Down Expand Up @@ -222,7 +223,7 @@ func defaultFormatFunc(s *Syslog) Formatter {

if s.DisplayColor() {

var color log.ANSIEscSeq
var color ansi.EscSeq

return func(e *log.Entry) []byte {
b = b[0:0]
Expand All @@ -240,7 +241,7 @@ func defaultFormatFunc(s *Syslog) Formatter {
b = append(b, space)
}
b = append(b, lvl...)
b = append(b, log.Reset...)
b = append(b, ansi.Reset...)
b = append(b, space)
b = append(b, e.Message...)

Expand Down Expand Up @@ -270,7 +271,7 @@ func defaultFormatFunc(s *Syslog) Formatter {
}

b = append(b, lvl...)
b = append(b, log.Reset...)
b = append(b, ansi.Reset...)
b = append(b, space)
b = append(b, file...)
b = append(b, colon)
Expand All @@ -283,7 +284,7 @@ func defaultFormatFunc(s *Syslog) Formatter {
b = append(b, space)
b = append(b, color...)
b = append(b, f.Key...)
b = append(b, log.Reset...)
b = append(b, ansi.Reset...)
b = append(b, equals)

switch f.Value.(type) {
Expand Down
50 changes: 25 additions & 25 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sync"
"testing"
"time"

"github.com/go-playground/ansi"
)

// NOTES:
Expand All @@ -23,9 +25,7 @@ import (
func TestConsoleLogger1(t *testing.T) {
stackTraceLimit = 1000
tests := getLogTests1()

buff := new(bytes.Buffer)

th := &testHandler{
writer: buff,
}
Expand Down Expand Up @@ -396,29 +396,29 @@ func TestFatal(t *testing.T) {

func TestColors(t *testing.T) {

fmt.Printf("%sBlack%s\n", Black, Reset)
fmt.Printf("%sDarkGray%s\n", DarkGray, Reset)
fmt.Printf("%sBlue%s\n", Blue, Reset)
fmt.Printf("%sLightBlue%s\n", LightBlue, Reset)
fmt.Printf("%sGreen%s\n", Green, Reset)
fmt.Printf("%sLightGreen%s\n", LightGreen, Reset)
fmt.Printf("%sCyan%s\n", Cyan, Reset)
fmt.Printf("%sLightCyan%s\n", LightCyan, Reset)
fmt.Printf("%sRed%s\n", Red, Reset)
fmt.Printf("%sLightRed%s\n", LightRed, Reset)
fmt.Printf("%sMagenta%s\n", Magenta, Reset)
fmt.Printf("%sLightMagenta%s\n", LightMagenta, Reset)
fmt.Printf("%sBrown%s\n", Brown, Reset)
fmt.Printf("%sYellow%s\n", Yellow, Reset)
fmt.Printf("%sLightGray%s\n", LightGray, Reset)
fmt.Printf("%sWhite%s\n", White, Reset)

fmt.Printf("%s%sUnderscoreRed%s\n", Red, Underscore, Reset)
fmt.Printf("%s%sBlinkRed%s\n", Red, Blink, Reset)
fmt.Printf("%s%s%sBlinkUnderscoreRed%s\n", Red, Blink, Underscore, Reset)

fmt.Printf("%s%sRedInverse%s\n", Red, Inverse, Reset)
fmt.Printf("%sGreenInverse%s\n", Green+Inverse, Reset)
fmt.Printf("%sBlack%s\n", ansi.Black, ansi.Reset)
fmt.Printf("%sDarkGray%s\n", ansi.DarkGray, ansi.Reset)
fmt.Printf("%sBlue%s\n", ansi.Blue, ansi.Reset)
fmt.Printf("%sLightBlue%s\n", ansi.LightBlue, ansi.Reset)
fmt.Printf("%sGreen%s\n", ansi.Green, ansi.Reset)
fmt.Printf("%sLightGreen%s\n", ansi.LightGreen, ansi.Reset)
fmt.Printf("%sCyan%s\n", ansi.Cyan, ansi.Reset)
fmt.Printf("%sLightCyan%s\n", ansi.LightCyan, ansi.Reset)
fmt.Printf("%sRed%s\n", ansi.Red, ansi.Reset)
fmt.Printf("%sLightRed%s\n", ansi.LightRed, ansi.Reset)
fmt.Printf("%sMagenta%s\n", ansi.Magenta, ansi.Reset)
fmt.Printf("%sLightMagenta%s\n", ansi.LightMagenta, ansi.Reset)
fmt.Printf("%sBrown%s\n", ansi.Brown, ansi.Reset)
fmt.Printf("%sYellow%s\n", ansi.Yellow, ansi.Reset)
fmt.Printf("%sLightGray%s\n", ansi.LightGray, ansi.Reset)
fmt.Printf("%sWhite%s\n", ansi.White, ansi.Reset)

fmt.Printf("%s%sUnderscoreRed%s\n", ansi.Red, ansi.Underline, ansi.Reset)
fmt.Printf("%s%sBlinkRed%s\n", ansi.Red, ansi.Blink, ansi.Reset)
fmt.Printf("%s%s%sBlinkUnderscoreRed%s\n", ansi.Red, ansi.Blink, ansi.Underline, ansi.Reset)

fmt.Printf("%s%sRedInverse%s\n", ansi.Red, ansi.Inverse, ansi.Reset)
fmt.Printf("%sGreenInverse%s\n", ansi.Green+ansi.Inverse, ansi.Reset)
}

func getLogTests1() []test {
Expand Down

0 comments on commit a2ed974

Please sign in to comment.