-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from leancodepl/task/better-logging
Add more visually pleasing logging
- Loading branch information
Showing
8 changed files
with
174 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,32 @@ | ||
// Package cmd provides command-line interface commands. | ||
package cmd | ||
|
||
import "github.com/spf13/cobra" | ||
import ( | ||
"context" | ||
|
||
"github.com/leancodepl/poe2arb/log" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "poe2arb", | ||
Short: "POEditor JSON to Flutter ARB converter", | ||
} | ||
|
||
func Execute() { | ||
type ctxKey int | ||
|
||
const loggerKey = ctxKey(1) | ||
|
||
func Execute(logger *log.Logger) { | ||
rootCmd.AddCommand(convertCmd) | ||
rootCmd.AddCommand(poeCmd) | ||
rootCmd.AddCommand(versionCmd) | ||
|
||
rootCmd.Execute() | ||
ctx := context.WithValue(context.Background(), loggerKey, logger) | ||
|
||
rootCmd.ExecuteContext(ctx) | ||
} | ||
|
||
func getLogger(cmd *cobra.Command) *log.Logger { | ||
return cmd.Context().Value(loggerKey).(*log.Logger) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Package log provides beautiful logger for the console interface. | ||
package log | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
clr "github.com/TwiN/go-color" | ||
) | ||
|
||
type Logger struct { | ||
writer io.Writer | ||
depth int | ||
} | ||
|
||
func New(writer io.Writer) *Logger { | ||
return &Logger{ | ||
writer: writer, | ||
depth: 0, | ||
} | ||
} | ||
|
||
func (l *Logger) Info(msg string, params ...any) *Logger { | ||
l.log(clr.Blue, msg, params...) | ||
|
||
return l | ||
} | ||
|
||
func (l *Logger) Success(msg string, params ...any) *Logger { | ||
l.log(clr.Green, msg, params...) | ||
|
||
return l | ||
} | ||
|
||
func (l *Logger) Error(msg string, params ...any) *Logger { | ||
l.log(clr.Red, msg, params...) | ||
|
||
return l | ||
} | ||
|
||
func (l *Logger) log(color, msg string, params ...any) { | ||
str := strings.Repeat(" ", l.depth) | ||
str += color + " • " + clr.Reset + fmt.Sprintf(msg, params...) + "\n" | ||
|
||
fmt.Fprint(l.writer, str) | ||
} | ||
|
||
func (l *Logger) Sub() *Logger { | ||
return &Logger{ | ||
writer: l.writer, | ||
depth: l.depth + 1, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package log_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/leancodepl/poe2arb/log" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
blue = "\x1b[34m" | ||
green = "\x1b[32m" | ||
red = "\x1b[31m" | ||
reset = "\x1b[0m" | ||
) | ||
|
||
func TestLoggerInfo(t *testing.T) { | ||
var buf bytes.Buffer | ||
|
||
l := log.New(&buf) | ||
|
||
l.Info("Hello, %s!", "world") | ||
|
||
assert.Equal(t, blue+" • "+reset+"Hello, world!\n", buf.String()) | ||
} | ||
|
||
func TestLoggerSuccess(t *testing.T) { | ||
var buf bytes.Buffer | ||
|
||
l := log.New(&buf) | ||
|
||
l.Success("Hello, %s!", "world") | ||
|
||
assert.Equal(t, green+" • "+reset+"Hello, world!\n", buf.String()) | ||
} | ||
|
||
func TestLoggerError(t *testing.T) { | ||
var buf bytes.Buffer | ||
|
||
l := log.New(&buf) | ||
|
||
l.Error("Hello, %s!", "world") | ||
|
||
assert.Equal(t, red+" • "+reset+"Hello, world!\n", buf.String()) | ||
} | ||
|
||
func TestLoggerSub(t *testing.T) { | ||
var buf bytes.Buffer | ||
|
||
l := log.New(&buf) | ||
|
||
sub := l.Sub() | ||
sub.Info("test") | ||
|
||
subSub := sub.Sub() | ||
subSub.Info("test2") | ||
|
||
assert.Equal(t, " "+blue+" • "+reset+"test\n "+blue+" • "+reset+"test2\n", buf.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,14 @@ | ||
package main | ||
|
||
import "github.com/leancodepl/poe2arb/cmd" | ||
import ( | ||
"os" | ||
|
||
"github.com/leancodepl/poe2arb/cmd" | ||
"github.com/leancodepl/poe2arb/log" | ||
) | ||
|
||
func main() { | ||
cmd.Execute() | ||
logger := log.New(os.Stdout) | ||
|
||
cmd.Execute(logger) | ||
} |