Skip to content

Commit 0b46303

Browse files
Peter Wilhelmsson2hdddg
authored andcommitted
Centralized log names to simplify implementation of filtered loggers
1 parent 8c88b6f commit 0b46303

File tree

14 files changed

+217
-200
lines changed

14 files changed

+217
-200
lines changed

neo4j/consolelogger.go

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
package neo4j
2121

2222
import (
23-
"fmt"
24-
"os"
25-
"time"
23+
"github.com/neo4j/neo4j-go-driver/v4/neo4j/log"
2624
)
2725

2826
// LogLevel is the type that default logging implementations use for available
@@ -40,63 +38,11 @@ const (
4038
DEBUG = 4
4139
)
4240

43-
func ConsoleLogger(level LogLevel) *ConsoleLog {
44-
return &ConsoleLog{
41+
func ConsoleLogger(level LogLevel) *log.Console {
42+
return &log.Console{
4543
Errors: level >= ERROR,
4644
Warns: level >= WARNING,
4745
Infos: level >= INFO,
4846
Debugs: level >= DEBUG,
4947
}
5048
}
51-
52-
// 2020-05-03 12:39:45.001 ERROR [router 1] Failed to connect
53-
// 2020-05-03 12:39:45.001 INFO [Server@192.168.0.1:666] Custom message
54-
// 2020-05-03 12:39:45.001 WARN [bolt3 bolt-361@127.0.0.1:7687] Custom message
55-
type ConsoleLog struct {
56-
Errors bool
57-
Infos bool
58-
Warns bool
59-
Debugs bool
60-
}
61-
62-
const timeFormat = "2006-01-02 15:04:05.000"
63-
64-
func (l *ConsoleLog) Error(name, id string, err error) {
65-
if !l.Errors {
66-
return
67-
}
68-
now := time.Now()
69-
fmt.Fprintf(os.Stderr, "%s ERROR [%s %s] %s\n", now.Format(timeFormat), name, id, err.Error())
70-
}
71-
72-
func (l *ConsoleLog) Errorf(name, id string, msg string, args ...interface{}) {
73-
if !l.Infos {
74-
return
75-
}
76-
now := time.Now()
77-
fmt.Fprintf(os.Stdout, "%s ERROR [%s %s] %s\n", now.Format(timeFormat), name, id, fmt.Sprintf(msg, args...))
78-
}
79-
80-
func (l *ConsoleLog) Infof(name, id string, msg string, args ...interface{}) {
81-
if !l.Infos {
82-
return
83-
}
84-
now := time.Now()
85-
fmt.Fprintf(os.Stdout, "%s INFO [%s %s] %s\n", now.Format(timeFormat), name, id, fmt.Sprintf(msg, args...))
86-
}
87-
88-
func (l *ConsoleLog) Warnf(name, id string, msg string, args ...interface{}) {
89-
if !l.Warns {
90-
return
91-
}
92-
now := time.Now()
93-
fmt.Fprintf(os.Stdout, "%s WARN [%s %s] %s\n", now.Format(timeFormat), name, id, fmt.Sprintf(msg, args...))
94-
}
95-
96-
func (l *ConsoleLog) Debugf(name, id string, msg string, args ...interface{}) {
97-
if !l.Debugs {
98-
return
99-
}
100-
now := time.Now()
101-
fmt.Fprintf(os.Stdout, "%s DEBUG [%s %s] %s\n", now.Format(timeFormat), name, id, fmt.Sprintf(msg, args...))
102-
}

neo4j/driver.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ package neo4j
2323
import (
2424
"fmt"
2525
"net/url"
26-
"strconv"
2726
"strings"
2827
"sync"
29-
"sync/atomic"
3028

3129
"github.com/neo4j/neo4j-go-driver/v4/neo4j/db"
3230
"github.com/neo4j/neo4j-go-driver/v4/neo4j/log"
@@ -64,8 +62,6 @@ type Driver interface {
6462
Close() error
6563
}
6664

67-
var driverLogName = "driver"
68-
6965
// NewDriver is the entry point to the neo4j driver to create an instance of a Driver. It is the first function to
7066
// be called in order to establish a connection to a neo4j database. It requires a Bolt URI and an authentication
7167
// token as parameters and can also take optional configuration function(s) as variadic parameters.
@@ -132,24 +128,25 @@ func NewDriver(target string, auth AuthToken, configurers ...func(*Config)) (Dri
132128
d.log = d.config.Log
133129
if d.log == nil {
134130
// Default to void logger
135-
d.log = &VoidLog{}
131+
d.log = &log.Void{}
136132
}
137-
d.logId = strconv.FormatUint(uint64(atomic.AddUint32(&driverid, 1)), 10)
133+
d.logId = log.NewId()
138134

139135
// Continue to setup connector
140136
d.connector.UserAgent = d.config.UserAgent
141137
d.connector.RootCAs = d.config.RootCAs
142138
d.connector.Log = d.log
143139
d.connector.Auth = auth.tokens
144140

145-
d.pool = pool.New(d.config.MaxConnectionPoolSize, d.config.MaxConnectionLifetime, d.connector.Connect, d.log)
141+
// Let the pool use the same logid as the driver to simplify log reading.
142+
d.pool = pool.New(d.config.MaxConnectionPoolSize, d.config.MaxConnectionLifetime, d.connector.Connect, d.log, d.logId)
146143

147144
if !routing {
148145
d.router = &directRouter{server: parsed.Host}
149146
} else {
150147
routingContext, err := routingContextFromUrl(parsed)
151148
if err != nil {
152-
d.log.Error(driverLogName, d.logId, err)
149+
d.log.Error(log.Driver, d.logId, err)
153150
return nil, err
154151
}
155152

@@ -165,15 +162,14 @@ func NewDriver(target string, auth AuthToken, configurers ...func(*Config)) (Dri
165162
return servers
166163
}
167164
}
168-
d.router = router.New(parsed.Host, routersResolver, routingContext, d.pool, d.log)
165+
// Let the router use the same logid as the driver to simplify log reading.
166+
d.router = router.New(parsed.Host, routersResolver, routingContext, d.pool, d.log, d.logId)
169167
}
170168

171-
d.log.Infof(driverLogName, d.logId, "Created { target: %s }", target)
169+
d.log.Infof(log.Driver, d.logId, "Created { target: %s }", target)
172170
return &d, nil
173171
}
174172

175-
var driverid uint32
176-
177173
func routingContextFromUrl(u *url.URL) (map[string]string, error) {
178174
queryValues := u.Query()
179175
routingContext := make(map[string]string, len(queryValues))
@@ -274,6 +270,6 @@ func (d *driver) Close() error {
274270
d.pool.Close()
275271
}
276272
d.pool = nil
277-
d.log.Infof(driverLogName, d.logId, "Closed")
273+
d.log.Infof(log.Driver, d.logId, "Closed")
278274
return nil
279275
}

neo4j/internal/bolt/bolt3.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ type bolt3 struct {
8484
bookmark string // Last bookmark
8585
birthDate time.Time
8686
log log.Logger
87-
logName string
8887
receiveBufer []byte
8988
sendBuffers [][]byte
9089
}
@@ -101,7 +100,6 @@ func NewBolt3(serverName string, conn net.Conn, log log.Logger) *bolt3 {
101100
unpacker: &packstream.Unpacker{},
102101
birthDate: time.Now(),
103102
log: log,
104-
logName: "bolt3",
105103
}
106104
}
107105

@@ -122,7 +120,7 @@ func (b *bolt3) appendMsg(tag packstream.StructTag, field ...interface{}) error
122120
// At this point we do not know the state of what has been written to the chunks.
123121
// Either we should support rolling back whatever that has been written or just
124122
// bail out this session.
125-
b.log.Error(b.logName, b.logId, err)
123+
b.log.Error(log.Bolt3, b.logId, err)
126124
b.state = bolt3_dead
127125
return err
128126
}
@@ -135,7 +133,7 @@ func (b *bolt3) sendMsg(tag packstream.StructTag, field ...interface{}) error {
135133
return err
136134
}
137135
if err := b.chunker.send(b.conn); err != nil {
138-
b.log.Error(b.logName, b.logId, err)
136+
b.log.Error(log.Bolt3, b.logId, err)
139137
b.state = bolt3_dead
140138
return err
141139
}
@@ -146,14 +144,14 @@ func (b *bolt3) receiveMsg() (interface{}, error) {
146144
var err error
147145
b.receiveBufer, err = dechunkMessage(b.conn, b.receiveBufer)
148146
if err != nil {
149-
b.log.Error(b.logName, b.logId, err)
147+
b.log.Error(log.Bolt3, b.logId, err)
150148
b.state = bolt3_dead
151149
return nil, err
152150
}
153151

154152
msg, err := b.unpacker.UnpackStruct(b.receiveBufer, hydrate)
155153
if err != nil {
156-
b.log.Error(b.logName, b.logId, err)
154+
b.log.Error(log.Bolt3, b.logId, err)
157155
b.state = bolt3_dead
158156
return nil, err
159157
}
@@ -176,15 +174,15 @@ func (b *bolt3) receiveSuccess() (*successResponse, error) {
176174
b.state = bolt3_failed
177175
if v.Classification() == "ClientError" {
178176
// These could include potentially large cypher statement, only log to debug
179-
b.log.Debugf(b.logName, b.logId, "%s", v)
177+
b.log.Debugf(log.Bolt3, b.logId, "%s", v)
180178
} else {
181-
b.log.Error(b.logName, b.logId, v)
179+
b.log.Error(log.Bolt3, b.logId, v)
182180
}
183181
return nil, v
184182
}
185183
b.state = bolt3_dead
186184
err = errors.New("Expected success or database error")
187-
b.log.Error(b.logName, b.logId, err)
185+
b.log.Error(log.Bolt3, b.logId, err)
188186
return nil, err
189187
}
190188

