strip logger

This commit is contained in:
Asim Aslam 2019-12-17 18:34:21 +00:00
parent 5a52593e66
commit e9efcbe8dc
5 changed files with 18 additions and 167 deletions

View File

@ -18,7 +18,10 @@ func (k *klog) Write(l log.Record) error {
}
func (k *klog) Stream() (log.Stream, error) {
return &klogStreamer{}, nil
return &klogStreamer{
streamChan: make(chan log.Record),
stop: make(chan bool),
}, nil
}
// New returns a configured Kubernetes logger

View File

@ -18,17 +18,23 @@ func write(l log.Record) error {
}
type klogStreamer struct {
// the k8s log stream
streamChan chan log.Record
// the stop chan
stop chan bool
}
func (k *klogStreamer) Chan() <-chan log.Record {
if k.streamChan == nil {
k.streamChan = make(chan log.Record)
}
return k.streamChan
}
func (k *klogStreamer) Stop() error {
close(k.streamChan)
select {
case <-k.stop:
return nil
default:
close(k.stop)
close(k.streamChan)
}
return nil
}

View File

@ -1,35 +0,0 @@
// Package log provides debug logging
package log
import (
"os"
)
// 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":
DefaultLevel = LevelTrace
case "debug":
DefaultLevel = LevelDebug
case "warn":
DefaultLevel = LevelWarn
case "info":
DefaultLevel = LevelInfo
case "error":
DefaultLevel = LevelError
case "fatal":
DefaultLevel = LevelFatal
}
}

View File

@ -6,17 +6,13 @@ import (
)
var (
// DefaultLog logger
DefaultLog = NewLog()
// DefaultLevel is default log level
DefaultLevel = LevelInfo
// Default buffer size if any
DefaultSize = 1024
// prefix for all messages
prefix string
// DefaultLog logger
DefaultLog = NewLog()
)
// Log is event log
// Log is debug log interface for reading and writing logs
type Log interface {
// Read reads log entries from the logger
Read(...ReadOption) ([]Record, error)
@ -36,6 +32,7 @@ type Record struct {
Metadata map[string]string `json:"metadata"`
}
// Stream returns a log stream
type Stream interface {
Chan() <-chan Record
Stop() error

View File

@ -1,120 +0,0 @@
// Package log provides debug logging
package log
import (
"fmt"
"os"
)
func log(v ...interface{}) {
if len(prefix) > 0 {
DefaultLog.Write(Record{Value: fmt.Sprint(append([]interface{}{prefix, " "}, v...)...)})
return
}
DefaultLog.Write(Record{Value: fmt.Sprint(v...)})
}
func logf(format string, v ...interface{}) {
if len(prefix) > 0 {
format = prefix + " " + format
}
DefaultLog.Write(Record{Value: fmt.Sprintf(format, v...)})
}
// WithLevel logs with the level specified
func WithLevel(l Level, v ...interface{}) {
if l > DefaultLevel {
return
}
log(v...)
}
// WithLevel logs with the level specified
func WithLevelf(l Level, format string, v ...interface{}) {
if l > DefaultLevel {
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)
}
// SetLevel sets the log level
func SetLevel(l Level) {
DefaultLevel = l
}
// GetLevel returns the current level
func GetLevel() Level {
return DefaultLevel
}
// Set a prefix for the logger
func SetPrefix(p string) {
prefix = p
}
// Set service name
func SetName(name string) {
prefix = fmt.Sprintf("[%s]", name)
}