@@ -32,17 +32,22 @@ import (
3232 "github.com/ethereum/go-ethereum/core/types"
3333 "github.com/ethereum/go-ethereum/crypto"
3434 "github.com/ethereum/go-ethereum/ethclient"
35+ "github.com/ethereum/go-ethereum/log"
3536 "github.com/ethereum/go-ethereum/p2p/enode"
3637 "github.com/ethersphere/swarm/contracts/swap"
3738 contract "github.com/ethersphere/swarm/contracts/swap"
38- "github.com/ethersphere/swarm/log"
3939 "github.com/ethersphere/swarm/p2p/protocols"
4040 "github.com/ethersphere/swarm/state"
4141)
4242
4343// ErrInvalidChequeSignature indicates the signature on the cheque was invalid
4444var ErrInvalidChequeSignature = errors .New ("invalid cheque signature" )
4545
46+ var auditLog log.Logger // logger for Swap related messages and audit trail
47+
48+ // swapLogLevel indicates filter level of log messages
49+ const swapLogLevel = 3
50+
4651// Swap represents the Swarm Accounting Protocol
4752// a peer to peer micropayment system
4853// A node maintains an individual balance with every peer
@@ -80,8 +85,42 @@ func NewParams() *Params {
8085 }
8186}
8287
88+ // newLogger returns a new logger
89+ func newLogger (logpath string ) log.Logger {
90+ swapLogger := log .New ("swaplog" , "*" )
91+
92+ lh := log .Root ().GetHandler ()
93+ rfh , err := swapRotatingFileHandler (logpath )
94+
95+ if err != nil {
96+ log .Warn ("RotatingFileHandler was not initialized" , "logdir" , logpath , "err" , err )
97+ //sets a fallback logger, it will use the swarm logger.
98+ swapLogger .SetHandler (lh )
99+ return swapLogger
100+ }
101+
102+ //Filters messages with the correct logLevel for swap
103+ rfh = log .LvlFilterHandler (log .Lvl (swapLogLevel ), rfh )
104+
105+ //Dispatches the logs to the default swarm log and also the filtered swap file logger.
106+ swapLogger .SetHandler (log .MultiHandler (lh , rfh ))
107+
108+ return swapLogger
109+ }
110+
111+ // swapRotatingFileHandler returns a RotatingFileHandler this will split the logs into multiple files.
112+ // the files are split based on the limit parameter expressed in bytes
113+ func swapRotatingFileHandler (logdir string ) (log.Handler , error ) {
114+ return log .RotatingFileHandler (
115+ logdir ,
116+ 262144 ,
117+ log .JSONFormatOrderedEx (false , true ),
118+ )
119+ }
120+
83121// new - swap constructor without integrity check
84- func new (stateStore state.Store , prvkey * ecdsa.PrivateKey , backend contract.Backend , disconnectThreshold uint64 , paymentThreshold uint64 ) * Swap {
122+ func new (logpath string , stateStore state.Store , prvkey * ecdsa.PrivateKey , backend contract.Backend , disconnectThreshold uint64 , paymentThreshold uint64 ) * Swap {
123+ auditLog = newLogger (logpath )
85124 return & Swap {
86125 store : stateStore ,
87126 peers : make (map [enode.ID ]* Peer ),
@@ -95,7 +134,7 @@ func new(stateStore state.Store, prvkey *ecdsa.PrivateKey, backend contract.Back
95134}
96135
97136// New - swap constructor with integrity checks
98- func New (dbPath string , prvkey * ecdsa.PrivateKey , backendURL string , disconnectThreshold uint64 , paymentThreshold uint64 ) (* Swap , error ) {
137+ func New (logpath string , dbPath string , prvkey * ecdsa.PrivateKey , backendURL string , disconnectThreshold uint64 , paymentThreshold uint64 ) (* Swap , error ) {
99138 // we MUST have a backend
100139 if backendURL == "" {
101140 return nil , errors .New ("swap init error: no backend URL given" )
@@ -114,6 +153,7 @@ func New(dbPath string, prvkey *ecdsa.PrivateKey, backendURL string, disconnectT
114153 return nil , fmt .Errorf ("swap init error: error connecting to Ethereum API %s: %s" , backendURL , err )
115154 }
116155 return new (
156+ logpath ,
117157 stateStore ,
118158 prvkey ,
119159 backend ,
@@ -185,7 +225,7 @@ func (s *Swap) Add(amount int64, peer *protocols.Peer) (err error) {
185225 // It is the peer with a negative balance who sends a cheque, thus we check
186226 // that the balance is *below* the threshold
187227 if swapPeer .getBalance () <= - s .paymentThreshold {
188- log . Warn ("balance for peer went over the payment threshold, sending cheque" , "peer" , peer .ID ().String (), "payment threshold" , s .paymentThreshold )
228+ auditLog . Info ("balance for peer went over the payment threshold, sending cheque" , "peer" , peer .ID ().String (), "payment threshold" , s .paymentThreshold )
189229 return swapPeer .sendCheque ()
190230 }
191231
@@ -212,13 +252,13 @@ func (s *Swap) handleEmitChequeMsg(ctx context.Context, p *Peer, msg *EmitCheque
212252 defer p .lock .Unlock ()
213253
214254 cheque := msg .Cheque
215- log .Info ("received cheque from peer" , "peer" , p .ID ().String (), "honey" , cheque .Honey )
255+ auditLog .Info ("received cheque from peer" , "peer" , p .ID ().String (), "honey" , cheque .Honey )
216256 _ , err := s .processAndVerifyCheque (cheque , p )
217257 if err != nil {
218258 return err
219259 }
220260
221- log .Debug ("received cheque processed and verified" , "peer" , p .ID ().String ())
261+ auditLog .Debug ("received cheque processed and verified" , "peer" , p .ID ().String ())
222262
223263 // reset balance by amount
224264 // as this is done by the creditor, receiving the cheque, the amount should be negative,
@@ -249,17 +289,17 @@ func cashCheque(s *Swap, otherSwap contract.Contract, opts *bind.TransactOpts, c
249289 if err != nil {
250290 // TODO: do something with the error
251291 // and we actually need to log this error as we are in an async routine; nobody is handling this error for now
252- log .Error ("error cashing cheque" , "err" , err )
292+ auditLog .Error ("error cashing cheque" , "err" , err )
253293 return
254294 }
255295
256296 if result .Bounced {
257- log . Error ("cheque bounced" , "tx" , receipt .TxHash )
297+ auditLog . Warn ("cheque bounced" , "tx" , receipt .TxHash )
258298 return
259299 // TODO: do something here
260300 }
261301
262- log .Debug ("cash tx mined" , "receipt" , receipt )
302+ auditLog .Debug ("cash tx mined" , "receipt" , receipt )
263303}
264304
265305// processAndVerifyCheque verifies the cheque and compares it with the last received cheque
@@ -283,7 +323,7 @@ func (s *Swap) processAndVerifyCheque(cheque *Cheque, p *Peer) (uint64, error) {
283323 }
284324
285325 if err := p .setLastReceivedCheque (cheque ); err != nil {
286- log .Error ("error while saving last received cheque" , "peer" , p .ID ().String (), "err" , err .Error ())
326+ auditLog .Error ("error while saving last received cheque" , "peer" , p .ID ().String (), "err" , err .Error ())
287327 // TODO: what do we do here? Related issue: https://github.com/ethersphere/swarm/issues/1515
288328 }
289329
@@ -432,13 +472,13 @@ func (s *Swap) Deploy(ctx context.Context) error {
432472 opts .Value = big .NewInt (int64 (s .params .InitialDepositAmount ))
433473 opts .Context = ctx
434474
435- log .Info ("deploying new swap" , "owner" , opts .From .Hex ())
475+ auditLog .Info ("deploying new swap" , "owner" , opts .From .Hex ())
436476 address , err := s .deployLoop (opts , s .owner .address , defaultHarddepositTimeoutDuration )
437477 if err != nil {
438- log .Error ("unable to deploy swap" , "error" , err )
478+ auditLog .Error ("unable to deploy swap" , "error" , err )
439479 return err
440480 }
441- log .Info ("swap deployed" , "address" , address .Hex (), "owner" , opts .From .Hex ())
481+ auditLog .Info ("swap deployed" , "address" , address .Hex (), "owner" , opts .From .Hex ())
442482
443483 return err
444484}
@@ -452,11 +492,11 @@ func (s *Swap) deployLoop(opts *bind.TransactOpts, owner common.Address, default
452492 }
453493
454494 if s .contract , tx , err = contract .Deploy (opts , s .backend , owner , defaultHarddepositTimeoutDuration ); err != nil {
455- log .Warn ("can't send chequebook deploy tx" , "try" , try , "error" , err )
495+ auditLog .Warn ("can't send chequebook deploy tx" , "try" , try , "error" , err )
456496 continue
457497 }
458498 if addr , err = bind .WaitDeployed (opts .Context , s .backend , tx ); err != nil {
459- log .Warn ("chequebook deploy error" , "try" , try , "error" , err )
499+ auditLog .Warn ("chequebook deploy error" , "try" , try , "error" , err )
460500 continue
461501 }
462502 return addr , nil
0 commit comments