gobyexample/logging/logging.go
2024-05-04 21:19:11 +05:30

43 lines
1001 B
Go

package logging
import (
"bytes"
"fmt"
"log"
"log/slog"
"os"
)
func Logging() {
log.Println("Baby's first log!")
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
log.Println("Log with microseconds!")
log.SetFlags(log.LstdFlags | log.Lshortfile)
log.Println("Now with filename!")
mlog := log.New(os.Stdout, "[mlog] ", log.LstdFlags|log.Lshortfile)
mlog.Println("My own logger!")
mlog.SetPrefix("[mlog with SetPrefix] ")
mlog.Println("Changed prefix")
var logBuffer bytes.Buffer
buflog := log.New(&logBuffer, "[bytes] ", log.LstdFlags|log.Lshortfile)
buflog.Println("Writing to a bytes buffer")
buflog.Println("Writing more to the bytes buffer")
fmt.Println()
fmt.Println("Printing logs from bytes buffer below:")
fmt.Println(logBuffer.String())
jsonHandler := slog.NewJSONHandler(os.Stderr, nil)
jlog := slog.New(jsonHandler)
jlog.Info("First json log entry")
jlog.Info("kv args", "key1", 1, "key2", 2, "key3", true)
jlog.Warn("warning!!!")
jlog.Error("error!!!")
}