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.

65 lines
1.6 KiB

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