Add log
This commit is contained in:
parent
47c2d3b2ec
commit
6b17bec30c
42
logging/logging.go
Normal file
42
logging/logging.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
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!!!")
|
||||||
|
}
|
6
main.go
6
main.go
@ -44,7 +44,8 @@ import (
|
|||||||
// "git.sangeeth.dev/gobyexample/embed"
|
// "git.sangeeth.dev/gobyexample/embed"
|
||||||
// "git.sangeeth.dev/gobyexample/cli"
|
// "git.sangeeth.dev/gobyexample/cli"
|
||||||
// "git.sangeeth.dev/gobyexample/subcmd"
|
// "git.sangeeth.dev/gobyexample/subcmd"
|
||||||
"git.sangeeth.dev/gobyexample/env"
|
// "git.sangeeth.dev/gobyexample/env"
|
||||||
|
"git.sangeeth.dev/gobyexample/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -91,5 +92,6 @@ func main() {
|
|||||||
// embed.Embed()
|
// embed.Embed()
|
||||||
// cli.Cli()
|
// cli.Cli()
|
||||||
// subcmd.SubCmd()
|
// subcmd.SubCmd()
|
||||||
env.Env()
|
// env.Env()
|
||||||
|
logging.Logging()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user