mirror of
https://github.com/go-micro/go-micro.git
synced 2024-12-24 10:07:04 +02:00
Added debug.Logger interface
This commit is contained in:
parent
3f7f2afc7b
commit
ee9776e7b2
@ -17,6 +17,14 @@ type Entry struct {
|
||||
Timestamp time.Time
|
||||
}
|
||||
|
||||
// New returns a new buffer of the given size
|
||||
func New(i int) *Buffer {
|
||||
return &Buffer{
|
||||
size: i,
|
||||
}
|
||||
}
|
||||
|
||||
// Put adds a new value to ring buffer
|
||||
func (b *Buffer) Put(v interface{}) {
|
||||
b.Lock()
|
||||
defer b.Unlock()
|
||||
@ -83,13 +91,7 @@ func (b *Buffer) Since(t time.Time) []*Entry {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Size returns the size of the ring buffer
|
||||
func (b *Buffer) Size() int {
|
||||
return b.size
|
||||
}
|
||||
|
||||
// New returns a new buffer of the given size
|
||||
func New(i int) *Buffer {
|
||||
return &Buffer{
|
||||
size: i,
|
||||
}
|
||||
}
|
||||
|
48
debug/log/default.go
Normal file
48
debug/log/default.go
Normal file
@ -0,0 +1,48 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
golog "log"
|
||||
|
||||
"github.com/micro/go-micro/debug/buffer"
|
||||
)
|
||||
|
||||
var (
|
||||
// DefaultSize of the logger buffer
|
||||
DefaultSize = 1000
|
||||
)
|
||||
|
||||
// defaultLogger is default micro logger
|
||||
type defaultLogger struct {
|
||||
*buffer.Buffer
|
||||
}
|
||||
|
||||
// NewLogger returns default Logger with
|
||||
func NewLogger(opts ...Option) Logger {
|
||||
// get default options
|
||||
options := DefaultOptions()
|
||||
|
||||
// apply requested options
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
return &defaultLogger{
|
||||
Buffer: buffer.New(options.Size),
|
||||
}
|
||||
}
|
||||
|
||||
// Write writes log into logger
|
||||
func (l *defaultLogger) Write(v ...interface{}) {
|
||||
l.log(fmt.Sprint(v...))
|
||||
golog.Print(v...)
|
||||
}
|
||||
|
||||
// Read reads logs from the logger
|
||||
func (l *defaultLogger) Read(n int) []interface{} {
|
||||
return l.Get(n)
|
||||
}
|
||||
|
||||
func (l *defaultLogger) log(entry string) {
|
||||
l.Buffer.Put(entry)
|
||||
}
|
26
debug/log/default_test.go
Normal file
26
debug/log/default_test.go
Normal file
@ -0,0 +1,26 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLogger(t *testing.T) {
|
||||
// make sure we have the right size of the logger ring buffer
|
||||
if logger.(*defaultLogger).Size() != DefaultSize {
|
||||
t.Errorf("expected buffer size: %d, got: %d", DefaultSize, logger.(*defaultLogger).Size())
|
||||
}
|
||||
|
||||
// Log some cruft
|
||||
Log("foobar")
|
||||
Logf("foo %s", "bar")
|
||||
|
||||
// Check if the logs are stored in the logger ring buffer
|
||||
expectedEntries := []string{"foobar", "foo bar"}
|
||||
entries := logger.Read(len(expectedEntries))
|
||||
for i, entry := range entries {
|
||||
if !reflect.DeepEqual(entry, expectedEntries[i]) {
|
||||
t.Errorf("expected %s, got %s", expectedEntries[i], entry)
|
||||
}
|
||||
}
|
||||
}
|
171
debug/log/logger.go
Normal file
171
debug/log/logger.go
Normal file
@ -0,0 +1,171 @@
|
||||
// Package log provides debug logging
|
||||
package log
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
// logger is default Logger
|
||||
logger Logger = NewLogger()
|
||||
// default log level is info
|
||||
level = LevelInfo
|
||||
// prefix for all messages
|
||||
prefix string
|
||||
)
|
||||
|
||||
// Logger is event logger
|
||||
type Logger interface {
|
||||
// Read reads specified number of log entries
|
||||
Read(int) []interface{}
|
||||
// Write writes logs to logger
|
||||
Write(...interface{})
|
||||
}
|
||||
|
||||
// level is a log level
|
||||
type Level int
|
||||
|
||||
const (
|
||||
LevelFatal Level = iota
|
||||
LevelError
|
||||
LevelInfo
|
||||
LevelWarn
|
||||
LevelDebug
|
||||
LevelTrace
|
||||
)
|
||||
|
||||
func init() {
|
||||
switch os.Getenv("MICRO_LOG_LEVEL") {
|
||||
case "trace":
|
||||
level = LevelTrace
|
||||
case "debug":
|
||||
level = LevelDebug
|
||||
case "warn":
|
||||
level = LevelWarn
|
||||
case "info":
|
||||
level = LevelInfo
|
||||
case "error":
|
||||
level = LevelError
|
||||
case "fatal":
|
||||
level = LevelFatal
|
||||
}
|
||||
}
|
||||
|
||||
func Log(v ...interface{}) {
|
||||
if len(prefix) > 0 {
|
||||
logger.Write(fmt.Sprint(append([]interface{}{prefix, " "}, v...)...))
|
||||
return
|
||||
}
|
||||
logger.Write(fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
func Logf(format string, v ...interface{}) {
|
||||
if len(prefix) > 0 {
|
||||
format = prefix + " " + format
|
||||
}
|
||||
logger.Write(fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
// WithLevel logs with the level specified
|
||||
func WithLevel(l Level, v ...interface{}) {
|
||||
if l > level {
|
||||
return
|
||||
}
|
||||
Log(v...)
|
||||
}
|
||||
|
||||
// WithLevel logs with the level specified
|
||||
func WithLevelf(l Level, format string, v ...interface{}) {
|
||||
if l > level {
|
||||
return
|
||||
}
|
||||
Logf(format, v...)
|
||||
}
|
||||
|
||||
// Trace provides trace level logging
|
||||
func Trace(v ...interface{}) {
|
||||
WithLevel(LevelTrace, v...)
|
||||
}
|
||||
|
||||
// Tracef provides trace level logging
|
||||
func Tracef(format string, v ...interface{}) {
|
||||
WithLevelf(LevelTrace, format, v...)
|
||||
}
|
||||
|
||||
// Debug provides debug level logging
|
||||
func Debug(v ...interface{}) {
|
||||
WithLevel(LevelDebug, v...)
|
||||
}
|
||||
|
||||
// Debugf provides debug level logging
|
||||
func Debugf(format string, v ...interface{}) {
|
||||
WithLevelf(LevelDebug, format, v...)
|
||||
}
|
||||
|
||||
// Warn provides warn level logging
|
||||
func Warn(v ...interface{}) {
|
||||
WithLevel(LevelWarn, v...)
|
||||
}
|
||||
|
||||
// Warnf provides warn level logging
|
||||
func Warnf(format string, v ...interface{}) {
|
||||
WithLevelf(LevelWarn, format, v...)
|
||||
}
|
||||
|
||||
// Info provides info level logging
|
||||
func Info(v ...interface{}) {
|
||||
WithLevel(LevelInfo, v...)
|
||||
}
|
||||
|
||||
// Infof provides info level logging
|
||||
func Infof(format string, v ...interface{}) {
|
||||
WithLevelf(LevelInfo, format, v...)
|
||||
}
|
||||
|
||||
// Error provides warn level logging
|
||||
func Error(v ...interface{}) {
|
||||
WithLevel(LevelError, v...)
|
||||
}
|
||||
|
||||
// Errorf provides warn level logging
|
||||
func Errorf(format string, v ...interface{}) {
|
||||
WithLevelf(LevelError, format, v...)
|
||||
}
|
||||
|
||||
// Fatal logs with Log and then exits with os.Exit(1)
|
||||
func Fatal(v ...interface{}) {
|
||||
WithLevel(LevelFatal, v...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Fatalf logs with Logf and then exits with os.Exit(1)
|
||||
func Fatalf(format string, v ...interface{}) {
|
||||
WithLevelf(LevelFatal, format, v...)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// GetLogger returns the local logger
|
||||
func GetLogger() Logger {
|
||||
return logger
|
||||
}
|
||||
|
||||
// SetLevel sets the log level
|
||||
func SetLevel(l Level) {
|
||||
level = l
|
||||
}
|
||||
|
||||
// GetLevel returns the current level
|
||||
func GetLevel() Level {
|
||||
return level
|
||||
}
|
||||
|
||||
// Set a prefix for the logger
|
||||
func SetPrefix(p string) {
|
||||
prefix = p
|
||||
}
|
||||
|
||||
// Set service name
|
||||
func Name(name string) {
|
||||
prefix = fmt.Sprintf("[%s]", name)
|
||||
}
|
24
debug/log/options.go
Normal file
24
debug/log/options.go
Normal file
@ -0,0 +1,24 @@
|
||||
package log
|
||||
|
||||
// Option used by the logger
|
||||
type Option func(*Options)
|
||||
|
||||
// Options are logger options
|
||||
type Options struct {
|
||||
// Size is the size of ring buffer
|
||||
Size int
|
||||
}
|
||||
|
||||
// Size sets the size of the ring buffer
|
||||
func Size(s int) Option {
|
||||
return func(o *Options) {
|
||||
o.Size = s
|
||||
}
|
||||
}
|
||||
|
||||
// DefaultOptions returns default options
|
||||
func DefaultOptions() Options {
|
||||
return Options{
|
||||
Size: DefaultSize,
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user