log.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package logger
  17. import (
  18. "fmt"
  19. "io"
  20. "log"
  21. "os"
  22. "github.com/ethereum/go-ethereum/common"
  23. )
  24. func openLogFile(datadir string, filename string) *os.File {
  25. path := common.AbsolutePath(datadir, filename)
  26. file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
  27. if err != nil {
  28. panic(fmt.Sprintf("error opening log file '%s': %v", filename, err))
  29. }
  30. return file
  31. }
  32. func New(datadir string, logFile string, logLevel int) LogSystem {
  33. var writer io.Writer
  34. if logFile == "" {
  35. writer = os.Stdout
  36. } else {
  37. writer = openLogFile(datadir, logFile)
  38. }
  39. var sys LogSystem
  40. sys = NewStdLogSystem(writer, log.LstdFlags, LogLevel(logLevel))
  41. AddLogSystem(sys)
  42. return sys
  43. }
  44. func NewJSONsystem(datadir string, logFile string) LogSystem {
  45. var writer io.Writer
  46. if logFile == "-" {
  47. writer = os.Stdout
  48. } else {
  49. writer = openLogFile(datadir, logFile)
  50. }
  51. var sys LogSystem
  52. sys = NewJsonLogSystem(writer)
  53. AddLogSystem(sys)
  54. return sys
  55. }