Files
kjol/l4g/terminal.go

72 lines
1.2 KiB
Go

package l4g
import (
"fmt"
"os"
"time"
)
type TerminalLogger struct{}
func NewTerminalLogger() *TerminalLogger {
return &TerminalLogger{}
}
func (t *TerminalLogger) Write(entry Entry) error {
timestamp := entry.Timestamp.Format(time.RFC3339)
var category string
switch entry.Category {
case CATEGORY_SYSTEM:
category = "SYSTEM"
case CATEGORY_ADMIN:
category = "ADMIN"
case CATEGORY_USER:
category = "USER"
case CATEGORY_ORG:
category = "ORG"
case CATEGORY_AUDIT:
category = "AUDIT"
case CATEGORY_AUTH:
category = "AUTH"
default:
category = "UNKNOWN"
}
var logType string
switch entry.LogType {
case TYPE_DEBUG:
logType = "DEBUG"
case TYPE_INFO:
logType = "INFO"
case TYPE_WARN:
logType = "WARN"
case TYPE_ERROR:
logType = "ERROR"
default:
logType = "UNKNOWN"
}
content := ""
if entry.Content != nil {
content = *entry.Content
}
fmt.Printf("[%s] %s/%s: %s\n", timestamp, category, logType, content)
if entry.StructuredContent != nil {
fmt.Printf("Details: %s\n", *entry.StructuredContent)
}
return nil
}
func (t *TerminalLogger) Fatal(entry Entry) error {
err := t.Write(entry)
if err != nil {
return err
}
os.Exit(1)
return nil
}