You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.6 KiB

10 years ago
  1. package logger
  2. import (
  3. "os"
  4. "github.com/tendermint/log15"
  5. . "github.com/tendermint/tendermint/common"
  6. "github.com/tendermint/tendermint/config"
  7. )
  8. var rootHandler log15.Handler
  9. func init() {
  10. Reset()
  11. }
  12. // You might want to call this after resetting tendermint/config.
  13. func Reset() {
  14. handlers := []log15.Handler{}
  15. // stdout handler
  16. stdoutHandler := log15.LvlFilterHandler(
  17. getLevel(config.App().GetString("Log.Stdout.Level")),
  18. log15.StreamHandler(os.Stdout, log15.TerminalFormat()),
  19. )
  20. handlers = append(handlers, stdoutHandler)
  21. /*
  22. // Maybe also write to a file.
  23. if _logFileDir := config.App().GetString("Log.File.Dir"); _logFileDir != "" {
  24. // Create log dir if it doesn't exist
  25. err := os.MkdirAll(_logFileDir, 0700)
  26. if err != nil {
  27. fmt.Printf("Could not create directory: %v", err)
  28. os.Exit(1)
  29. }
  30. // File handler
  31. handlers = append(handlers, log15.LvlFilterHandler(
  32. getLevel(config.App().GetString("Log.File.Level")),
  33. log15.Must.FileHandler(_logFileDir+"/tendermint.log", log15.LogfmtFormat()),
  34. ))
  35. }
  36. */
  37. // Set rootHandler.
  38. //rootHandler = log15.MultiHandler(handlers...)
  39. rootHandler = stdoutHandler
  40. // By setting handlers on the root, we handle events from all loggers.
  41. log15.Root().SetHandler(rootHandler)
  42. }
  43. // See binary/log for an example of usage.
  44. func RootHandler() log15.Handler {
  45. return rootHandler
  46. }
  47. func New(ctx ...interface{}) log15.Logger {
  48. return log15.Root().New(ctx...)
  49. }
  50. func getLevel(lvlString string) log15.Lvl {
  51. lvl, err := log15.LvlFromString(lvlString)
  52. if err != nil {
  53. Exit(Fmt("Invalid log level %v: %v", lvlString, err))
  54. }
  55. return lvl
  56. }