-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Logging): improved logger prefixes
- Loading branch information
Showing
26 changed files
with
111 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package twitch | ||
|
||
import ( | ||
"cake4everybot/logger" | ||
"cake4everybot/tools/streamelements" | ||
) | ||
|
||
var ( | ||
log *logger.Logger = logger.New("Event/Twitch") | ||
se *streamelements.Streamelements | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package youtube | ||
|
||
import ( | ||
"cake4everybot/logger" | ||
) | ||
|
||
var log = logger.New("Event/YouTube") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package logger | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"sync" | ||
) | ||
|
||
// Logger is a wrapper around the standard [log.Logger] that adds a prefix to each | ||
// loger instance. | ||
type Logger struct { | ||
*log.Logger | ||
} | ||
|
||
const ( | ||
prefixBrackets = "[%s]" | ||
prefixFormat = "% -*s | " | ||
prefixBracketsLength = len(prefixBrackets) - 2 | ||
) | ||
|
||
var ( | ||
loggers = make(map[string]*Logger) | ||
loggersMux sync.Mutex | ||
prefixLength int | ||
) | ||
|
||
// New creates a new logger with the given prefix that writes to the standard | ||
// logger destination. | ||
func New(prefix string) *Logger { | ||
loggersMux.Lock() | ||
defer loggersMux.Unlock() | ||
|
||
if l, ok := loggers[prefix]; ok { | ||
return l | ||
} | ||
|
||
if len(prefix) > prefixLength { | ||
prefixLength = len(prefix) | ||
updatePrefixes() | ||
} | ||
|
||
loggers[prefix] = &Logger{log.New( | ||
log.Writer(), | ||
getPrefix(prefix), | ||
log.LstdFlags|log.Lmsgprefix, | ||
)} | ||
return loggers[prefix] | ||
} | ||
|
||
func updatePrefixes() { | ||
for prefix, l := range loggers { | ||
l.SetPrefix(getPrefix(prefix)) | ||
} | ||
} | ||
|
||
func getPrefix(prefix string) string { | ||
prefix = fmt.Sprintf(prefixBrackets, prefix) | ||
return fmt.Sprintf(prefixFormat, prefixBracketsLength+prefixLength, prefix) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,6 @@ | |
package birthday | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/bwmarrin/discordgo" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,6 @@ import ( | |
"cake4everybot/data/lang" | ||
"cake4everybot/util" | ||
"fmt" | ||
"log" | ||
"strconv" | ||
"time" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.