-
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.
Split printer package to 3 packages: printer, console and logadapter `printer` package is not very obvious to newcomers. If someone is searching for console or log adapters he will not realize that printer package does that.
- Loading branch information
Showing
14 changed files
with
170 additions
and
88 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/elgopher/yala/adapter/console" | ||
"github.com/elgopher/yala/logger" | ||
) | ||
|
||
var ErrSome = errors.New("ErrSome") | ||
|
||
// This example shows how to use yala with minimal console adapter | ||
func main() { | ||
ctx := context.Background() | ||
|
||
// log to console, stdout | ||
log := logger.Local{Adapter: console.StdoutAdapter()} | ||
|
||
log.Debug(ctx, "Hello fmt") | ||
log.With(ctx, "field_name", "field_value").Info("Some info") | ||
log.With(ctx, "parameter", "some value").Warn("Deprecated configuration parameter. It will be removed.") | ||
log.WithError(ctx, ErrSome).Error("Some error") | ||
} |
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,35 @@ | ||
// (c) 2022 Jacek Olszak | ||
// This code is licensed under MIT license (see LICENSE for details) | ||
|
||
package console | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/elgopher/yala/adapter/printer" | ||
"github.com/elgopher/yala/logger" | ||
) | ||
|
||
// StdoutAdapter returns a logger.Adapter implementation which prints log messages to stdout. | ||
func StdoutAdapter() logger.Adapter { // nolint | ||
return printer.Adapter{Printer: WriterPrinter{os.Stdout}} | ||
} | ||
|
||
// StderrAdapter returns a logger.Adapter implementation which prints log messages to stderr. | ||
func StderrAdapter() logger.Adapter { // nolint | ||
return printer.Adapter{Printer: WriterPrinter{os.Stderr}} | ||
} | ||
|
||
type WriterPrinter struct { | ||
Writer io.Writer | ||
} | ||
|
||
func (p WriterPrinter) Println(args ...interface{}) { | ||
if p.Writer == nil { | ||
return | ||
} | ||
|
||
_, _ = fmt.Fprintln(p.Writer, args...) | ||
} |
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,21 @@ | ||
// (c) 2022 Jacek Olszak | ||
// This code is licensed under MIT license (see LICENSE for details) | ||
|
||
package console_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/elgopher/yala/adapter/console" | ||
"github.com/elgopher/yala/adapter/internal/benchmark" | ||
"github.com/elgopher/yala/adapter/printer" | ||
) | ||
|
||
func BenchmarkPrinter(b *testing.B) { | ||
adapter := printer.Adapter{ | ||
Printer: console.WriterPrinter{ | ||
Writer: benchmark.DiscardWriter{}, | ||
}, | ||
} | ||
benchmark.Adapter(b, adapter) | ||
} |
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,20 @@ | ||
// (c) 2022 Jacek Olszak | ||
// This code is licensed under MIT license (see LICENSE for details) | ||
|
||
package console_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/elgopher/yala/adapter/console" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestWriterPrinter_Println(t *testing.T) { | ||
t.Run("should not panic when Writer is nil", func(t *testing.T) { | ||
p := console.WriterPrinter{Writer: nil} | ||
assert.NotPanics(t, func() { | ||
p.Println("") | ||
}) | ||
}) | ||
} |
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,28 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"log" | ||
"os" | ||
|
||
"github.com/elgopher/yala/adapter/logadapter" | ||
"github.com/elgopher/yala/logger" | ||
) | ||
|
||
var ErrSome = errors.New("ErrSome") | ||
|
||
// This example shows how to use yala with standard `log` package | ||
func main() { | ||
ctx := context.Background() | ||
|
||
// log using standard log package | ||
standardLog := log.New(os.Stdout, "", log.LstdFlags) | ||
adapter := logadapter.Adapter(standardLog) | ||
yalaLogger := logger.Local{Adapter: adapter} | ||
|
||
yalaLogger.Debug(ctx, "Hello standard log") | ||
yalaLogger.With(ctx, "f1", "v1").With("f2", "f2").Info("Some info") | ||
yalaLogger.With(ctx, "parameter", "some").Warn("Deprecated configuration parameter. It will be removed.") | ||
yalaLogger.WithError(ctx, ErrSome).Error("Some error") | ||
} |
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,12 @@ | ||
package logadapter | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/elgopher/yala/adapter/printer" | ||
"github.com/elgopher/yala/logger" | ||
) | ||
|
||
func Adapter(l *log.Logger) logger.Adapter { // nolint | ||
return printer.Adapter{Printer: l} | ||
} |
6 changes: 4 additions & 2 deletions
6
adapter/printer/printer_bench_test.go → adapter/logadapter/logadapter_bench_test.go
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,16 +1,18 @@ | ||
// (c) 2022 Jacek Olszak | ||
// This code is licensed under MIT license (see LICENSE for details) | ||
|
||
package printer_test | ||
package logadapter_test | ||
|
||
import ( | ||
"log" | ||
"testing" | ||
|
||
"github.com/elgopher/yala/adapter/internal/benchmark" | ||
"github.com/elgopher/yala/adapter/printer" | ||
) | ||
|
||
func BenchmarkPrinter(b *testing.B) { | ||
adapter := printer.Adapter{Printer: printer.WriterPrinter{Writer: benchmark.DiscardWriter{}}} | ||
standardLog := log.New(benchmark.DiscardWriter{}, "", log.LstdFlags) | ||
adapter := printer.Adapter{Printer: standardLog} | ||
benchmark.Adapter(b, adapter) | ||
} |
This file was deleted.
Oops, something went wrong.
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
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