-
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.
- Loading branch information
Showing
6 changed files
with
208 additions
and
70 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/lvlcn-t/loggerhead/logger" | ||
) | ||
|
||
func main() { | ||
err := os.Setenv("LOG_FORMAT", "text") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
log := logger.NewLogger() | ||
|
||
log.Debug("This is a debug message!") | ||
log.Debugf("This is a %s message!", "debug") | ||
|
||
log.Info("Hello, world!") | ||
log.Infof("Hello, %s!", "world") | ||
|
||
log.Warn("This is a warning!") | ||
log.Warnf("This is a %s!", "warning") | ||
|
||
log.Error("This is an error!") | ||
log.Errorf("This is an %s!", "error") | ||
|
||
func() { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
log.Fatal("This is a fatal error!") | ||
} | ||
}() | ||
log.Panic("This is a panic!") | ||
}() | ||
} |
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,60 @@ | ||
package logger | ||
|
||
import ( | ||
"log/slog" | ||
"strings" | ||
) | ||
|
||
// Level is the type of log levels. | ||
type Level = slog.Level | ||
|
||
const ( | ||
LevelTrace Level = slog.Level(-8) | ||
LevelDebug Level = slog.LevelDebug | ||
LevelInfo Level = slog.LevelInfo | ||
LevelNotice Level = slog.Level(2) | ||
LevelWarn Level = slog.LevelWarn | ||
LevelError Level = slog.LevelError | ||
LevelPanic Level = slog.Level(12) | ||
LevelFatal Level = slog.Level(16) | ||
) | ||
|
||
var LevelNames = map[Level]string{ | ||
LevelTrace: "TRACE", | ||
LevelDebug: "DEBUG", | ||
LevelInfo: "INFO", | ||
LevelNotice: "NOTICE", | ||
LevelWarn: "WARN", | ||
LevelError: "ERROR", | ||
LevelPanic: "PANIC", | ||
LevelFatal: "FATAL", | ||
} | ||
|
||
// getLevel returns the integer value of the given level string. | ||
// If the level is not recognized, it returns LevelInfo. | ||
func getLevel(level string) int { | ||
switch strings.ToUpper(level) { | ||
case "TRACE": | ||
return int(LevelTrace) | ||
case "DEBUG": | ||
return int(LevelDebug) | ||
case "INFO": | ||
return int(LevelInfo) | ||
case "NOTICE": | ||
return int(LevelNotice) | ||
case "WARN", "WARNING": | ||
return int(LevelWarn) | ||
case "ERROR": | ||
return int(LevelError) | ||
default: | ||
return int(LevelInfo) | ||
} | ||
} | ||
|
||
// getLevelString returns the string value of the given level. | ||
func getLevelString(level Level) string { | ||
if s, ok := LevelNames[level]; ok { | ||
return s | ||
} | ||
return "UNKNOWN" | ||
} |
File renamed without changes.
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