Product Promotion
0x5a.live
for different kinds of informations and explorations.
GitHub - heartwilltell/log: Simple leveled logging wrapper around standard log package
Simple leveled logging wrapper around standard log package - heartwilltell/log
Visit SiteGitHub - heartwilltell/log: Simple leveled logging wrapper around standard log package
Simple leveled logging wrapper around standard log package - heartwilltell/log
Powered by 0x5a.live ๐
log
- Simple leveled logging based on standard log package
Documentation
Benefits
- ๐ป Leveled logging
- ๐ Simple API
- ๐ค
fmt
friendly - ๐ Zero dependencies
- ๐ฎโ๐จ No global logger
- ๐ No structured logging bullshit
Installation
go get github.com/heartwilltell/log
Leveled logging
The StdLog
implements a simple interface:
// Logger formats the message according to standard format specifiers from the fmt package
// and writes the message to writer specified by the concrete interface implementation.
type Logger interface {
// Error formats and writes the error level message.
Error(format string, v ...any)
// Warning formats and writes the warning level message.
Warning(format string, v ...any)
// Info formats and writes the information level message.
Info(format string, v ...any)
// Debug formats and writes the debug level message.
Debug(format string, v ...any)
}
Usage
๐ The usage is pretty simple. Just create a logger instance and call any of leveled methods.
logger := log.New()
logger.Info("Listen on port: %d", 8080)
๐ Sets the logging level to debug
level.
logger := log.New(log.WithLevel(log.DBG))
๐ Parses string to level and creates logger with warning
level.
level, levelErr := log.ParseLevel("warning")
if levelErr != nil {
// handle error here
}
logger := log.New(log.WithLevel(level))
๐ Creates logger with different io.Writer
.
var w bytes.Buffer
logger := log.New(log.WithWriter(w))
๐ Disables the colorful output.
logger := log.New(log.WithNoColor())
๐ Sets the UTC time format.
logger := log.New(log.WithUTC())
๐ Enables printing the code line number.
// Short format:
// INF: 2022/07/08 11:22:30 server.go:111: message
logger := log.New(log.WithLineNum(log.ShortFmt))
OR
// Long format:
// INF: 2022/07/08 11:22:30 /Users/heartwilltell/Go/app/server.go:111: message
logger := log.New(log.WithLineNum(log.LongFmt))
๐ Sets the level mark at the end of log prefix.
logger := log.New(log.WithLevelAtPrefixEnd())
Will produce this ๐
// 2022/07/08 11:22:30 INF: message
Instead of this ๐
// INF: 2022/07/08 11:22:30: message
๐ Creates nop logger which implements log.Logger
interface.
logger := log.NewNopLog()
๐ก Useful for tests or places where logger should be disabled by default
License
GoLang Resources
are all listed below.
Made with โค๏ธ
to provide different kinds of informations and resources.