Log System Implementation #1
1
log.go
1
log.go
@ -223,6 +223,7 @@ func getRandomPrefix(level StoneLevel) string {
|
|||||||
WARN: stoneWarnPrefixes,
|
WARN: stoneWarnPrefixes,
|
||||||
ERROR: stoneErrorPrefixes,
|
ERROR: stoneErrorPrefixes,
|
||||||
FATAL: stonePanicPrefixes,
|
FATAL: stonePanicPrefixes,
|
||||||
|
PANIC: stonePanicPrefixes,
|
||||||
}
|
}
|
||||||
|
|
||||||
prefixes := prefixMap[level]
|
prefixes := prefixMap[level]
|
||||||
|
47
log_test.go
47
log_test.go
@ -1,6 +1,8 @@
|
|||||||
package stonelog
|
package stonelog
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"os/exec"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -46,6 +48,17 @@ func Test_LogLevelColor(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_JSONLogs(t *testing.T) {
|
||||||
|
InitStoneLog(TRACE, true, false)
|
||||||
|
Trace("This is a trace log")
|
||||||
|
Debug("This is a debug log")
|
||||||
|
Observation("This is an observation log")
|
||||||
|
Hypothesis("This is a hypothesis log")
|
||||||
|
Failure("This is a failure log")
|
||||||
|
|
||||||
|
SetLogLevel(INFO)
|
||||||
|
}
|
||||||
|
|
||||||
func Test_PlainTextLogs(t *testing.T) {
|
func Test_PlainTextLogs(t *testing.T) {
|
||||||
InitStoneLog(TRACE, false, false)
|
InitStoneLog(TRACE, false, false)
|
||||||
Trace("This is a trace log")
|
Trace("This is a trace log")
|
||||||
@ -73,13 +86,29 @@ func Test_MutedPlainTextLogs(t *testing.T) {
|
|||||||
Failure("This is a failure log")
|
Failure("This is a failure log")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_JSONLogs(t *testing.T) {
|
func Test_PanicPlainTextLogs(t *testing.T) {
|
||||||
InitStoneLog(TRACE, true, false)
|
defer func() {
|
||||||
Trace("This is a trace log")
|
if r := recover(); r != nil {
|
||||||
Debug("This is a debug log")
|
Observation("Recovered from panic: %v", r)
|
||||||
Observation("This is an observation log")
|
}
|
||||||
Hypothesis("This is a hypothesis log")
|
}()
|
||||||
Failure("This is a failure log")
|
|
||||||
|
InitStoneLog(TRACE, false, true)
|
||||||
SetLogLevel(INFO)
|
Panic("This is a panic log")
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_FatalPlainTextLogs(t *testing.T) {
|
||||||
|
cmd := exec.Command(
|
||||||
|
"go", "run", "./test/main.go",
|
||||||
|
)
|
||||||
|
cmd.Env = append(cmd.Env, "TEST_FATAL=1")
|
||||||
|
|
||||||
|
err := cmd.Run()
|
||||||
|
|
||||||
|
var exitErr *exec.ExitError
|
||||||
|
if errors.As(err, &exitErr) {
|
||||||
|
if exitErr.ExitCode() != 1 {
|
||||||
|
t.Errorf("Expected exit code 1, got %d", exitErr.ExitCode())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
14
tests/main.go
Normal file
14
tests/main.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"gitstormr.dev/stone-utils/stonelog"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if os.Getenv("TEST_FATAL") == "1" {
|
||||||
|
stonelog.InitStoneLog(stonelog.TRACE, false, false)
|
||||||
|
stonelog.Meltdown("A fatal error occurred")
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user