main.go 868 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // webLogger shows an example usage of logging.GetHTTPHandler
  2. // it exposes log events over websocket using my wslog package
  3. package main
  4. import (
  5. "net/http"
  6. "os"
  7. "time"
  8. "github.com/cryptix/go/logging/wslog"
  9. "github.com/op/go-logging"
  10. )
  11. var l = logging.MustGetLogger("example")
  12. func main() {
  13. i := 0
  14. stderrbackend := logging.NewLogBackend(os.Stderr, "stderr:", 0)
  15. wsbackend := wslog.NewBackend()
  16. var format = logging.MustStringFormatter(
  17. "%{longfile} ▶ %{id:03x} %{message}",
  18. )
  19. wsFormatter := logging.NewBackendFormatter(wsbackend, format)
  20. logging.SetBackend(stderrbackend, wsFormatter)
  21. go func() {
  22. for {
  23. l.Warning("Logging event %d", i)
  24. l.Debug("Hello")
  25. i++
  26. time.Sleep(time.Second)
  27. }
  28. }()
  29. http.Handle("/log", wsbackend)
  30. check(http.ListenAndServe(":8080", nil))
  31. }
  32. func check(err error) {
  33. if err != nil {
  34. panic(err)
  35. }
  36. }