initial commit
This commit is contained in:
34
format.go
Normal file
34
format.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package logging
|
||||
|
||||
import "fmt"
|
||||
|
||||
type LogFormat int
|
||||
const (
|
||||
FormatText LogFormat = iota
|
||||
FormatJson
|
||||
)
|
||||
|
||||
var logFormat = FormatText
|
||||
|
||||
var formatString = map[LogFormat]string {
|
||||
FormatText: "text",
|
||||
FormatJson: "json",
|
||||
}
|
||||
|
||||
func (f LogFormat) String() string {
|
||||
str, _ := formatString[f]
|
||||
return str
|
||||
}
|
||||
|
||||
func SetLogFormat(format LogFormat) error {
|
||||
_, exist := formatString[format]
|
||||
if ! exist {
|
||||
return fmt.Errorf("Logging : ERROR : No such log format (%d)", format)
|
||||
}
|
||||
|
||||
logFormat = format
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
75
level.go
Normal file
75
level.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type LogLevel int
|
||||
const (
|
||||
LevelDebug LogLevel = iota
|
||||
LevelInfo
|
||||
LevelWarn
|
||||
LevelError
|
||||
LevelFatal
|
||||
)
|
||||
|
||||
func (l LogLevel) String() string {
|
||||
str, _ := levelString[l]
|
||||
return str
|
||||
}
|
||||
|
||||
func (l LogLevel) MarshalJSON() ([]byte, error) {
|
||||
return []byte(`"` + l.String() + `"`), nil
|
||||
}
|
||||
|
||||
func (l *LogLevel) UnmarshalJSON(data []byte) error {
|
||||
level_string := strings.Trim(string(data), `"`)
|
||||
for key, val := range levelString {
|
||||
if val == level_string {
|
||||
*l = key
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("Logging : ERROR : LogLevel(%s) is not a valid level", level_string)
|
||||
}
|
||||
|
||||
var levelString = map[LogLevel]string {
|
||||
LevelDebug: "DEBUG",
|
||||
LevelInfo: "INFO",
|
||||
LevelWarn: "WARN",
|
||||
LevelError: "ERROR",
|
||||
LevelFatal: "FATAL",
|
||||
}
|
||||
|
||||
var minLevel = LevelInfo
|
||||
var maxLevel = LevelFatal
|
||||
|
||||
func SetMaxLogLevel(level LogLevel) error {
|
||||
|
||||
if _, exist := levelString[level]; ! exist {
|
||||
return fmt.Errorf("Logger : ERROR : Level with value %d does not exist", level)
|
||||
}
|
||||
|
||||
if level < minLevel {
|
||||
return fmt.Errorf("Logger : ERROR : Cannot set your Max LogLevel (%s) to be lower than the current Min (%s)", level, minLevel)
|
||||
}
|
||||
maxLevel = level
|
||||
return nil
|
||||
}
|
||||
|
||||
func SetMinLogLevel(level LogLevel) error {
|
||||
if _, exist := levelString[level]; ! exist {
|
||||
return fmt.Errorf("Logger : ERROR : Level with value %d does not exist", level)
|
||||
}
|
||||
|
||||
if level > maxLevel {
|
||||
return fmt.Errorf("Logger : ERROR : Cannot set your Min LogLevel (%s) to be greater than the current Max (%s)", level, maxLevel)
|
||||
}
|
||||
|
||||
minLevel = level
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
57
log.go
Normal file
57
log.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
type Log struct {
|
||||
Time time.Time `json:"time"`
|
||||
Level LogLevel `json:"level"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
|
||||
func valid_to_log(level LogLevel) bool {
|
||||
return level <= maxLevel && level >= minLevel
|
||||
}
|
||||
|
||||
func log(log Log) error {
|
||||
if valid_to_log(log.Level) {
|
||||
switch logFormat {
|
||||
case FormatText:
|
||||
fmt.Printf("%s : %s : %s\n", log.Time.Format(time.DateTime), log.Level, log.Message)
|
||||
case FormatJson:
|
||||
data, err := json.Marshal(log)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(data))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Debug(format string, args ...any) error {
|
||||
return log(Log{ Time: time.Now(), Level: LevelDebug, Message: fmt.Sprintf(format, args...)})
|
||||
}
|
||||
|
||||
func Info(format string, args ...any) error {
|
||||
return log(Log{ Time: time.Now(), Level: LevelInfo, Message: fmt.Sprintf(format, args...)})
|
||||
}
|
||||
|
||||
func Warn(format string, args ...any) error {
|
||||
return log(Log{ Time: time.Now(), Level: LevelWarn, Message: fmt.Sprintf(format, args...)})
|
||||
}
|
||||
|
||||
func Error(format string, args ...any) error {
|
||||
return log(Log{ Time: time.Now(), Level: LevelError, Message: fmt.Sprintf(format, args...)})
|
||||
}
|
||||
|
||||
func Fatal(format string, args ...any) error {
|
||||
return log(Log{ Time: time.Now(), Level: LevelFatal, Message: fmt.Sprintf(format, args...)})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user