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

total rewrite #7

Merged
merged 2 commits into from
Aug 31, 2023
Merged
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
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
`devslog` is zero dependency custom logging handler for Go's standard [`log/slog`](https://pkg.go.dev/log/slog) package that provides structured logging with colorful and indented structure for developing.

### Develop with this output
![image](https://github.com/golang-cz/devslog/assets/17728576/0bae7ec7-0513-41a4-9682-0e0fc74747e7)
![image](https://github.com/golang-cz/devslog/assets/17728576/cfdc1634-16fe-4dd0-a643-21bf519cd4fe)

### Instead of these outputs
`TextHandler`
![image](https://github.com/golang-cz/devslog/assets/17728576/856f7e34-dc72-4f22-bd47-9fd5cbf7dd2f)
![image](https://github.com/golang-cz/devslog/assets/17728576/49aab1c0-93ba-409d-8637-a96eeeaaf0e1)

`JSONHandler`
![image](https://github.com/golang-cz/devslog/assets/17728576/3d4b091d-813a-461d-88e1-4cc95b9d6939)
![image](https://github.com/golang-cz/devslog/assets/17728576/775af693-2f96-47e8-9190-5ead77b41a27)

## Install
```
Expand All @@ -26,7 +27,7 @@ w := os.Stdout

logger := slog.New(devslog.NewHandler(w, nil))

// set global logger
// optional: set global logger
slog.SetDefault(logger)
```

Expand Down
2 changes: 1 addition & 1 deletion attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (a attributes) Less(i, j int) bool {
func (a attributes) padding(c foregroundColor) int {
var padding int
for _, e := range a {
color := len(cs(e.Key, c))
color := len(cs([]byte(e.Key), c))
if color > padding {
padding = color
}
Expand Down
21 changes: 15 additions & 6 deletions attributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ import (
"testing"
)

func Test_AttributesLen(t *testing.T) {
func Test_Attributes(t *testing.T) {
test_AttributesLen(t)
test_AttributesSwap(t)
test_AttributesLess(t)
test_AttributesLessGroupTrue(t)
test_AttributesLessGroupFalse(t)
test_AttributesPadding(t)
}

func test_AttributesLen(t *testing.T) {
someValue := slog.StringValue("value")
attrs := attributes{
slog.Attr{Key: "key1", Value: someValue},
Expand All @@ -21,7 +30,7 @@ func Test_AttributesLen(t *testing.T) {
}
}

func Test_AttributesSwap(t *testing.T) {
func test_AttributesSwap(t *testing.T) {
attr1 := slog.Attr{Key: "key1", Value: slog.StringValue("value")}
attr2 := slog.Attr{Key: "key2", Value: slog.StringValue("value")}
attrs := attributes{
Expand All @@ -36,7 +45,7 @@ func Test_AttributesSwap(t *testing.T) {
}
}

func Test_AttributesLess(t *testing.T) {
func test_AttributesLess(t *testing.T) {
someValue := slog.StringValue("value")
attrs := attributes{
slog.Attr{Key: "key1", Value: someValue},
Expand All @@ -50,7 +59,7 @@ func Test_AttributesLess(t *testing.T) {
}
}

func Test_AttributesLessGroupTrue(t *testing.T) {
func test_AttributesLessGroupTrue(t *testing.T) {
attrs := attributes{
slog.String("key1", "someValue"),
slog.Group("key2", slog.String("someString", "someValue")),
Expand All @@ -63,7 +72,7 @@ func Test_AttributesLessGroupTrue(t *testing.T) {
}
}

func Test_AttributesLessGroupFalse(t *testing.T) {
func test_AttributesLessGroupFalse(t *testing.T) {
attrs := attributes{
slog.Group("key1", slog.String("someString", "someValue")),
slog.String("key2", "someValue"),
Expand All @@ -76,7 +85,7 @@ func Test_AttributesLessGroupFalse(t *testing.T) {
}
}

func Test_AttributesPadding(t *testing.T) {
func test_AttributesPadding(t *testing.T) {
someValue := slog.StringValue("value")
attrs := attributes{
slog.Attr{Key: "key1", Value: someValue},
Expand Down
74 changes: 41 additions & 33 deletions color.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,64 @@
package devslog

import "fmt"

type (
foregroundColor string
backgroundColor string
commonValuesColor string
foregroundColor []byte
backgroundColor []byte
commonValuesColor []byte
)

const (
var (
// Foreground colors
fgBlack foregroundColor = "\x1b[30m"
fgRed foregroundColor = "\x1b[31m"
fgGreen foregroundColor = "\x1b[32m"
fgYellow foregroundColor = "\x1b[33m"
fgBlue foregroundColor = "\x1b[34m"
fgMagenta foregroundColor = "\x1b[35m"
fgCyan foregroundColor = "\x1b[36m"
fgWhite foregroundColor = "\x1b[37m"
fgBlack foregroundColor = []byte("\x1b[30m")
fgRed foregroundColor = []byte("\x1b[31m")
fgGreen foregroundColor = []byte("\x1b[32m")
fgYellow foregroundColor = []byte("\x1b[33m")
fgBlue foregroundColor = []byte("\x1b[34m")
fgMagenta foregroundColor = []byte("\x1b[35m")
fgCyan foregroundColor = []byte("\x1b[36m")
fgWhite foregroundColor = []byte("\x1b[37m")

// Background colors
bgBlack backgroundColor = "\x1b[40m"
bgRed backgroundColor = "\x1b[41m"
bgGreen backgroundColor = "\x1b[42m"
bgYellow backgroundColor = "\x1b[43m"
bgBlue backgroundColor = "\x1b[44m"
bgMagenta backgroundColor = "\x1b[45m"
bgCyan backgroundColor = "\x1b[46m"
bgWhite backgroundColor = "\x1b[47m"
bgBlack backgroundColor = []byte("\x1b[40m")
bgRed backgroundColor = []byte("\x1b[41m")
bgGreen backgroundColor = []byte("\x1b[42m")
bgYellow backgroundColor = []byte("\x1b[43m")
bgBlue backgroundColor = []byte("\x1b[44m")
bgMagenta backgroundColor = []byte("\x1b[45m")
bgCyan backgroundColor = []byte("\x1b[46m")
bgWhite backgroundColor = []byte("\x1b[47m")

// Common consts
resetColor commonValuesColor = "\x1b[0m"
faintColor commonValuesColor = "\x1b[2m"
underlineColor commonValuesColor = "\x1b[4m"
resetColor commonValuesColor = []byte("\x1b[0m")
faintColor commonValuesColor = []byte("\x1b[2m")
underlineColor commonValuesColor = []byte("\x1b[4m")
)

// Color string foreground
func cs(text string, fgColor foregroundColor) string {
return fmt.Sprintf("%v%v%v", fgColor, text, resetColor)
func cs(b []byte, fgColor foregroundColor) []byte {
b = append(fgColor, b...)
b = append(b, resetColor...)
return b
}

// Color string fainted
func csf(text string, fgColor foregroundColor) string {
return fmt.Sprintf("%v%v%v%v", fgColor, faintColor, text, resetColor)
func csf(b []byte, fgColor foregroundColor) []byte {
b = append(fgColor, b...)
b = append(faintColor, b...)
b = append(b, resetColor...)
return b
}

// Color string background
func csb(text string, fgColor foregroundColor, bgColor backgroundColor) string {
return fmt.Sprintf("%v%v%v%v", fgColor, bgColor, text, resetColor)
func csb(b []byte, fgColor foregroundColor, bgColor backgroundColor) []byte {
b = append(fgColor, b...)
b = append(bgColor, b...)
b = append(b, resetColor...)
return b
}

// Underline text
func ul(text string) string {
return fmt.Sprintf("%v%v%v", underlineColor, text, resetColor)
func ul(b []byte) []byte {
b = append(underlineColor, b...)
b = append(b, resetColor...)
return b
}
49 changes: 29 additions & 20 deletions color_test.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
package devslog

import (
"bytes"
"testing"
)

func Test_ColorCs(t *testing.T) {
expected := "\x1b[32mHello\x1b[0m"
result := cs("Hello", fgGreen)
func Test_Color(t *testing.T) {
b := []byte("Hello")
test_ColorCs(t, b)
test_ColorCsf(t, b)
test_ColorCsb(t, b)
test_ColorUl(t, b)
}

func test_ColorCs(t *testing.T, b []byte) {
result := cs(b, fgGreen)

if result != expected {
t.Errorf("Expected: %q, but got: %q", expected, result)
expected := []byte("\x1b[32mHello\x1b[0m")
if !bytes.Equal(expected, result) {
t.Errorf("\nExpected: %s\nResult: %s\nExpected: %[1]q\nResult: %[2]q", expected, result)
}
}

func Test_ColorCsf(t *testing.T) {
expected := "\x1b[34m\x1b[2mHello\x1b[0m"
result := csf("Hello", fgBlue)
func test_ColorCsf(t *testing.T, b []byte) {
result := csf(b, fgBlue)

if result != expected {
t.Errorf("Expected: %q, but got: %q", expected, result)
expected := []byte("\x1b[2m\x1b[34mHello\x1b[0m")
if !bytes.Equal(expected, result) {
t.Errorf("\nExpected: %s\nResult: %s\nExpected: %[1]q\nResult: %[2]q", expected, result)
}
}

func Test_ColorCsb(t *testing.T) {
expected := "\x1b[35m\x1b[43mHello\x1b[0m"
result := csb("Hello", fgMagenta, bgYellow)
func test_ColorCsb(t *testing.T, b []byte) {
result := csb(b, fgYellow, bgRed)

if result != expected {
t.Errorf("Expected: %q, but got: %q", expected, result)
expected := []byte("\x1b[41m\x1b[33mHello\x1b[0m")
if !bytes.Equal(expected, result) {
t.Errorf("\nExpected: %s\nResult: %s\nExpected: %[1]q\nResult: %[2]q", expected, result)
}
}

func Test_ColorUl(t *testing.T) {
expected := "\x1b[4mHello\x1b[0m"
result := ul("Hello")
func test_ColorUl(t *testing.T, b []byte) {
result := ul(b)

if result != expected {
t.Errorf("Expected: %q, but got: %q", expected, result)
expected := []byte("\x1b[4mHello\x1b[0m")
if !bytes.Equal(expected, result) {
t.Errorf("\nExpected: %s\nResult: %s\nExpected: %[1]q\nResult: %[2]q", expected, result)
}
}
Loading