Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Commit

Permalink
fixed line logging
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Sep 10, 2021
1 parent 387f22b commit 3de0af3
Showing 1 changed file with 84 additions and 69 deletions.
153 changes: 84 additions & 69 deletions simple_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (

var _ Logger = (*SimpleLogger)(nil)

var std *SimpleLogger = nil
var std = New(log.LstdFlags)

// These flags define which text to prefix to each log entry generated by the Logger.
// These flags define which text to prefix to each Output entry generated by the Logger.
// Bits are or'ed together to control what's printed.
// Except the Lmsgprefix flag, there is no
// control over the order they appear (the order listed here)
Expand All @@ -33,7 +33,7 @@ const (
LstdFlags = Ldate | Ltime // initial values for the standard logger
)

// Level are different levels at which the SimpleLogger can log
// Level are different levels at which the SimpleLogger can Output
type Level int

// All Level(s) which SimpleLogger supports
Expand Down Expand Up @@ -85,12 +85,15 @@ var styles = map[Level]Style{
//Default returns the default SimpleLogger
//goland:noinspection GoUnusedExportedFunction
func Default() *SimpleLogger {
if std == nil {
std = New(log.LstdFlags)
}
return std
}

//SetDefault sets the default SimpleLogger
//goland:noinspection GoUnusedExportedFunction
func SetDefault(logger *SimpleLogger) {
std = logger
}

// New returns a newInt SimpleLogger implementation
//goland:noinspection GoUnusedExportedFunction
func New(flags int) *SimpleLogger {
Expand All @@ -107,17 +110,17 @@ type SimpleLogger struct {
prefix Style
}

// SetLevel sets the lowest Level to log for
// SetLevel sets the lowest Level to Output for
func (l *SimpleLogger) SetLevel(level Level) {
l.level = level
}

// SetFlags sets the log flags like: Ldate, Ltime, Lmicroseconds, Llongfile, Lshortfile, LUTC, Lmsgprefix,LstdFlags
// SetFlags sets the Output flags like: Ldate, Ltime, Lmicroseconds, Llongfile, Lshortfile, LUTC, Lmsgprefix,LstdFlags
func (l *SimpleLogger) SetFlags(flags int) {
l.logger.SetFlags(flags)
}

func (l *SimpleLogger) log(level Level, args ...interface{}) {
func (l *SimpleLogger) Output(calldepth int, level Level, v ...interface{}) {
if level < l.level {
return
}
Expand All @@ -127,90 +130,93 @@ func (l *SimpleLogger) log(level Level, args ...interface{}) {
l.logger.SetPrefix(prefixStyle.String())
}

args = append(args, "", StyleReset)
copy(args[2:], args)
v = append(v, "", StyleReset)
copy(v[2:], v)

levelStr := level.String() + " "
textStyleStr := ""
if enableColors {
levelStr = levelStyle.And(styles[level]).ApplyClear(levelStr)
levelStr = levelStyle.And(styles[level]).Apply(levelStr)
textStyleStr = textStyle.String()
}
args[0] = levelStr
args[1] = textStyleStr
v[0] = levelStr
v[1] = textStyleStr

switch level {
case LevelFatal:
l.logger.Fatal(args...)
_ = l.logger.Output(calldepth, fmt.Sprint(v...))
os.Exit(1)
case LevelPanic:
l.logger.Panic(args...)
s := fmt.Sprint(v...)
_ = l.logger.Output(calldepth, s)
panic(s)
default:
l.logger.Print(args...)
_ = l.logger.Output(calldepth, fmt.Sprint(v...))
}
}

func (l *SimpleLogger) logf(level Level, format string, args ...interface{}) {
l.log(level, fmt.Sprintf(format, args...))
func (l *SimpleLogger) Outputf(calldepth int, level Level, format string, v ...interface{}) {
l.Output(calldepth+1, level, fmt.Sprintf(format, v...))
}

// Debug logs on the LevelDebug
func (l *SimpleLogger) Debug(args ...interface{}) {
l.log(LevelDebug, args...)
func (l *SimpleLogger) Debug(v ...interface{}) {
l.Output(3, LevelDebug, v...)
}

// Debugf logs on the LevelDebug
func (l *SimpleLogger) Debugf(format string, args ...interface{}) {
l.logf(LevelDebug, format, args...)
func (l *SimpleLogger) Debugf(format string, v ...interface{}) {
l.Outputf(3, LevelDebug, format, v...)
}

// Info logs on the LevelInfo
func (l *SimpleLogger) Info(args ...interface{}) {
l.log(LevelInfo, args...)
func (l *SimpleLogger) Info(v ...interface{}) {
l.Output(3, LevelInfo, v...)
}

// Infof logs on the LevelInfo
func (l *SimpleLogger) Infof(format string, args ...interface{}) {
l.logf(LevelInfo, format, args...)
func (l *SimpleLogger) Infof(format string, v ...interface{}) {
l.Outputf(3, LevelInfo, format, v...)
}

// Warn logs on the LevelWarn
func (l *SimpleLogger) Warn(args ...interface{}) {
l.log(LevelWarn, args...)
func (l *SimpleLogger) Warn(v ...interface{}) {
l.Output(3, LevelWarn, v...)
}

// Warnf logs on the LevelWarn
func (l *SimpleLogger) Warnf(format string, args ...interface{}) {
l.logf(LevelWarn, format, args...)
func (l *SimpleLogger) Warnf(format string, v ...interface{}) {
l.Outputf(3, LevelWarn, format, v...)
}

// Error logs on the LevelError
func (l *SimpleLogger) Error(args ...interface{}) {
l.log(LevelError, args...)
func (l *SimpleLogger) Error(v ...interface{}) {
l.Output(3, LevelError, v...)
}

// Errorf logs on the LevelError
func (l *SimpleLogger) Errorf(format string, args ...interface{}) {
l.logf(LevelError, format, args...)
func (l *SimpleLogger) Errorf(format string, v ...interface{}) {
l.Outputf(3, LevelError, format, v...)
}

// Fatal logs on the LevelFatal
func (l *SimpleLogger) Fatal(args ...interface{}) {
l.log(LevelFatal, args...)
func (l *SimpleLogger) Fatal(v ...interface{}) {
l.Output(3, LevelFatal, v...)
}

// Fatalf logs on the LevelFatal
func (l *SimpleLogger) Fatalf(format string, args ...interface{}) {
l.logf(LevelFatal, format, args...)
func (l *SimpleLogger) Fatalf(format string, v ...interface{}) {
l.Outputf(3, LevelFatal, format, v...)
}

// Panic logs on the LevelPanic
func (l *SimpleLogger) Panic(args ...interface{}) {
l.log(LevelPanic, args...)
func (l *SimpleLogger) Panic(v ...interface{}) {
l.Output(3, LevelPanic, v...)
}

// Panicf logs on the LevelPanic
func (l *SimpleLogger) Panicf(format string, args ...interface{}) {
l.logf(LevelPanic, format, args...)
func (l *SimpleLogger) Panicf(format string, v ...interface{}) {
l.Outputf(3, LevelPanic, format, v...)
}

// SetLevel sets the Level of the default Logger
Expand All @@ -237,86 +243,95 @@ func EnableColors(enable bool) {
enableColors = enable
}

// SetTextColor sets the Style which is used for text of a log message
// SetTextColor sets the Style which is used for text of a Output message
//goland:noinspection GoUnusedExportedFunction
func SetTextColor(color Style) {
textStyle = color
}

// SetFlags sets the log flags like: Ldate, Ltime, Lmicroseconds, Llongfile, Lshortfile, LUTC, Lmsgprefix,LstdFlags of the default Logger
// SetFlags sets the Output flags like: Ldate, Ltime, Lmicroseconds, Llongfile, Lshortfile, LUTC, Lmsgprefix,LstdFlags of the default Logger
//goland:noinspection GoUnusedExportedFunction
func SetFlags(flags int) {
Default().SetFlags(flags)
}

// Debug logs on the LevelDebug with the default SimpleLogger
//goland:noinspection GoUnusedExportedFunction
func Debug(args ...interface{}) {
Default().Debug(args...)
func Debug(v ...interface{}) {
Output(3, LevelDebug, v...)
}

// Debugf logs on the LevelDebug with the default SimpleLogger
//goland:noinspection GoUnusedExportedFunction
func Debugf(format string, args ...interface{}) {
Default().Debugf(format, args...)
func Debugf(format string, v ...interface{}) {
Outputf(3, LevelDebug, format, v...)
}

// Info logs on the LevelInfo with the default SimpleLogger
//goland:noinspection GoUnusedExportedFunction
func Info(args ...interface{}) {
Default().Info(args...)
func Info(v ...interface{}) {
Output(3, LevelInfo, v...)
}

// Infof logs on the LevelInfo with the default SimpleLogger
//goland:noinspection GoUnusedExportedFunction
func Infof(format string, args ...interface{}) {
Default().Infof(format, args...)
func Infof(format string, v ...interface{}) {
Outputf(3, LevelInfo, format, v...)
}

// Warn logs on the LevelWarn with the default SimpleLogger
//goland:noinspection GoUnusedExportedFunction
func Warn(args ...interface{}) {
Default().Warn(args...)
func Warn(v ...interface{}) {
Output(3, LevelWarn, v...)
}

// Warnf logs on the Level with the default SimpleLogger
//goland:noinspection GoUnusedExportedFunction
func Warnf(format string, args ...interface{}) {
Default().Warnf(format, args...)
func Warnf(format string, v ...interface{}) {
Outputf(3, LevelWarn, format, v...)
}

// Error logs on the LevelError with the default SimpleLogger
//goland:noinspection GoUnusedExportedFunction
func Error(args ...interface{}) {
Default().Error(args...)
func Error(v ...interface{}) {
Output(3, LevelError, v...)
}

// Errorf logs on the LevelError with the default SimpleLogger
//goland:noinspection GoUnusedExportedFunction
func Errorf(format string, args ...interface{}) {
Default().Errorf(format, args...)
func Errorf(format string, v ...interface{}) {
Outputf(3, LevelError, format, v...)
}

// Fatal logs on the LevelFatal with the default SimpleLogger
//goland:noinspection GoUnusedExportedFunction
func Fatal(args ...interface{}) {
Default().Fatal(args...)
func Fatal(v ...interface{}) {
Output(3, LevelFatal, v...)
}

// Fatalf logs on the LevelFatal with the default SimpleLogger
//goland:noinspection GoUnusedExportedFunction
func Fatalf(format string, args ...interface{}) {
Default().Fatalf(format, args...)
func Fatalf(format string, v ...interface{}) {
Outputf(3, LevelFatal, format, v...)
}

// Panic logs on the LevelPanic with the default SimpleLogger
//goland:noinspection GoUnusedExportedFunction
func Panic(args ...interface{}) {
Default().Panic(args...)
func Panic(v ...interface{}) {
Output(3, LevelPanic, v...)
}

// Panicf logs on the LevelPanic with the default SimpleLogger
//goland:noinspection GoUnusedExportedFunction
func Panicf(format string, args ...interface{}) {
Default().Panicf(format, args...)
func Panicf(format string, v ...interface{}) {
Outputf(3, LevelPanic, format, v...)
}

func Output(calldepth int, level Level, v ...interface{}) {
std.Output(calldepth+1, level, v...)
}

func Outputf(calldepth int, level Level, format string, v ...interface{}) {
std.Outputf(calldepth+1, level, format, v...)
}

0 comments on commit 3de0af3

Please sign in to comment.