go_log/main.go

180 lines
3.6 KiB
Go
Raw Permalink Normal View History

2022-10-15 23:22:06 +02:00
package go_log
2022-10-11 06:06:26 +02:00
import (
2022-10-16 00:22:29 +02:00
"fmt"
2022-10-11 06:06:26 +02:00
"os"
"time"
)
type LogLvl byte
const (
Error LogLvl = 1
Warn LogLvl = 2
Info LogLvl = 3
Verbose LogLvl = 4
Debug LogLvl = 5
)
type FmtOpts struct {
2022-10-16 00:22:29 +02:00
Context []interface{}
2022-10-11 06:06:26 +02:00
LogLvlName string
2022-10-16 00:22:29 +02:00
Parts []interface{}
2022-10-11 06:06:26 +02:00
Timestamp time.Time
}
type Fmt func(FmtOpts) string
type Std func(string)
type Log struct {
2022-10-16 00:22:29 +02:00
Context []interface{} // Will be prepended to metadata on all log entries
2022-10-11 07:30:46 +02:00
MinLogLvl LogLvl // Minimal log level to output
Fmt Fmt // Log message formatter
Stderr Std // Log message outputter for Debug, Verbose and Info
Stdout Std // Log message outputter for Warning and Error
TimeLocation *time.Location // Timestamp location/time zone setting
2022-10-11 06:06:26 +02:00
}
func LogLvlFromStr(logLvl string) LogLvl {
if logLvl == "Error" {
return Error
} else if logLvl == "Warn" {
return Warn
} else if logLvl == "Info" {
return Info
} else if logLvl == "Verbose" {
return Verbose
} else if logLvl == "Debug" {
return Debug
} else {
return 0
}
}
2022-10-11 06:06:26 +02:00
func LogName(logLvl LogLvl) string {
if logLvl == 0 {
return "Error"
}
if logLvl == 1 {
return "Warn"
}
if logLvl == 2 {
return "Info"
}
if logLvl == 3 {
return "Verbose"
}
if logLvl == 4 {
return "Debug"
}
return "Invalid"
}
func LogNameShort(logLvl LogLvl) string {
if logLvl == 1 {
return "\033[31mErr\033[0m"
}
if logLvl == 2 {
return "\033[33mWrn\033[0m"
}
if logLvl == 3 {
return "\033[32mInf\033[0m"
}
if logLvl == 4 {
return "\033[34mVer\033[0m"
}
if logLvl == 5 {
return "\033[37mDeb\033[0m"
}
return "Invalid"
}
func DefaultFmt(opts FmtOpts) string {
output := opts.Timestamp.Format("2006-01-02 15:04:05")
2022-10-16 00:22:29 +02:00
output += " [" + opts.LogLvlName + "] " + fmt.Sprintf("%v", opts.Parts[0])
2022-10-11 06:06:26 +02:00
2022-10-16 00:22:29 +02:00
for i := 0; i < len(opts.Context); i = i + 2 {
output += " " + fmt.Sprintf("%v", opts.Context[i]) + ": " + fmt.Sprintf("%v", opts.Context[i+1])
}
for i := 1; i < len(opts.Parts); i = i + 2 {
output += " " + fmt.Sprintf("%v", opts.Parts[i]) + ": " + fmt.Sprintf("%v", opts.Parts[i+1])
2022-10-11 06:06:26 +02:00
}
return output + "\n"
}
func DefaultStderr(msg string) {
os.Stderr.WriteString(msg)
}
func DefaultStdout(msg string) {
os.Stdout.WriteString(msg)
}
func GetLog() Log {
log := Log{
Fmt: DefaultFmt,
MinLogLvl: Info,
Stderr: DefaultStderr,
Stdout: DefaultStdout,
2022-10-11 06:06:26 +02:00
}
log.TimeLocation, _ = time.LoadLocation("UTC")
return log
2022-10-11 06:06:26 +02:00
}
2022-10-16 00:22:29 +02:00
func (log *Log) Error(parts ...interface{}) {
2022-10-11 06:06:26 +02:00
if log.MinLogLvl >= Error {
log.Stderr(log.Fmt(FmtOpts{
2022-10-16 00:22:29 +02:00
Context: log.Context,
2022-10-11 06:06:26 +02:00
LogLvlName: LogNameShort(Error),
2022-10-16 00:22:29 +02:00
Parts: parts,
2022-10-11 06:06:26 +02:00
Timestamp: time.Now().In(log.TimeLocation),
}))
}
}
2022-10-16 00:22:29 +02:00
func (log *Log) Warn(parts ...interface{}) {
2022-10-11 06:06:26 +02:00
if log.MinLogLvl >= Warn {
log.Stderr(log.Fmt(FmtOpts{
2022-10-16 00:22:29 +02:00
Context: log.Context,
2022-10-11 06:06:26 +02:00
Timestamp: time.Now().In(log.TimeLocation),
LogLvlName: LogNameShort(Warn),
2022-10-16 00:22:29 +02:00
Parts: parts,
2022-10-11 06:06:26 +02:00
}))
}
}
2022-10-16 00:22:29 +02:00
func (log *Log) Info(parts ...interface{}) {
2022-10-11 06:06:26 +02:00
if log.MinLogLvl >= Info {
log.Stdout(log.Fmt(FmtOpts{
2022-10-16 00:22:29 +02:00
Context: log.Context,
2022-10-11 06:06:26 +02:00
Timestamp: time.Now().In(log.TimeLocation),
LogLvlName: LogNameShort(Info),
2022-10-16 00:22:29 +02:00
Parts: parts,
2022-10-11 06:06:26 +02:00
}))
}
}
2022-10-16 00:22:29 +02:00
func (log *Log) Verbose(parts ...interface{}) {
2022-10-11 06:06:26 +02:00
if log.MinLogLvl >= Verbose {
log.Stdout(log.Fmt(FmtOpts{
2022-10-16 00:22:29 +02:00
Context: log.Context,
2022-10-11 06:06:26 +02:00
Timestamp: time.Now().In(log.TimeLocation),
LogLvlName: LogNameShort(Verbose),
2022-10-16 00:22:29 +02:00
Parts: parts,
2022-10-11 06:06:26 +02:00
}))
}
}
2022-10-16 00:22:29 +02:00
func (log *Log) Debug(parts ...interface{}) {
2022-10-11 06:06:26 +02:00
if log.MinLogLvl >= Debug {
log.Stdout(log.Fmt(FmtOpts{
2022-10-16 00:22:29 +02:00
Context: log.Context,
2022-10-11 06:06:26 +02:00
Timestamp: time.Now().In(log.TimeLocation),
LogLvlName: LogNameShort(Debug),
2022-10-16 00:22:29 +02:00
Parts: parts,
2022-10-11 06:06:26 +02:00
}))
}
}