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

Commit

Permalink
added default logger coloring
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Aug 3, 2021
1 parent 134de93 commit 387f22b
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 8 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# log


[![Go Reference](https://pkg.go.dev/badge/github.com/DisgoOrg/log.svg)](https://pkg.go.dev/github.com/DisgoOrg/log)
[![Go Report](https://goreportcard.com/badge/github.com/DisgoOrg/log)](https://goreportcard.com/report/github.com/DisgoOrg/log)
[![Go Version](https://img.shields.io/github/go-mod/go-version/DisgoOrg/log)](https://golang.org/doc/devel/release.html)
Expand All @@ -12,11 +11,11 @@ The `Logger` interface can be used instead to give the user choice over which lo

This lib ships with a default implementation of the `Logger` interface

[SimpleLogger](https://github.com/DisgoOrg/log/blob/master/simple_logger.go) is a wrapped standard [Logger](https://pkg.go.dev/log) to fit the `Logger` interface
[SimpleLogger](https://github.com/DisgoOrg/log/blob/master/simple_logger.go) is a wrapped
standard [Logger](https://pkg.go.dev/log) to fit the `Logger` interface

You can use your own implementation or a library like [logrus](https://github.com/sirupsen/logrus)


### Installing

```sh
Expand Down
21 changes: 21 additions & 0 deletions color_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package log

import (
"os"
"runtime"
"syscall"
)

// based off https://github.com/TwinProduction/go-color/blob/master/color_windows.go
func init() {
if runtime.GOOS == "windows" {
// Try to make ANSI work
handle := syscall.Handle(os.Stdout.Fd())
kernel32DLL := syscall.NewLazyDLL("kernel32.dll")
setConsoleModeProc := kernel32DLL.NewProc("SetConsoleMode")
// If it fails, fallback to no styles
if _, _, err := setConsoleModeProc.Call(uintptr(handle), 0x0001|0x0002|0x0004); err != nil && err.Error() != "The operation completed successfully." {
enableColors = false
}
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/DisgoOrg/log

go 1.16
go 1.16
Empty file added go.sum
Empty file.
82 changes: 78 additions & 4 deletions simple_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var std *SimpleLogger = nil

// These flags define which text to prefix to each log entry generated by the Logger.
// Bits are or'ed together to control what's printed.
// With the exception of the Lmsgprefix flag, there is no
// Except the Lmsgprefix flag, there is no
// control over the order they appear (the order listed here)
// or the format they present (as described in the comments).
// The prefix is followed by a colon only when Llongfile or Lshortfile
Expand All @@ -21,6 +21,7 @@ var std *SimpleLogger = nil
// 2009/01/23 01:23:23 message
// while flags Ldate | Ltime | Lmicroseconds | Llongfile produce,
// 2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message
//goland:noinspection GoUnusedConst
const (
Ldate = 1 << iota // the date in the local time zone: 2009/01/23
Ltime // the time in the local time zone: 01:23:23
Expand Down Expand Up @@ -65,16 +66,32 @@ func (l Level) String() string {
}
}

var (
enableColors = true
prefixStyle = ForegroundColorBrightBlack
levelStyle = StyleBold
textStyle = ForegroundColorWhite
)

var styles = map[Level]Style{
LevelDebug: ForegroundColorWhite,
LevelInfo: ForegroundColorCyan,
LevelWarn: ForegroundColorYellow,
LevelError: ForegroundColorBrightRed,
LevelFatal: ForegroundColorRed,
LevelPanic: ForegroundColorMagenta,
}

//Default returns the default SimpleLogger
//goland:noinspection GoUnusedExportedFunction
func Default() *SimpleLogger {
if std == nil {
std = New(log.LstdFlags | log.Lmsgprefix)
std = New(log.LstdFlags)
}
return std
}

// New returns a new SimpleLogger implementation
// New returns a newInt SimpleLogger implementation
//goland:noinspection GoUnusedExportedFunction
func New(flags int) *SimpleLogger {
return &SimpleLogger{
Expand All @@ -87,6 +104,7 @@ func New(flags int) *SimpleLogger {
type SimpleLogger struct {
logger *log.Logger
level Level
prefix Style
}

// SetLevel sets the lowest Level to log for
Expand All @@ -103,7 +121,24 @@ func (l *SimpleLogger) log(level Level, args ...interface{}) {
if level < l.level {
return
}
l.logger.SetPrefix(level.String() + " ")

if l.prefix != prefixStyle {
l.prefix = prefixStyle
l.logger.SetPrefix(prefixStyle.String())
}

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

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

switch level {
case LevelFatal:
l.logger.Fatal(args...)
Expand All @@ -113,6 +148,7 @@ func (l *SimpleLogger) log(level Level, args ...interface{}) {
l.logger.Print(args...)
}
}

func (l *SimpleLogger) logf(level Level, format string, args ...interface{}) {
l.log(level, fmt.Sprintf(format, args...))
}
Expand Down Expand Up @@ -178,71 +214,109 @@ func (l *SimpleLogger) Panicf(format string, args ...interface{}) {
}

// SetLevel sets the Level of the default Logger
//goland:noinspection GoUnusedExportedFunction
func SetLevel(level Level) {
Default().SetLevel(level)
}

// SetLevelColor sets the Style of the given Level
//goland:noinspection GoUnusedExportedFunction
func SetLevelColor(level Level, color Style) {
styles[level] = color
}

// SetLevelStyle sets the default Style of all Level(s)
//goland:noinspection GoUnusedExportedFunction
func SetLevelStyle(style Style) {
levelStyle = style
}

// EnableColors enables/disables usage of Style(s) in all Logger(s)
//goland:noinspection GoUnusedExportedFunction
func EnableColors(enable bool) {
enableColors = enable
}

// SetTextColor sets the Style which is used for text of a log 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
//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...)
}

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

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

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

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

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

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

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

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

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

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

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

0 comments on commit 387f22b

Please sign in to comment.