From 7f9c3354872e6fcc63c37abddb184e3e58790bc3 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 21 Jan 2015 10:04:11 -0600 Subject: Split into multiple files --- logger/loggers.go | 122 ---------------------------------------------------- logger/logsystem.go | 31 +++++++++++++ logger/sys.go | 99 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+), 122 deletions(-) create mode 100644 logger/logsystem.go create mode 100644 logger/sys.go diff --git a/logger/loggers.go b/logger/loggers.go index 1bf7bfa0e..de9dfc095 100644 --- a/logger/loggers.go +++ b/logger/loggers.go @@ -14,11 +14,7 @@ package logger import ( "fmt" - "io" - "log" "os" - "sync" - "sync/atomic" ) // LogSystem is implemented by log output devices. @@ -46,100 +42,6 @@ const ( DebugDetailLevel ) -var ( - logMessageC = make(chan message) - addSystemC = make(chan LogSystem) - flushC = make(chan chan struct{}) - resetC = make(chan chan struct{}) -) - -func init() { - go dispatchLoop() -} - -// each system can buffer this many messages before -// blocking incoming log messages. -const sysBufferSize = 500 - -func dispatchLoop() { - var ( - systems []LogSystem - systemIn []chan message - systemWG sync.WaitGroup - ) - bootSystem := func(sys LogSystem) { - in := make(chan message, sysBufferSize) - systemIn = append(systemIn, in) - systemWG.Add(1) - go sysLoop(sys, in, &systemWG) - } - - for { - select { - case msg := <-logMessageC: - for _, c := range systemIn { - c <- msg - } - - case sys := <-addSystemC: - systems = append(systems, sys) - bootSystem(sys) - - case waiter := <-resetC: - // reset means terminate all systems - for _, c := range systemIn { - close(c) - } - systems = nil - systemIn = nil - systemWG.Wait() - close(waiter) - - case waiter := <-flushC: - // flush means reboot all systems - for _, c := range systemIn { - close(c) - } - systemIn = nil - systemWG.Wait() - for _, sys := range systems { - bootSystem(sys) - } - close(waiter) - } - } -} - -func sysLoop(sys LogSystem, in <-chan message, wg *sync.WaitGroup) { - for msg := range in { - if sys.GetLogLevel() >= msg.level { - sys.LogPrint(msg.level, msg.msg) - } - } - wg.Done() -} - -// Reset removes all active log systems. -// It blocks until all current messages have been delivered. -func Reset() { - waiter := make(chan struct{}) - resetC <- waiter - <-waiter -} - -// Flush waits until all current log messages have been dispatched to -// the active log systems. -func Flush() { - waiter := make(chan struct{}) - flushC <- waiter - <-waiter -} - -// AddLogSystem starts printing messages to the given LogSystem. -func AddLogSystem(sys LogSystem) { - addSystemC <- sys -} - // A Logger prints messages prefixed by a given tag. It provides named // Printf and Println style methods for all loglevels. Each ethereum // component should have its own logger with a unique prefix. @@ -222,27 +124,3 @@ func (logger *Logger) Fatalf(format string, v ...interface{}) { Flush() os.Exit(0) } - -// NewStdLogSystem creates a LogSystem that prints to the given writer. -// The flag values are defined package log. -func NewStdLogSystem(writer io.Writer, flags int, level LogLevel) LogSystem { - logger := log.New(writer, "", flags) - return &stdLogSystem{logger, uint32(level)} -} - -type stdLogSystem struct { - logger *log.Logger - level uint32 -} - -func (t *stdLogSystem) LogPrint(level LogLevel, msg string) { - t.logger.Print(msg) -} - -func (t *stdLogSystem) SetLogLevel(i LogLevel) { - atomic.StoreUint32(&t.level, uint32(i)) -} - -func (t *stdLogSystem) GetLogLevel() LogLevel { - return LogLevel(atomic.LoadUint32(&t.level)) -} diff --git a/logger/logsystem.go b/logger/logsystem.go new file mode 100644 index 000000000..f154773ae --- /dev/null +++ b/logger/logsystem.go @@ -0,0 +1,31 @@ +package logger + +import ( + "io" + "log" + "sync/atomic" +) + +// NewStdLogSystem creates a LogSystem that prints to the given writer. +// The flag values are defined package log. +func NewStdLogSystem(writer io.Writer, flags int, level LogLevel) LogSystem { + logger := log.New(writer, "", flags) + return &stdLogSystem{logger, uint32(level)} +} + +type stdLogSystem struct { + logger *log.Logger + level uint32 +} + +func (t *stdLogSystem) LogPrint(level LogLevel, msg string) { + t.logger.Print(msg) +} + +func (t *stdLogSystem) SetLogLevel(i LogLevel) { + atomic.StoreUint32(&t.level, uint32(i)) +} + +func (t *stdLogSystem) GetLogLevel() LogLevel { + return LogLevel(atomic.LoadUint32(&t.level)) +} diff --git a/logger/sys.go b/logger/sys.go new file mode 100644 index 000000000..5b48241c2 --- /dev/null +++ b/logger/sys.go @@ -0,0 +1,99 @@ +package logger + +import ( + "sync" +) + +var ( + logMessageC = make(chan message) + addSystemC = make(chan LogSystem) + flushC = make(chan chan struct{}) + resetC = make(chan chan struct{}) +) + +func init() { + go dispatchLoop() +} + +// each system can buffer this many messages before +// blocking incoming log messages. +const sysBufferSize = 500 + +func dispatchLoop() { + var ( + systems []LogSystem + systemIn []chan message + systemWG sync.WaitGroup + ) + bootSystem := func(sys LogSystem) { + in := make(chan message, sysBufferSize) + systemIn = append(systemIn, in) + systemWG.Add(1) + go sysLoop(sys, in, &systemWG) + } + + for { + select { + case msg := <-logMessageC: + for _, c := range systemIn { + c <- msg + } + + case sys := <-addSystemC: + systems = append(systems, sys) + bootSystem(sys) + + case waiter := <-resetC: + // reset means terminate all systems + for _, c := range systemIn { + close(c) + } + systems = nil + systemIn = nil + systemWG.Wait() + close(waiter) + + case waiter := <-flushC: + // flush means reboot all systems + for _, c := range systemIn { + close(c) + } + systemIn = nil + systemWG.Wait() + for _, sys := range systems { + bootSystem(sys) + } + close(waiter) + } + } +} + +func sysLoop(sys LogSystem, in <-chan message, wg *sync.WaitGroup) { + for msg := range in { + if sys.GetLogLevel() >= msg.level { + sys.LogPrint(msg.level, msg.msg) + } + } + wg.Done() +} + +// Reset removes all active log systems. +// It blocks until all current messages have been delivered. +func Reset() { + waiter := make(chan struct{}) + resetC <- waiter + <-waiter +} + +// Flush waits until all current log messages have been dispatched to +// the active log systems. +func Flush() { + waiter := make(chan struct{}) + flushC <- waiter + <-waiter +} + +// AddLogSystem starts printing messages to the given LogSystem. +func AddLogSystem(sys LogSystem) { + addSystemC <- sys +} -- cgit v1.2.3 From acdc19d1b7b25d6ebd8457f423659f3d112a4a75 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 21 Jan 2015 10:16:15 -0600 Subject: Add rawLogSystem --- logger/logsystem.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/logger/logsystem.go b/logger/logsystem.go index f154773ae..3601e926c 100644 --- a/logger/logsystem.go +++ b/logger/logsystem.go @@ -29,3 +29,27 @@ func (t *stdLogSystem) SetLogLevel(i LogLevel) { func (t *stdLogSystem) GetLogLevel() LogLevel { return LogLevel(atomic.LoadUint32(&t.level)) } + +// NewRawLogSystem creates a LogSystem that prints to the given writer without +// adding extra information. Suitable for preformatted output +func NewRawLogSystem(writer io.Writer, flags int, level LogLevel) LogSystem { + logger := log.New(writer, "", 0) + return &rawLogSystem{logger, uint32(level)} +} + +type rawLogSystem struct { + logger *log.Logger + level uint32 +} + +func (t *rawLogSystem) LogPrint(level LogLevel, msg string) { + t.logger.Print(msg) +} + +func (t *rawLogSystem) SetLogLevel(i LogLevel) { + atomic.StoreUint32(&t.level, uint32(i)) +} + +func (t *rawLogSystem) GetLogLevel() LogLevel { + return LogLevel(atomic.LoadUint32(&t.level)) +} -- cgit v1.2.3 From bdf99e098126880c10dc2ef68623c47ec6f04537 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 21 Jan 2015 10:17:07 -0600 Subject: Add LogFormat flag --- cmd/ethereum/flags.go | 2 ++ cmd/ethereum/main.go | 1 + eth/backend.go | 3 ++- logger/log.go | 10 ++++++++-- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/cmd/ethereum/flags.go b/cmd/ethereum/flags.go index f829744dc..885cdfe5b 100644 --- a/cmd/ethereum/flags.go +++ b/cmd/ethereum/flags.go @@ -57,6 +57,7 @@ var ( ConfigFile string DebugFile string LogLevel int + LogFormat string Dump bool DumpHash string DumpNumber int @@ -110,6 +111,7 @@ func Init() { flag.StringVar(&ConfigFile, "conf", defaultConfigFile, "config file") flag.StringVar(&DebugFile, "debug", "", "debug file (no debugging if not set)") flag.IntVar(&LogLevel, "loglevel", int(logger.InfoLevel), "loglevel: 0-5: silent,error,warn,info,debug,debug detail)") + flag.StringVar(&LogFormat, "logformat", "std", "logformat: std,raw)") flag.BoolVar(&DiffTool, "difftool", false, "creates output for diff'ing. Sets LogLevel=0") flag.StringVar(&DiffType, "diff", "all", "sets the level of diff output [vm, all]. Has no effect if difftool=false") flag.BoolVar(&ShowGenesis, "genesis", false, "Dump the genesis block") diff --git a/cmd/ethereum/main.go b/cmd/ethereum/main.go index b816c678e..b7997d485 100644 --- a/cmd/ethereum/main.go +++ b/cmd/ethereum/main.go @@ -67,6 +67,7 @@ func main() { DataDir: Datadir, LogFile: LogFile, LogLevel: LogLevel, + LogFormat: LogFormat, Identifier: Identifier, MaxPeers: MaxPeer, Port: OutboundPort, diff --git a/eth/backend.go b/eth/backend.go index c3c7d1287..da75da051 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -29,6 +29,7 @@ type Config struct { DataDir string LogFile string LogLevel int + LogFormat string KeyRing string MaxPeers int @@ -80,7 +81,7 @@ type Ethereum struct { func New(config *Config) (*Ethereum, error) { // Boostrap database - logger := ethlogger.New(config.DataDir, config.LogFile, config.LogLevel) + logger := ethlogger.New(config.DataDir, config.LogFile, config.LogLevel, config.LogFormat) db, err := ethdb.NewLDBDatabase("blockchain") if err != nil { return nil, err diff --git a/logger/log.go b/logger/log.go index 53065f870..baa3dfaf2 100644 --- a/logger/log.go +++ b/logger/log.go @@ -18,7 +18,7 @@ func openLogFile(datadir string, filename string) *os.File { return file } -func New(datadir string, logFile string, logLevel int) LogSystem { +func New(datadir string, logFile string, logLevel int, logFormat string) LogSystem { var writer io.Writer if logFile == "" { writer = os.Stdout @@ -26,7 +26,13 @@ func New(datadir string, logFile string, logLevel int) LogSystem { writer = openLogFile(datadir, logFile) } - sys := NewStdLogSystem(writer, log.LstdFlags, LogLevel(logLevel)) + var sys LogSystem + switch logFormat { + case "raw": + sys = NewRawLogSystem(writer, 0, LogLevel(logLevel)) + default: + sys = NewStdLogSystem(writer, log.LstdFlags, LogLevel(logLevel)) + } AddLogSystem(sys) return sys -- cgit v1.2.3 From 41d80ba17bac2d6cfefb8a1c6206fe1ccee04d1d Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 21 Jan 2015 10:18:46 -0600 Subject: Add JsonLevel log level --- logger/loggers.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/logger/loggers.go b/logger/loggers.go index de9dfc095..497060fdb 100644 --- a/logger/loggers.go +++ b/logger/loggers.go @@ -30,7 +30,7 @@ type message struct { msg string } -type LogLevel uint8 +type LogLevel uint32 const ( // Standard log levels @@ -40,6 +40,7 @@ const ( InfoLevel DebugLevel DebugDetailLevel + JsonLevel = 1000 ) // A Logger prints messages prefixed by a given tag. It provides named -- cgit v1.2.3 From ed7d7b405e1de8010f2ddeecf2b3798e2f07ad31 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 21 Jan 2015 10:26:54 -0600 Subject: Split file cleanup --- logger/loggers.go | 13 ------------- logger/logsystem.go | 8 ++++++++ logger/sys.go | 5 +++++ 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/logger/loggers.go b/logger/loggers.go index 497060fdb..77d111974 100644 --- a/logger/loggers.go +++ b/logger/loggers.go @@ -17,19 +17,6 @@ import ( "os" ) -// LogSystem is implemented by log output devices. -// All methods can be called concurrently from multiple goroutines. -type LogSystem interface { - GetLogLevel() LogLevel - SetLogLevel(i LogLevel) - LogPrint(LogLevel, string) -} - -type message struct { - level LogLevel - msg string -} - type LogLevel uint32 const ( diff --git a/logger/logsystem.go b/logger/logsystem.go index 3601e926c..8458b938f 100644 --- a/logger/logsystem.go +++ b/logger/logsystem.go @@ -6,6 +6,14 @@ import ( "sync/atomic" ) +// LogSystem is implemented by log output devices. +// All methods can be called concurrently from multiple goroutines. +type LogSystem interface { + GetLogLevel() LogLevel + SetLogLevel(i LogLevel) + LogPrint(LogLevel, string) +} + // NewStdLogSystem creates a LogSystem that prints to the given writer. // The flag values are defined package log. func NewStdLogSystem(writer io.Writer, flags int, level LogLevel) LogSystem { diff --git a/logger/sys.go b/logger/sys.go index 5b48241c2..59b8582f9 100644 --- a/logger/sys.go +++ b/logger/sys.go @@ -4,6 +4,11 @@ import ( "sync" ) +type message struct { + level LogLevel + msg string +} + var ( logMessageC = make(chan message) addSystemC = make(chan LogSystem) -- cgit v1.2.3 From 1077109e1153cc4fb4eece59dd48cd9f640d0e0b Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 21 Jan 2015 10:57:29 -0600 Subject: Add JsonLogger type --- eth/backend.go | 9 +++++++++ logger/loggers.go | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/eth/backend.go b/eth/backend.go index da75da051..c16727e1c 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -44,6 +44,7 @@ type Config struct { } var logger = ethlogger.NewLogger("SERV") +var jsonlogger = ethlogger.NewJsonLogger() type Ethereum struct { // Channel for shutting down the ethereum @@ -221,6 +222,14 @@ func (s *Ethereum) MaxPeers() int { // Start the ethereum func (s *Ethereum) Start(seed bool) error { + evd := map[string]interface{}{ + "version_string": s.ClientIdentity().String(), + "guid": ethutil.Bytes2Hex(s.ClientIdentity().Pubkey()), + "level": "debug", + "coinbase": ethutil.Bytes2Hex(s.KeyManager().Address()), + "eth_version": ProtocolVersion, + } + jsonlogger.Log("starting", evd) err := s.net.Start() if err != nil { return err diff --git a/logger/loggers.go b/logger/loggers.go index 77d111974..053f120be 100644 --- a/logger/loggers.go +++ b/logger/loggers.go @@ -13,8 +13,10 @@ logging of mutable state. package logger import ( + "encoding/json" "fmt" "os" + "time" ) type LogLevel uint32 @@ -112,3 +114,27 @@ func (logger *Logger) Fatalf(format string, v ...interface{}) { Flush() os.Exit(0) } + +type JsonLogger struct{} + +func NewJsonLogger() *JsonLogger { + return &JsonLogger{} +} + +func (logger *JsonLogger) Log(msgname string, dict map[string]interface{}) { + if _, ok := dict["ts"]; !ok { + dict["ts"] = time.Now().Local().Format(time.RFC3339Nano) + } + + // FIX + if _, ok := dict["level"]; !ok { + dict["level"] = "debug" + } + + obj := map[string]interface{}{ + msgname: dict, + } + + jsontxt, _ := json.Marshal(obj) + logMessageC <- message{JsonLevel, fmt.Sprintf("%s", jsontxt)} +} -- cgit v1.2.3 From 622bfd4f0815b9220f02e6b79f74ed1c77ec7649 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 21 Jan 2015 10:58:09 -0600 Subject: Check LogSystem type --- logger/sys.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/logger/sys.go b/logger/sys.go index 59b8582f9..bd826b587 100644 --- a/logger/sys.go +++ b/logger/sys.go @@ -75,8 +75,16 @@ func dispatchLoop() { func sysLoop(sys LogSystem, in <-chan message, wg *sync.WaitGroup) { for msg := range in { - if sys.GetLogLevel() >= msg.level { - sys.LogPrint(msg.level, msg.msg) + switch sys.(type) { + case *rawLogSystem: + // This is a semantic hack since rawLogSystem has little to do with JsonLevel + if msg.level == JsonLevel { + sys.LogPrint(msg.level, msg.msg) + } + default: + if sys.GetLogLevel() >= msg.level { + sys.LogPrint(msg.level, msg.msg) + } } } wg.Done() -- cgit v1.2.3 From 0aa76d3e5b3eb261103ebf5afd515394df0af914 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 21 Jan 2015 11:45:30 -0600 Subject: Rename jsonlogger method --- eth/backend.go | 2 +- logger/loggers.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/eth/backend.go b/eth/backend.go index c16727e1c..5057aa3aa 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -229,7 +229,7 @@ func (s *Ethereum) Start(seed bool) error { "coinbase": ethutil.Bytes2Hex(s.KeyManager().Address()), "eth_version": ProtocolVersion, } - jsonlogger.Log("starting", evd) + jsonlogger.LogJson("starting", evd) err := s.net.Start() if err != nil { return err diff --git a/logger/loggers.go b/logger/loggers.go index 053f120be..cd465ce87 100644 --- a/logger/loggers.go +++ b/logger/loggers.go @@ -121,7 +121,7 @@ func NewJsonLogger() *JsonLogger { return &JsonLogger{} } -func (logger *JsonLogger) Log(msgname string, dict map[string]interface{}) { +func (logger *JsonLogger) LogJson(msgname string, dict map[string]interface{}) { if _, ok := dict["ts"]; !ok { dict["ts"] = time.Now().Local().Format(time.RFC3339Nano) } -- cgit v1.2.3 From c14900dbb007af8aefc5f5583f3c17a613c66802 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 21 Jan 2015 11:47:21 -0600 Subject: Add initial P2P json logs --- p2p/server.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/p2p/server.go b/p2p/server.go index 4fd1f7d03..7dcbc9d11 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -8,6 +8,7 @@ import ( "sync" "time" + "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/logger" ) @@ -20,6 +21,8 @@ const ( var srvlog = logger.NewLogger("P2P Server") +var jsonlogger = logger.NewJsonLogger() + // Server manages all peer connections. // // The fields of Server are used as configuration parameters. @@ -353,9 +356,25 @@ func (srv *Server) dialLoop() { // connect to peer via dial out func (srv *Server) dialPeer(desc *peerAddr, slot int) { srvlog.Debugf("Dialing %v (slot %d)\n", desc, slot) + evd := map[string]interface{}{ + "remote_id": ethutil.Bytes2Hex(desc.Pubkey), + "remote_endpoint": desc.String(), + "level": "debug", + "guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()), + "num_connections": srv.PeerCount(), + } + jsonlogger.LogJson("p2p.connecting", evd) conn, err := srv.Dialer.Dial(desc.Network(), desc.String()) if err != nil { srvlog.DebugDetailf("dial error: %v", err) + evd := map[string]interface{}{ + "reason": "dial error", + "remote_id": desc.String(), + "level": "debug", + "guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()), + "num_connections": srv.PeerCount(), + } + jsonlogger.LogJson("p2p.disconnecting", evd) srv.peerSlots <- slot return } @@ -375,7 +394,17 @@ func (srv *Server) addPeer(conn net.Conn, desc *peerAddr, slot int) *Peer { peer.slot = slot srv.peers[slot] = peer srv.peerCount++ - go func() { peer.loop(); srv.peerDisconnect <- peer }() + go func() { + evd := map[string]interface{}{ + "guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()), + "num_connections": srv.PeerCount(), + "remote_id": desc.String(), + "level": "debug", + } + jsonlogger.LogJson("p2p.connected", evd) + peer.loop() + srv.peerDisconnect <- peer + }() return peer } @@ -393,13 +422,36 @@ func (srv *Server) removePeer(peer *Peer) { srv.peers[peer.slot] = nil // release slot to signal need for a new peer, last! srv.peerSlots <- peer.slot + evd := map[string]interface{}{ + "guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()), + "num_connections": srv.PeerCount(), + "remote_id": ethutil.Bytes2Hex(peer.Identity().Pubkey()), + "level": "debug", + } + jsonlogger.LogJson("p2p.disconnected", evd) } func (srv *Server) verifyPeer(addr *peerAddr) error { if srv.Blacklist.Exists(addr.Pubkey) { + evd := map[string]interface{}{ + "reason": "blacklisted", + "remote_id": addr.String(), + "level": "debug", + "guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()), + "num_connections": srv.PeerCount(), + } + jsonlogger.LogJson("p2p.disconnecting.reputation", evd) return errors.New("blacklisted") } if bytes.Equal(srv.Identity.Pubkey()[1:], addr.Pubkey) { + evd := map[string]interface{}{ + "reason": "not allowed to connect to srv", + "remote_id": addr.String(), + "level": "debug", + "guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()), + "num_connections": srv.PeerCount(), + } + jsonlogger.LogJson("p2p.disconnecting", evd) return newPeerError(errPubkeyForbidden, "not allowed to connect to srv") } srv.lock.RLock() @@ -408,6 +460,14 @@ func (srv *Server) verifyPeer(addr *peerAddr) error { if peer != nil { id := peer.Identity() if id != nil && bytes.Equal(id.Pubkey(), addr.Pubkey) { + evd := map[string]interface{}{ + "reason": "already connected", + "remote_id": addr.String(), + "level": "debug", + "guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()), + "num_connections": srv.PeerCount(), + } + jsonlogger.LogJson("p2p.disconnecting", evd) return errors.New("already connected") } } -- cgit v1.2.3 From d53e5646ecfce75790fea45a1ee552494ef88668 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 10 Feb 2015 19:21:13 +0100 Subject: Use strongly-typed objects --- eth/backend.go | 15 ++- logger/loggers.go | 17 +--- logger/types.go | 294 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ p2p/server.go | 55 ---------- 4 files changed, 305 insertions(+), 76 deletions(-) create mode 100644 logger/types.go diff --git a/eth/backend.go b/eth/backend.go index 5057aa3aa..cdccd3940 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -222,14 +222,13 @@ func (s *Ethereum) MaxPeers() int { // Start the ethereum func (s *Ethereum) Start(seed bool) error { - evd := map[string]interface{}{ - "version_string": s.ClientIdentity().String(), - "guid": ethutil.Bytes2Hex(s.ClientIdentity().Pubkey()), - "level": "debug", - "coinbase": ethutil.Bytes2Hex(s.KeyManager().Address()), - "eth_version": ProtocolVersion, - } - jsonlogger.LogJson("starting", evd) + jsonlogger.LogJson("starting", ðlogger.LogStarting{ + ClientString: s.ClientIdentity().String(), + Guid: ethutil.Bytes2Hex(s.ClientIdentity().Pubkey()), + Coinbase: ethutil.Bytes2Hex(s.KeyManager().Address()), + ProtocolVersion: ProtocolVersion, + }) + err := s.net.Start() if err != nil { return err diff --git a/logger/loggers.go b/logger/loggers.go index cd465ce87..36bc38116 100644 --- a/logger/loggers.go +++ b/logger/loggers.go @@ -16,7 +16,6 @@ import ( "encoding/json" "fmt" "os" - "time" ) type LogLevel uint32 @@ -121,20 +120,12 @@ func NewJsonLogger() *JsonLogger { return &JsonLogger{} } -func (logger *JsonLogger) LogJson(msgname string, dict map[string]interface{}) { - if _, ok := dict["ts"]; !ok { - dict["ts"] = time.Now().Local().Format(time.RFC3339Nano) - } - - // FIX - if _, ok := dict["level"]; !ok { - dict["level"] = "debug" - } - +func (logger *JsonLogger) LogJson(msgname string, v interface{}) { obj := map[string]interface{}{ - msgname: dict, + msgname: v, } jsontxt, _ := json.Marshal(obj) - logMessageC <- message{JsonLevel, fmt.Sprintf("%s", jsontxt)} + logMessageC <- message{JsonLevel, string(jsontxt)} + } diff --git a/logger/types.go b/logger/types.go new file mode 100644 index 000000000..ee53394f0 --- /dev/null +++ b/logger/types.go @@ -0,0 +1,294 @@ +package logger + +import ( + "time" +) + +type utctime8601 struct{} + +func (utctime8601) MarshalJSON() ([]byte, error) { + // FIX This should be re-formated for proper ISO 8601 + return []byte(`"` + time.Now().UTC().Format(time.RFC3339Nano)[:26] + `Z"`), nil +} + +//"starting" +type LogStarting struct { + ClientString string `json:"version_string"` + Guid string `json:"guid"` + Coinbase string `json:"coinbase"` + ProtocolVersion int `json:"eth_version"` + Ts utctime8601 `json:"ts"` +} + +//"p2p.connecting" +type P2PConnecting struct { + RemoteId string `json:"remote_id"` + RemoteEndpoint string `json:"remote_endpoint"` + Guid string `json:"guid"` + NumConnections int `json:"num_connections"` + Ts utctime8601 `json:"ts"` +} + +//"p2p.connected" +type P2PConnected struct { + Guid string `json:"guid"` + NumConnections int `json:"num_connections"` + RemoteId string `json:"remote_id"` + Ts utctime8601 `json:"ts"` +} + +//"p2p.handshaked" +type P2PHandshaked struct { + RemoteCapabilities []string `json:"remote_capabilities"` + RemoteId string `json:"remote_id"` + Guid string `json:"guid"` + NumConnections int `json:"num_connections"` + Ts string `json:"ts"` +} + +//"p2p.disconnected" +type P2PDisconnected struct { + Guid string `json:"guid"` + NumConnections int `json:"num_connections"` + RemoteId string `json:"remote_id"` + Ts utctime8601 `json:"ts"` +} + +//"p2p.disconnecting" +type P2PDisconnecting struct { + Reason string `json:"reason"` + RemoteId string `json:"remote_id"` + Guid string `json:"guid"` + NumConnections int `json:"num_connections"` + Ts utctime8601 `json:"ts"` +} + +//"p2p.disconnecting.bad_handshake" +type P2PDisconnectingBadHandshake struct { + Reason string `json:"reason"` + RemoteId string `json:"remote_id"` + Guid string `json:"guid"` + NumConnections int `json:"num_connections"` + Ts utctime8601 `json:"ts"` +} + +//"p2p.disconnecting.bad_protocol" +type P2PDisconnectingBadProtocol struct { + Reason string `json:"reason"` + RemoteId string `json:"remote_id"` + Guid string `json:"guid"` + NumConnections int `json:"num_connections"` + Ts utctime8601 `json:"ts"` +} + +//"p2p.disconnecting.reputation" +type P2PDisconnectingReputation struct { + Reason string `json:"reason"` + RemoteId string `json:"remote_id"` + Guid string `json:"guid"` + NumConnections int `json:"num_connections"` + Ts utctime8601 `json:"ts"` +} + +//"p2p.disconnecting.dht" +type P2PDisconnectingDHT struct { + Reason string `json:"reason"` + RemoteId string `json:"remote_id"` + Guid string `json:"guid"` + NumConnections int `json:"num_connections"` + Ts utctime8601 `json:"ts"` +} + +//"p2p.eth.disconnecting.bad_block" +type P2PEthDisconnectingBadBlock struct { + Reason string `json:"reason"` + RemoteId string `json:"remote_id"` + Guid string `json:"guid"` + NumConnections int `json:"num_connections"` + Ts utctime8601 `json:"ts"` +} + +//"p2p.eth.disconnecting.bad_tx" +type P2PEthDisconnectingBadTx struct { + Reason string `json:"reason"` + RemoteId string `json:"remote_id"` + Guid string `json:"guid"` + NumConnections int `json:"num_connections"` + Ts utctime8601 `json:"ts"` +} + +//"eth.newblock.mined" +type EthNewBlockMined struct { + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockHexRlp string `json:"block_hexrlp"` + BlockDifficulty int `json:"block_difficulty"` + Guid string `json:"guid"` + BlockPrevHash string `json:"block_prev_hash"` + Ts utctime8601 `json:"ts"` +} + +//"eth.newblock.broadcasted" +type EthNewBlockBroadcasted struct { + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + Guid string `json:"guid"` + BlockPrevHash string `json:"block_prev_hash"` + Ts utctime8601 `json:"ts"` +} + +//"eth.newblock.received" +type EthNewBlockReceived struct { + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + Guid string `json:"guid"` + BlockPrevHash string `json:"block_prev_hash"` + Ts utctime8601 `json:"ts"` +} + +//"eth.newblock.is_known" +type EthNewBlockIsKnown struct { + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + Guid string `json:"guid"` + BlockPrevHash string `json:"block_prev_hash"` + Ts utctime8601 `json:"ts"` +} + +//"eth.newblock.is_new" +type EthNewBlockIsNew struct { + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + Guid string `json:"guid"` + BlockPrevHash string `json:"block_prev_hash"` + Ts utctime8601 `json:"ts"` +} + +//"eth.newblock.missing_parent" +type EthNewBlockMissingParent struct { + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + Guid string `json:"guid"` + BlockPrevHash string `json:"block_prev_hash"` + Ts utctime8601 `json:"ts"` +} + +//"eth.newblock.is_invalid" +type EthNewBlockIsInvalid struct { + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + Guid string `json:"guid"` + BlockPrevHash string `json:"block_prev_hash"` + Ts utctime8601 `json:"ts"` +} + +//"eth.newblock.chain.is_older" +type EthNewBlockChainIsOlder struct { + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + Guid string `json:"guid"` + BlockPrevHash string `json:"block_prev_hash"` + Ts utctime8601 `json:"ts"` +} + +//"eth.newblock.chain.is_cannonical" +type EthNewBlockChainIsCanonical struct { + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + Guid string `json:"guid"` + BlockPrevHash string `json:"block_prev_hash"` + Ts utctime8601 `json:"ts"` +} + +//"eth.newblock.chain.not_cannonical" +type EthNewBlockChainNotCanonical struct { + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + Guid string `json:"guid"` + BlockPrevHash string `json:"block_prev_hash"` + Ts utctime8601 `json:"ts"` +} + +//"eth.newblock.chain.switched" +type EthNewBlockChainSwitched struct { + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + OldHeadHash string `json:"old_head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + Guid string `json:"guid"` + BlockPrevHash string `json:"block_prev_hash"` + Ts utctime8601 `json:"ts"` +} + +//"eth.tx.created" +type EthTxCreated struct { + TxHash string `json:"tx_hash"` + TxSender string `json:"tx_sender"` + TxAddress string `json:"tx_address"` + TxHexRLP string `json:"tx_hexrlp"` + TxNonce int `json:"tx_nonce"` + Guid string `json:"guid"` + Ts utctime8601 `json:"ts"` +} + +//"eth.tx.received" +type EthTxReceived struct { + TxHash string `json:"tx_hash"` + TxAddress string `json:"tx_address"` + TxHexRLP string `json:"tx_hexrlp"` + RemoteId string `json:"remote_id"` + TxNonce int `json:"tx_nonce"` + Guid string `json:"guid"` + Ts utctime8601 `json:"ts"` +} + +//"eth.tx.broadcasted" +type EthTxBroadcasted struct { + TxHash string `json:"tx_hash"` + TxSender string `json:"tx_sender"` + TxAddress string `json:"tx_address"` + TxNonce int `json:"tx_nonce"` + Guid string `json:"guid"` + Ts utctime8601 `json:"ts"` +} + +//"eth.tx.validated" +type EthTxValidated struct { + TxHash string `json:"tx_hash"` + TxSender string `json:"tx_sender"` + TxAddress string `json:"tx_address"` + TxNonce int `json:"tx_nonce"` + Guid string `json:"guid"` + Ts utctime8601 `json:"ts"` +} + +//"eth.tx.is_invalid" +type EthTxIsInvalid struct { + TxHash string `json:"tx_hash"` + TxSender string `json:"tx_sender"` + TxAddress string `json:"tx_address"` + Reason string `json:"reason"` + TxNonce int `json:"tx_nonce"` + Guid string `json:"guid"` + Ts utctime8601 `json:"ts"` +} diff --git a/p2p/server.go b/p2p/server.go index 7dcbc9d11..ee2d26dbe 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -8,7 +8,6 @@ import ( "sync" "time" - "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/logger" ) @@ -356,25 +355,9 @@ func (srv *Server) dialLoop() { // connect to peer via dial out func (srv *Server) dialPeer(desc *peerAddr, slot int) { srvlog.Debugf("Dialing %v (slot %d)\n", desc, slot) - evd := map[string]interface{}{ - "remote_id": ethutil.Bytes2Hex(desc.Pubkey), - "remote_endpoint": desc.String(), - "level": "debug", - "guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()), - "num_connections": srv.PeerCount(), - } - jsonlogger.LogJson("p2p.connecting", evd) conn, err := srv.Dialer.Dial(desc.Network(), desc.String()) if err != nil { srvlog.DebugDetailf("dial error: %v", err) - evd := map[string]interface{}{ - "reason": "dial error", - "remote_id": desc.String(), - "level": "debug", - "guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()), - "num_connections": srv.PeerCount(), - } - jsonlogger.LogJson("p2p.disconnecting", evd) srv.peerSlots <- slot return } @@ -395,13 +378,6 @@ func (srv *Server) addPeer(conn net.Conn, desc *peerAddr, slot int) *Peer { srv.peers[slot] = peer srv.peerCount++ go func() { - evd := map[string]interface{}{ - "guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()), - "num_connections": srv.PeerCount(), - "remote_id": desc.String(), - "level": "debug", - } - jsonlogger.LogJson("p2p.connected", evd) peer.loop() srv.peerDisconnect <- peer }() @@ -422,36 +398,13 @@ func (srv *Server) removePeer(peer *Peer) { srv.peers[peer.slot] = nil // release slot to signal need for a new peer, last! srv.peerSlots <- peer.slot - evd := map[string]interface{}{ - "guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()), - "num_connections": srv.PeerCount(), - "remote_id": ethutil.Bytes2Hex(peer.Identity().Pubkey()), - "level": "debug", - } - jsonlogger.LogJson("p2p.disconnected", evd) } func (srv *Server) verifyPeer(addr *peerAddr) error { if srv.Blacklist.Exists(addr.Pubkey) { - evd := map[string]interface{}{ - "reason": "blacklisted", - "remote_id": addr.String(), - "level": "debug", - "guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()), - "num_connections": srv.PeerCount(), - } - jsonlogger.LogJson("p2p.disconnecting.reputation", evd) return errors.New("blacklisted") } if bytes.Equal(srv.Identity.Pubkey()[1:], addr.Pubkey) { - evd := map[string]interface{}{ - "reason": "not allowed to connect to srv", - "remote_id": addr.String(), - "level": "debug", - "guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()), - "num_connections": srv.PeerCount(), - } - jsonlogger.LogJson("p2p.disconnecting", evd) return newPeerError(errPubkeyForbidden, "not allowed to connect to srv") } srv.lock.RLock() @@ -460,14 +413,6 @@ func (srv *Server) verifyPeer(addr *peerAddr) error { if peer != nil { id := peer.Identity() if id != nil && bytes.Equal(id.Pubkey(), addr.Pubkey) { - evd := map[string]interface{}{ - "reason": "already connected", - "remote_id": addr.String(), - "level": "debug", - "guid": ethutil.Bytes2Hex(srv.Identity.Pubkey()), - "num_connections": srv.PeerCount(), - } - jsonlogger.LogJson("p2p.disconnecting", evd) return errors.New("already connected") } } -- cgit v1.2.3 From 3d6fd601c5eec13480b6c736f6811b663a885766 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 11 Feb 2015 12:45:41 +0100 Subject: Move event names within each object --- eth/backend.go | 2 +- logger/loggers.go | 3 +- logger/types.go | 144 +++++++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 119 insertions(+), 30 deletions(-) diff --git a/eth/backend.go b/eth/backend.go index cdccd3940..b8b9416d5 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -222,7 +222,7 @@ func (s *Ethereum) MaxPeers() int { // Start the ethereum func (s *Ethereum) Start(seed bool) error { - jsonlogger.LogJson("starting", ðlogger.LogStarting{ + jsonlogger.LogJson(ðlogger.LogStarting{ ClientString: s.ClientIdentity().String(), Guid: ethutil.Bytes2Hex(s.ClientIdentity().Pubkey()), Coinbase: ethutil.Bytes2Hex(s.KeyManager().Address()), diff --git a/logger/loggers.go b/logger/loggers.go index 36bc38116..fd2a01dfd 100644 --- a/logger/loggers.go +++ b/logger/loggers.go @@ -120,7 +120,8 @@ func NewJsonLogger() *JsonLogger { return &JsonLogger{} } -func (logger *JsonLogger) LogJson(msgname string, v interface{}) { +func (logger *JsonLogger) LogJson(v LogEvent) { + msgname := v.EventName() obj := map[string]interface{}{ msgname: v, } diff --git a/logger/types.go b/logger/types.go index ee53394f0..954a76ba3 100644 --- a/logger/types.go +++ b/logger/types.go @@ -11,7 +11,10 @@ func (utctime8601) MarshalJSON() ([]byte, error) { return []byte(`"` + time.Now().UTC().Format(time.RFC3339Nano)[:26] + `Z"`), nil } -//"starting" +type LogEvent interface { + EventName() string +} + type LogStarting struct { ClientString string `json:"version_string"` Guid string `json:"guid"` @@ -20,7 +23,10 @@ type LogStarting struct { Ts utctime8601 `json:"ts"` } -//"p2p.connecting" +func (l *LogStarting) EventName() string { + return "starting" +} + type P2PConnecting struct { RemoteId string `json:"remote_id"` RemoteEndpoint string `json:"remote_endpoint"` @@ -29,7 +35,10 @@ type P2PConnecting struct { Ts utctime8601 `json:"ts"` } -//"p2p.connected" +func (l *P2PConnecting) EventName() string { + return "p2p.connecting" +} + type P2PConnected struct { Guid string `json:"guid"` NumConnections int `json:"num_connections"` @@ -37,7 +46,10 @@ type P2PConnected struct { Ts utctime8601 `json:"ts"` } -//"p2p.handshaked" +func (l *P2PConnected) EventName() string { + return "p2p.connected" +} + type P2PHandshaked struct { RemoteCapabilities []string `json:"remote_capabilities"` RemoteId string `json:"remote_id"` @@ -46,7 +58,10 @@ type P2PHandshaked struct { Ts string `json:"ts"` } -//"p2p.disconnected" +func (l *P2PHandshaked) EventName() string { + return "p2p.handshaked" +} + type P2PDisconnected struct { Guid string `json:"guid"` NumConnections int `json:"num_connections"` @@ -54,7 +69,10 @@ type P2PDisconnected struct { Ts utctime8601 `json:"ts"` } -//"p2p.disconnecting" +func (l *P2PDisconnected) EventName() string { + return "p2p.disconnected" +} + type P2PDisconnecting struct { Reason string `json:"reason"` RemoteId string `json:"remote_id"` @@ -63,7 +81,10 @@ type P2PDisconnecting struct { Ts utctime8601 `json:"ts"` } -//"p2p.disconnecting.bad_handshake" +func (l *P2PDisconnecting) EventName() string { + return "p2p.disconnecting" +} + type P2PDisconnectingBadHandshake struct { Reason string `json:"reason"` RemoteId string `json:"remote_id"` @@ -72,7 +93,10 @@ type P2PDisconnectingBadHandshake struct { Ts utctime8601 `json:"ts"` } -//"p2p.disconnecting.bad_protocol" +func (l *P2PDisconnectingBadHandshake) EventName() string { + return "p2p.disconnecting.bad_handshake" +} + type P2PDisconnectingBadProtocol struct { Reason string `json:"reason"` RemoteId string `json:"remote_id"` @@ -81,7 +105,10 @@ type P2PDisconnectingBadProtocol struct { Ts utctime8601 `json:"ts"` } -//"p2p.disconnecting.reputation" +func (l *P2PDisconnectingBadProtocol) EventName() string { + return "p2p.disconnecting.bad_protocol" +} + type P2PDisconnectingReputation struct { Reason string `json:"reason"` RemoteId string `json:"remote_id"` @@ -90,7 +117,10 @@ type P2PDisconnectingReputation struct { Ts utctime8601 `json:"ts"` } -//"p2p.disconnecting.dht" +func (l *P2PDisconnectingReputation) EventName() string { + return "p2p.disconnecting.reputation" +} + type P2PDisconnectingDHT struct { Reason string `json:"reason"` RemoteId string `json:"remote_id"` @@ -99,7 +129,10 @@ type P2PDisconnectingDHT struct { Ts utctime8601 `json:"ts"` } -//"p2p.eth.disconnecting.bad_block" +func (l *P2PDisconnectingDHT) EventName() string { + return "p2p.disconnecting.dht" +} + type P2PEthDisconnectingBadBlock struct { Reason string `json:"reason"` RemoteId string `json:"remote_id"` @@ -108,7 +141,10 @@ type P2PEthDisconnectingBadBlock struct { Ts utctime8601 `json:"ts"` } -//"p2p.eth.disconnecting.bad_tx" +func (l *P2PEthDisconnectingBadBlock) EventName() string { + return "p2p.eth.disconnecting.bad_block" +} + type P2PEthDisconnectingBadTx struct { Reason string `json:"reason"` RemoteId string `json:"remote_id"` @@ -117,7 +153,10 @@ type P2PEthDisconnectingBadTx struct { Ts utctime8601 `json:"ts"` } -//"eth.newblock.mined" +func (l *P2PEthDisconnectingBadTx) EventName() string { + return "p2p.eth.disconnecting.bad_tx" +} + type EthNewBlockMined struct { BlockNumber int `json:"block_number"` HeadHash string `json:"head_hash"` @@ -129,7 +168,10 @@ type EthNewBlockMined struct { Ts utctime8601 `json:"ts"` } -//"eth.newblock.broadcasted" +func (l *EthNewBlockMined) EventName() string { + return "eth.newblock.mined" +} + type EthNewBlockBroadcasted struct { BlockNumber int `json:"block_number"` HeadHash string `json:"head_hash"` @@ -140,7 +182,10 @@ type EthNewBlockBroadcasted struct { Ts utctime8601 `json:"ts"` } -//"eth.newblock.received" +func (l *EthNewBlockBroadcasted) EventName() string { + return "eth.newblock.broadcasted" +} + type EthNewBlockReceived struct { BlockNumber int `json:"block_number"` HeadHash string `json:"head_hash"` @@ -151,7 +196,10 @@ type EthNewBlockReceived struct { Ts utctime8601 `json:"ts"` } -//"eth.newblock.is_known" +func (l *EthNewBlockReceived) EventName() string { + return "eth.newblock.received" +} + type EthNewBlockIsKnown struct { BlockNumber int `json:"block_number"` HeadHash string `json:"head_hash"` @@ -162,7 +210,10 @@ type EthNewBlockIsKnown struct { Ts utctime8601 `json:"ts"` } -//"eth.newblock.is_new" +func (l *EthNewBlockIsKnown) EventName() string { + return "eth.newblock.is_known" +} + type EthNewBlockIsNew struct { BlockNumber int `json:"block_number"` HeadHash string `json:"head_hash"` @@ -173,7 +224,10 @@ type EthNewBlockIsNew struct { Ts utctime8601 `json:"ts"` } -//"eth.newblock.missing_parent" +func (l *EthNewBlockIsNew) EventName() string { + return "eth.newblock.is_new" +} + type EthNewBlockMissingParent struct { BlockNumber int `json:"block_number"` HeadHash string `json:"head_hash"` @@ -184,7 +238,10 @@ type EthNewBlockMissingParent struct { Ts utctime8601 `json:"ts"` } -//"eth.newblock.is_invalid" +func (l *EthNewBlockMissingParent) EventName() string { + return "eth.newblock.missing_parent" +} + type EthNewBlockIsInvalid struct { BlockNumber int `json:"block_number"` HeadHash string `json:"head_hash"` @@ -195,7 +252,10 @@ type EthNewBlockIsInvalid struct { Ts utctime8601 `json:"ts"` } -//"eth.newblock.chain.is_older" +func (l *EthNewBlockIsInvalid) EventName() string { + return "eth.newblock.is_invalid" +} + type EthNewBlockChainIsOlder struct { BlockNumber int `json:"block_number"` HeadHash string `json:"head_hash"` @@ -206,7 +266,10 @@ type EthNewBlockChainIsOlder struct { Ts utctime8601 `json:"ts"` } -//"eth.newblock.chain.is_cannonical" +func (l *EthNewBlockChainIsOlder) EventName() string { + return "eth.newblock.chain.is_older" +} + type EthNewBlockChainIsCanonical struct { BlockNumber int `json:"block_number"` HeadHash string `json:"head_hash"` @@ -217,7 +280,10 @@ type EthNewBlockChainIsCanonical struct { Ts utctime8601 `json:"ts"` } -//"eth.newblock.chain.not_cannonical" +func (l *EthNewBlockChainIsCanonical) EventName() string { + return "eth.newblock.chain.is_cannonical" +} + type EthNewBlockChainNotCanonical struct { BlockNumber int `json:"block_number"` HeadHash string `json:"head_hash"` @@ -228,7 +294,10 @@ type EthNewBlockChainNotCanonical struct { Ts utctime8601 `json:"ts"` } -//"eth.newblock.chain.switched" +func (l *EthNewBlockChainNotCanonical) EventName() string { + return "eth.newblock.chain.not_cannonical" +} + type EthNewBlockChainSwitched struct { BlockNumber int `json:"block_number"` HeadHash string `json:"head_hash"` @@ -240,7 +309,10 @@ type EthNewBlockChainSwitched struct { Ts utctime8601 `json:"ts"` } -//"eth.tx.created" +func (l *EthNewBlockChainSwitched) EventName() string { + return "eth.newblock.chain.switched" +} + type EthTxCreated struct { TxHash string `json:"tx_hash"` TxSender string `json:"tx_sender"` @@ -251,7 +323,10 @@ type EthTxCreated struct { Ts utctime8601 `json:"ts"` } -//"eth.tx.received" +func (l *EthTxCreated) EventName() string { + return "eth.tx.created" +} + type EthTxReceived struct { TxHash string `json:"tx_hash"` TxAddress string `json:"tx_address"` @@ -262,7 +337,10 @@ type EthTxReceived struct { Ts utctime8601 `json:"ts"` } -//"eth.tx.broadcasted" +func (l *EthTxReceived) EventName() string { + return "eth.tx.received" +} + type EthTxBroadcasted struct { TxHash string `json:"tx_hash"` TxSender string `json:"tx_sender"` @@ -272,7 +350,10 @@ type EthTxBroadcasted struct { Ts utctime8601 `json:"ts"` } -//"eth.tx.validated" +func (l *EthTxBroadcasted) EventName() string { + return "eth.tx.broadcasted" +} + type EthTxValidated struct { TxHash string `json:"tx_hash"` TxSender string `json:"tx_sender"` @@ -282,7 +363,10 @@ type EthTxValidated struct { Ts utctime8601 `json:"ts"` } -//"eth.tx.is_invalid" +func (l *EthTxValidated) EventName() string { + return "eth.tx.validated" +} + type EthTxIsInvalid struct { TxHash string `json:"tx_hash"` TxSender string `json:"tx_sender"` @@ -292,3 +376,7 @@ type EthTxIsInvalid struct { Guid string `json:"guid"` Ts utctime8601 `json:"ts"` } + +func (l *EthTxIsInvalid) EventName() string { + return "eth.tx.is_invalid" +} -- cgit v1.2.3 From db24fb792cf0dab91bc85e79aecf6758349002a4 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Wed, 11 Feb 2015 18:49:00 +0100 Subject: Move standard fields to LogEvent --- eth/backend.go | 2 +- logger/loggers.go | 6 +- logger/types.go | 314 +++++++++++++++++++++++++----------------------------- p2p/server.go | 2 - 4 files changed, 151 insertions(+), 173 deletions(-) diff --git a/eth/backend.go b/eth/backend.go index b8b9416d5..677b5d8e3 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -224,9 +224,9 @@ func (s *Ethereum) MaxPeers() int { func (s *Ethereum) Start(seed bool) error { jsonlogger.LogJson(ðlogger.LogStarting{ ClientString: s.ClientIdentity().String(), - Guid: ethutil.Bytes2Hex(s.ClientIdentity().Pubkey()), Coinbase: ethutil.Bytes2Hex(s.KeyManager().Address()), ProtocolVersion: ProtocolVersion, + LogEvent: ethlogger.LogEvent{Guid: ethutil.Bytes2Hex(s.ClientIdentity().Pubkey())}, }) err := s.net.Start() diff --git a/logger/loggers.go b/logger/loggers.go index fd2a01dfd..147b2b85f 100644 --- a/logger/loggers.go +++ b/logger/loggers.go @@ -114,13 +114,15 @@ func (logger *Logger) Fatalf(format string, v ...interface{}) { os.Exit(0) } -type JsonLogger struct{} +type JsonLogger struct { + Coinbase string +} func NewJsonLogger() *JsonLogger { return &JsonLogger{} } -func (logger *JsonLogger) LogJson(v LogEvent) { +func (logger *JsonLogger) LogJson(v JsonLog) { msgname := v.EventName() obj := map[string]interface{}{ msgname: v, diff --git a/logger/types.go b/logger/types.go index 954a76ba3..f8dcb4e78 100644 --- a/logger/types.go +++ b/logger/types.go @@ -11,16 +11,21 @@ func (utctime8601) MarshalJSON() ([]byte, error) { return []byte(`"` + time.Now().UTC().Format(time.RFC3339Nano)[:26] + `Z"`), nil } -type LogEvent interface { +type JsonLog interface { EventName() string } +type LogEvent struct { + Guid string `json:"guid"` + Ts utctime8601 `json:"ts"` + // Level string `json:"level"` +} + type LogStarting struct { - ClientString string `json:"version_string"` - Guid string `json:"guid"` - Coinbase string `json:"coinbase"` - ProtocolVersion int `json:"eth_version"` - Ts utctime8601 `json:"ts"` + ClientString string `json:"version_string"` + Coinbase string `json:"coinbase"` + ProtocolVersion int `json:"eth_version"` + LogEvent } func (l *LogStarting) EventName() string { @@ -28,11 +33,10 @@ func (l *LogStarting) EventName() string { } type P2PConnecting struct { - RemoteId string `json:"remote_id"` - RemoteEndpoint string `json:"remote_endpoint"` - Guid string `json:"guid"` - NumConnections int `json:"num_connections"` - Ts utctime8601 `json:"ts"` + RemoteId string `json:"remote_id"` + RemoteEndpoint string `json:"remote_endpoint"` + NumConnections int `json:"num_connections"` + LogEvent } func (l *P2PConnecting) EventName() string { @@ -40,10 +44,9 @@ func (l *P2PConnecting) EventName() string { } type P2PConnected struct { - Guid string `json:"guid"` - NumConnections int `json:"num_connections"` - RemoteId string `json:"remote_id"` - Ts utctime8601 `json:"ts"` + NumConnections int `json:"num_connections"` + RemoteId string `json:"remote_id"` + LogEvent } func (l *P2PConnected) EventName() string { @@ -53,9 +56,8 @@ func (l *P2PConnected) EventName() string { type P2PHandshaked struct { RemoteCapabilities []string `json:"remote_capabilities"` RemoteId string `json:"remote_id"` - Guid string `json:"guid"` NumConnections int `json:"num_connections"` - Ts string `json:"ts"` + LogEvent } func (l *P2PHandshaked) EventName() string { @@ -63,10 +65,9 @@ func (l *P2PHandshaked) EventName() string { } type P2PDisconnected struct { - Guid string `json:"guid"` - NumConnections int `json:"num_connections"` - RemoteId string `json:"remote_id"` - Ts utctime8601 `json:"ts"` + NumConnections int `json:"num_connections"` + RemoteId string `json:"remote_id"` + LogEvent } func (l *P2PDisconnected) EventName() string { @@ -74,11 +75,10 @@ func (l *P2PDisconnected) EventName() string { } type P2PDisconnecting struct { - Reason string `json:"reason"` - RemoteId string `json:"remote_id"` - Guid string `json:"guid"` - NumConnections int `json:"num_connections"` - Ts utctime8601 `json:"ts"` + Reason string `json:"reason"` + RemoteId string `json:"remote_id"` + NumConnections int `json:"num_connections"` + LogEvent } func (l *P2PDisconnecting) EventName() string { @@ -86,11 +86,10 @@ func (l *P2PDisconnecting) EventName() string { } type P2PDisconnectingBadHandshake struct { - Reason string `json:"reason"` - RemoteId string `json:"remote_id"` - Guid string `json:"guid"` - NumConnections int `json:"num_connections"` - Ts utctime8601 `json:"ts"` + Reason string `json:"reason"` + RemoteId string `json:"remote_id"` + NumConnections int `json:"num_connections"` + LogEvent } func (l *P2PDisconnectingBadHandshake) EventName() string { @@ -98,11 +97,10 @@ func (l *P2PDisconnectingBadHandshake) EventName() string { } type P2PDisconnectingBadProtocol struct { - Reason string `json:"reason"` - RemoteId string `json:"remote_id"` - Guid string `json:"guid"` - NumConnections int `json:"num_connections"` - Ts utctime8601 `json:"ts"` + Reason string `json:"reason"` + RemoteId string `json:"remote_id"` + NumConnections int `json:"num_connections"` + LogEvent } func (l *P2PDisconnectingBadProtocol) EventName() string { @@ -110,11 +108,10 @@ func (l *P2PDisconnectingBadProtocol) EventName() string { } type P2PDisconnectingReputation struct { - Reason string `json:"reason"` - RemoteId string `json:"remote_id"` - Guid string `json:"guid"` - NumConnections int `json:"num_connections"` - Ts utctime8601 `json:"ts"` + Reason string `json:"reason"` + RemoteId string `json:"remote_id"` + NumConnections int `json:"num_connections"` + LogEvent } func (l *P2PDisconnectingReputation) EventName() string { @@ -122,11 +119,10 @@ func (l *P2PDisconnectingReputation) EventName() string { } type P2PDisconnectingDHT struct { - Reason string `json:"reason"` - RemoteId string `json:"remote_id"` - Guid string `json:"guid"` - NumConnections int `json:"num_connections"` - Ts utctime8601 `json:"ts"` + Reason string `json:"reason"` + RemoteId string `json:"remote_id"` + NumConnections int `json:"num_connections"` + LogEvent } func (l *P2PDisconnectingDHT) EventName() string { @@ -134,11 +130,10 @@ func (l *P2PDisconnectingDHT) EventName() string { } type P2PEthDisconnectingBadBlock struct { - Reason string `json:"reason"` - RemoteId string `json:"remote_id"` - Guid string `json:"guid"` - NumConnections int `json:"num_connections"` - Ts utctime8601 `json:"ts"` + Reason string `json:"reason"` + RemoteId string `json:"remote_id"` + NumConnections int `json:"num_connections"` + LogEvent } func (l *P2PEthDisconnectingBadBlock) EventName() string { @@ -146,11 +141,10 @@ func (l *P2PEthDisconnectingBadBlock) EventName() string { } type P2PEthDisconnectingBadTx struct { - Reason string `json:"reason"` - RemoteId string `json:"remote_id"` - Guid string `json:"guid"` - NumConnections int `json:"num_connections"` - Ts utctime8601 `json:"ts"` + Reason string `json:"reason"` + RemoteId string `json:"remote_id"` + NumConnections int `json:"num_connections"` + LogEvent } func (l *P2PEthDisconnectingBadTx) EventName() string { @@ -158,14 +152,13 @@ func (l *P2PEthDisconnectingBadTx) EventName() string { } type EthNewBlockMined struct { - BlockNumber int `json:"block_number"` - HeadHash string `json:"head_hash"` - BlockHash string `json:"block_hash"` - BlockHexRlp string `json:"block_hexrlp"` - BlockDifficulty int `json:"block_difficulty"` - Guid string `json:"guid"` - BlockPrevHash string `json:"block_prev_hash"` - Ts utctime8601 `json:"ts"` + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockHexRlp string `json:"block_hexrlp"` + BlockDifficulty int `json:"block_difficulty"` + BlockPrevHash string `json:"block_prev_hash"` + LogEvent } func (l *EthNewBlockMined) EventName() string { @@ -173,13 +166,12 @@ func (l *EthNewBlockMined) EventName() string { } type EthNewBlockBroadcasted struct { - BlockNumber int `json:"block_number"` - HeadHash string `json:"head_hash"` - BlockHash string `json:"block_hash"` - BlockDifficulty int `json:"block_difficulty"` - Guid string `json:"guid"` - BlockPrevHash string `json:"block_prev_hash"` - Ts utctime8601 `json:"ts"` + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + BlockPrevHash string `json:"block_prev_hash"` + LogEvent } func (l *EthNewBlockBroadcasted) EventName() string { @@ -187,13 +179,12 @@ func (l *EthNewBlockBroadcasted) EventName() string { } type EthNewBlockReceived struct { - BlockNumber int `json:"block_number"` - HeadHash string `json:"head_hash"` - BlockHash string `json:"block_hash"` - BlockDifficulty int `json:"block_difficulty"` - Guid string `json:"guid"` - BlockPrevHash string `json:"block_prev_hash"` - Ts utctime8601 `json:"ts"` + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + BlockPrevHash string `json:"block_prev_hash"` + LogEvent } func (l *EthNewBlockReceived) EventName() string { @@ -201,13 +192,12 @@ func (l *EthNewBlockReceived) EventName() string { } type EthNewBlockIsKnown struct { - BlockNumber int `json:"block_number"` - HeadHash string `json:"head_hash"` - BlockHash string `json:"block_hash"` - BlockDifficulty int `json:"block_difficulty"` - Guid string `json:"guid"` - BlockPrevHash string `json:"block_prev_hash"` - Ts utctime8601 `json:"ts"` + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + BlockPrevHash string `json:"block_prev_hash"` + LogEvent } func (l *EthNewBlockIsKnown) EventName() string { @@ -215,13 +205,12 @@ func (l *EthNewBlockIsKnown) EventName() string { } type EthNewBlockIsNew struct { - BlockNumber int `json:"block_number"` - HeadHash string `json:"head_hash"` - BlockHash string `json:"block_hash"` - BlockDifficulty int `json:"block_difficulty"` - Guid string `json:"guid"` - BlockPrevHash string `json:"block_prev_hash"` - Ts utctime8601 `json:"ts"` + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + BlockPrevHash string `json:"block_prev_hash"` + LogEvent } func (l *EthNewBlockIsNew) EventName() string { @@ -229,13 +218,12 @@ func (l *EthNewBlockIsNew) EventName() string { } type EthNewBlockMissingParent struct { - BlockNumber int `json:"block_number"` - HeadHash string `json:"head_hash"` - BlockHash string `json:"block_hash"` - BlockDifficulty int `json:"block_difficulty"` - Guid string `json:"guid"` - BlockPrevHash string `json:"block_prev_hash"` - Ts utctime8601 `json:"ts"` + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + BlockPrevHash string `json:"block_prev_hash"` + LogEvent } func (l *EthNewBlockMissingParent) EventName() string { @@ -243,13 +231,12 @@ func (l *EthNewBlockMissingParent) EventName() string { } type EthNewBlockIsInvalid struct { - BlockNumber int `json:"block_number"` - HeadHash string `json:"head_hash"` - BlockHash string `json:"block_hash"` - BlockDifficulty int `json:"block_difficulty"` - Guid string `json:"guid"` - BlockPrevHash string `json:"block_prev_hash"` - Ts utctime8601 `json:"ts"` + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + BlockPrevHash string `json:"block_prev_hash"` + LogEvent } func (l *EthNewBlockIsInvalid) EventName() string { @@ -257,13 +244,12 @@ func (l *EthNewBlockIsInvalid) EventName() string { } type EthNewBlockChainIsOlder struct { - BlockNumber int `json:"block_number"` - HeadHash string `json:"head_hash"` - BlockHash string `json:"block_hash"` - BlockDifficulty int `json:"block_difficulty"` - Guid string `json:"guid"` - BlockPrevHash string `json:"block_prev_hash"` - Ts utctime8601 `json:"ts"` + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + BlockPrevHash string `json:"block_prev_hash"` + LogEvent } func (l *EthNewBlockChainIsOlder) EventName() string { @@ -271,13 +257,12 @@ func (l *EthNewBlockChainIsOlder) EventName() string { } type EthNewBlockChainIsCanonical struct { - BlockNumber int `json:"block_number"` - HeadHash string `json:"head_hash"` - BlockHash string `json:"block_hash"` - BlockDifficulty int `json:"block_difficulty"` - Guid string `json:"guid"` - BlockPrevHash string `json:"block_prev_hash"` - Ts utctime8601 `json:"ts"` + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + BlockPrevHash string `json:"block_prev_hash"` + LogEvent } func (l *EthNewBlockChainIsCanonical) EventName() string { @@ -285,13 +270,12 @@ func (l *EthNewBlockChainIsCanonical) EventName() string { } type EthNewBlockChainNotCanonical struct { - BlockNumber int `json:"block_number"` - HeadHash string `json:"head_hash"` - BlockHash string `json:"block_hash"` - BlockDifficulty int `json:"block_difficulty"` - Guid string `json:"guid"` - BlockPrevHash string `json:"block_prev_hash"` - Ts utctime8601 `json:"ts"` + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + BlockPrevHash string `json:"block_prev_hash"` + LogEvent } func (l *EthNewBlockChainNotCanonical) EventName() string { @@ -299,14 +283,13 @@ func (l *EthNewBlockChainNotCanonical) EventName() string { } type EthNewBlockChainSwitched struct { - BlockNumber int `json:"block_number"` - HeadHash string `json:"head_hash"` - OldHeadHash string `json:"old_head_hash"` - BlockHash string `json:"block_hash"` - BlockDifficulty int `json:"block_difficulty"` - Guid string `json:"guid"` - BlockPrevHash string `json:"block_prev_hash"` - Ts utctime8601 `json:"ts"` + BlockNumber int `json:"block_number"` + HeadHash string `json:"head_hash"` + OldHeadHash string `json:"old_head_hash"` + BlockHash string `json:"block_hash"` + BlockDifficulty int `json:"block_difficulty"` + BlockPrevHash string `json:"block_prev_hash"` + LogEvent } func (l *EthNewBlockChainSwitched) EventName() string { @@ -314,13 +297,12 @@ func (l *EthNewBlockChainSwitched) EventName() string { } type EthTxCreated struct { - TxHash string `json:"tx_hash"` - TxSender string `json:"tx_sender"` - TxAddress string `json:"tx_address"` - TxHexRLP string `json:"tx_hexrlp"` - TxNonce int `json:"tx_nonce"` - Guid string `json:"guid"` - Ts utctime8601 `json:"ts"` + TxHash string `json:"tx_hash"` + TxSender string `json:"tx_sender"` + TxAddress string `json:"tx_address"` + TxHexRLP string `json:"tx_hexrlp"` + TxNonce int `json:"tx_nonce"` + LogEvent } func (l *EthTxCreated) EventName() string { @@ -328,13 +310,12 @@ func (l *EthTxCreated) EventName() string { } type EthTxReceived struct { - TxHash string `json:"tx_hash"` - TxAddress string `json:"tx_address"` - TxHexRLP string `json:"tx_hexrlp"` - RemoteId string `json:"remote_id"` - TxNonce int `json:"tx_nonce"` - Guid string `json:"guid"` - Ts utctime8601 `json:"ts"` + TxHash string `json:"tx_hash"` + TxAddress string `json:"tx_address"` + TxHexRLP string `json:"tx_hexrlp"` + RemoteId string `json:"remote_id"` + TxNonce int `json:"tx_nonce"` + LogEvent } func (l *EthTxReceived) EventName() string { @@ -342,12 +323,11 @@ func (l *EthTxReceived) EventName() string { } type EthTxBroadcasted struct { - TxHash string `json:"tx_hash"` - TxSender string `json:"tx_sender"` - TxAddress string `json:"tx_address"` - TxNonce int `json:"tx_nonce"` - Guid string `json:"guid"` - Ts utctime8601 `json:"ts"` + TxHash string `json:"tx_hash"` + TxSender string `json:"tx_sender"` + TxAddress string `json:"tx_address"` + TxNonce int `json:"tx_nonce"` + LogEvent } func (l *EthTxBroadcasted) EventName() string { @@ -355,12 +335,11 @@ func (l *EthTxBroadcasted) EventName() string { } type EthTxValidated struct { - TxHash string `json:"tx_hash"` - TxSender string `json:"tx_sender"` - TxAddress string `json:"tx_address"` - TxNonce int `json:"tx_nonce"` - Guid string `json:"guid"` - Ts utctime8601 `json:"ts"` + TxHash string `json:"tx_hash"` + TxSender string `json:"tx_sender"` + TxAddress string `json:"tx_address"` + TxNonce int `json:"tx_nonce"` + LogEvent } func (l *EthTxValidated) EventName() string { @@ -368,13 +347,12 @@ func (l *EthTxValidated) EventName() string { } type EthTxIsInvalid struct { - TxHash string `json:"tx_hash"` - TxSender string `json:"tx_sender"` - TxAddress string `json:"tx_address"` - Reason string `json:"reason"` - TxNonce int `json:"tx_nonce"` - Guid string `json:"guid"` - Ts utctime8601 `json:"ts"` + TxHash string `json:"tx_hash"` + TxSender string `json:"tx_sender"` + TxAddress string `json:"tx_address"` + Reason string `json:"reason"` + TxNonce int `json:"tx_nonce"` + LogEvent } func (l *EthTxIsInvalid) EventName() string { diff --git a/p2p/server.go b/p2p/server.go index ee2d26dbe..e0d9f18a5 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -20,8 +20,6 @@ const ( var srvlog = logger.NewLogger("P2P Server") -var jsonlogger = logger.NewJsonLogger() - // Server manages all peer connections. // // The fields of Server are used as configuration parameters. -- cgit v1.2.3