Compare commits

...

2 Commits

Author SHA1 Message Date
6b17bec30c
Add log 2024-05-04 21:19:11 +05:30
47c2d3b2ec
Add env 2024-05-04 21:07:59 +05:30
3 changed files with 69 additions and 2 deletions

21
env/env.go vendored Normal file
View File

@ -0,0 +1,21 @@
package env
import (
"fmt"
"os"
"strings"
)
func Env() {
os.Setenv("GOBYEXAMPLE", "WORKS")
fmt.Println("$GOBYEXAMPLE=", os.Getenv("GOBYEXAMPLE"))
envGoRoot := os.Getenv("GOROOT")
fmt.Println("$GOROOT=", envGoRoot)
fmt.Println()
fmt.Println("All env vars:")
for _, envLine := range os.Environ() {
pair := strings.SplitN(envLine, "=", 2)
fmt.Printf("%s=%s\n", pair[0], pair[1])
}
}

42
logging/logging.go Normal file
View 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!!!")
}

View File

@ -43,7 +43,9 @@ import (
// "git.sangeeth.dev/gobyexample/tmp"
// "git.sangeeth.dev/gobyexample/embed"
// "git.sangeeth.dev/gobyexample/cli"
"git.sangeeth.dev/gobyexample/subcmd"
// "git.sangeeth.dev/gobyexample/subcmd"
// "git.sangeeth.dev/gobyexample/env"
"git.sangeeth.dev/gobyexample/logging"
)
func main() {
@ -89,5 +91,7 @@ func main() {
// tmp.Tmp()
// embed.Embed()
// cli.Cli()
subcmd.SubCmd()
// subcmd.SubCmd()
// env.Env()
logging.Logging()
}