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.

64 lines
1.5 KiB

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