@@ -225,7 +223,7 @@ func (b *bolt3) connect(auth map[string]interface{}, userAgent string) error {
225223

226224
// Transition into ready state
227225
b.state = bolt3_ready
228-
b.log.Infof(b.logName, b.logId, "Connected")
226+
b.log.Infof(log.Bolt3, b.logId, "Connected")
229227
return nil
230228
}
231229

@@ -307,7 +305,7 @@ func (b *bolt3) TxCommit(txh db.Handle) error {
307305
if commitSuccess == nil {
308306
b.state = bolt3_dead
309307
err := errors.New(fmt.Sprintf("Failed to parse commit response: %+v", succRes))
310-
b.log.Error(b.logName, b.logId, err)
308+
b.log.Error(log.Bolt3, b.logId, err)
311309
return err
312310
}
313311

@@ -430,7 +428,7 @@ func (b *bolt3) run(cypher string, params map[string]interface{}, tx *internalTx
430428
if runRes == nil {
431429
b.state = bolt3_dead
432430
err = errors.New(fmt.Sprintf("Failed to parse RUN response: %+v", res))
433-
b.log.Error(b.logName, b.logId, err)
431+
b.log.Error(log.Bolt3, b.logId, err)
434432
return nil, err
435433
}
436434
b.tfirst = runRes.t_first
@@ -448,11 +446,11 @@ func (b *bolt3) run(cypher string, params map[string]interface{}, tx *internalTx
448446
}
449447

450448
func (b *bolt3) logError(err error) {
451-
b.log.Error(b.logName, b.logId, err)
449+
b.log.Error(log.Bolt3, b.logId, err)
452450
}
453451

454452
func (b *bolt3) logDebug(err error) {
455-
b.log.Debugf(b.logName, b.logId, "%s", err)
453+
b.log.Debugf(log.Bolt3, b.logId, "%s", err)
456454
}
457455

458456
func (b *bolt3) Run(
@@ -508,7 +506,7 @@ func (b *bolt3) Next(shandle db.Handle) (*db.Record, *db.Summary, error) {
508506
if sum == nil {
509507
b.state = bolt3_dead
510508
err = errors.New("Failed to parse summary")
511-
b.log.Error(b.logName, b.logId, err)
509+
b.log.Error(log.Bolt3, b.logId, err)
512510
return nil, nil, err
513511
}
514512
if b.state == bolt3_streamingtx {
@@ -530,15 +528,15 @@ func (b *bolt3) Next(shandle db.Handle) (*db.Record, *db.Summary, error) {
530528
b.state = bolt3_failed
531529
if x.Classification() == "ClientError" {
532530
// These could include potentially large cypher statement, only log to debug
533-
b.log.Debugf(b.logName, b.logId, "%s", x)
531+
b.log.Debugf(log.Bolt3, b.logId, "%s", x)
534532
} else {
535-
b.log.Error(b.logName, b.logId, x)
533+
b.log.Error(log.Bolt3, b.logId, x)
536534
}
537535
return nil, nil, x
538536
default:
539537
b.state = bolt3_dead
540538
err = errors.New("Unknown response")
541-
b.log.Error(b.logName, b.logId, err)
539+
b.log.Error(log.Bolt3, b.logId, err)
542540
return nil, nil, err
543541
}
544542
}
@@ -645,7 +643,7 @@ func (b *bolt3) GetRoutingTable(database string, context map[string]string) (*db
645643

646644
// Beware, could be called on another thread when driver is closed.
647645
func (b *bolt3) Close() {
648-
b.log.Infof(b.logName, b.logId, "Disconnected")
646+
b.log.Infof(log.Bolt3, b.logId, "Disconnected")
649647
if b.state != bolt3_dead {
650648
b.sendMsg(msgGoodbye)
651649
}

0 commit comments

Comments
 (0)