mirror of
https://github.com/jpillora/chisel
synced 2026-06-08 15:07:02 +00:00
54 lines
1.0 KiB
Go
54 lines
1.0 KiB
Go
package chisel
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
//Logger is ...
|
|
type Logger struct {
|
|
prefix string
|
|
logger *log.Logger
|
|
Info, Debug bool
|
|
}
|
|
|
|
func NewLogger(prefix string) *Logger {
|
|
return NewLoggerFlag(prefix, log.Ldate|log.Ltime)
|
|
}
|
|
|
|
func NewLoggerFlag(prefix string, flag int) *Logger {
|
|
l := &Logger{
|
|
prefix: prefix,
|
|
logger: log.New(os.Stdout, "", flag),
|
|
Info: false,
|
|
Debug: false,
|
|
}
|
|
return l
|
|
}
|
|
|
|
func (l *Logger) Infof(f string, args ...interface{}) {
|
|
if l.Info {
|
|
l.logger.Printf(l.prefix+": "+f, args...)
|
|
}
|
|
}
|
|
|
|
func (l *Logger) Debugf(f string, args ...interface{}) {
|
|
if l.Debug {
|
|
l.logger.Printf(l.prefix+": "+f, args...)
|
|
}
|
|
}
|
|
|
|
func (l *Logger) Errorf(f string, args ...interface{}) error {
|
|
return fmt.Errorf(l.prefix+": "+f, args...)
|
|
}
|
|
|
|
func (l *Logger) Fork(prefix string, args ...interface{}) *Logger {
|
|
//slip the parent prefix at the front
|
|
args = append([]interface{}{l.prefix}, args...)
|
|
ll := NewLogger(fmt.Sprintf("%s: "+prefix, args...))
|
|
ll.Info = l.Info
|
|
ll.Debug = l.Debug
|
|
return ll
|
|
}
|