Skip to content

Commit 2b98a60

Browse files
authored
Merge pull request #163 from yfyf/reduce-logging
Eliminate unnecessary logs produced by Driver
2 parents d660cba + b515c65 commit 2b98a60

6 files changed

Lines changed: 20 additions & 38 deletions

File tree

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## [UNRELEASED]
44

5+
- Reduce log metadata and volume
6+
57
## [2.7.0] - 2025-10-15
68

79
### Added

src/dividat-driver/senso/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ func (backend *DeviceBackend) Connect(address string) {
122122
backend.broker.TryPub(data, brokerTopicRx)
123123
}
124124

125-
go connectTCP(ctx, backend.log.WithField("channel", "data"), address+":55568", backend.broker.Sub("noTx"), onReceive)
125+
go connectTCP(ctx, backend.log.WithField("channel", "data"), address+":55568", backend.broker.Sub("noTx"), onReceive, true)
126126
time.Sleep(1000 * time.Millisecond)
127-
go connectTCP(ctx, backend.log.WithField("channel", "control"), address+":55567", backend.broker.Sub(brokerTopicTx), onReceive)
127+
go connectTCP(ctx, backend.log.WithField("channel", "control"), address+":55567", backend.broker.Sub(brokerTopicTx), onReceive, false)
128128

129129
backend.cancelCurrentConnection = cancel
130130
}

src/dividat-driver/senso/tcp.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,19 @@ const maxInterval = 30 * time.Second
2020
type onReceive = func([]byte)
2121

2222
// connectTCP creates a persistent tcp connection to address
23-
func connectTCP(ctx context.Context, baseLogger *logrus.Entry, address string, tx chan interface{}, onReceive onReceive) {
23+
// isOptional is used when connecting to the data channel, which is used only in legacy Senso firmware
24+
func connectTCP(ctx context.Context, baseLogger *logrus.Entry, address string, tx chan interface{}, onReceive onReceive, isOptional bool) {
2425
var dialer net.Dialer
2526

2627
var log = baseLogger.WithField("address", address)
2728

29+
var connectionAttemptLogLevel logrus.Level
30+
if isOptional {
31+
connectionAttemptLogLevel = logrus.DebugLevel
32+
} else {
33+
connectionAttemptLogLevel = logrus.InfoLevel
34+
}
35+
2836
var conn net.Conn
2937
dialTCP := func() error {
3038

@@ -34,11 +42,11 @@ func connectTCP(ctx context.Context, baseLogger *logrus.Entry, address string, t
3442
conn.Close()
3543
}
3644

37-
log.Info("Dialing TCP connection.")
45+
log.Log(connectionAttemptLogLevel, "Dialing TCP connection.")
3846
conn, connErr = dialer.DialContext(ctx, "tcp", address)
3947

4048
if connErr != nil {
41-
log.WithError(connErr).Info("Could not connect with Senso.")
49+
log.WithError(connErr).Log(connectionAttemptLogLevel, "Could not connect with Senso.")
4250
}
4351
return connErr
4452
}

src/dividat-driver/server/main.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package server
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"net/http"
78

89
"github.com/sirupsen/logrus"
@@ -28,23 +29,15 @@ func Start(logger *logrus.Logger, origins []string) context.CancelFunc {
2829
logServer := logging.NewLogServer()
2930
logger.AddHook(logServer)
3031

31-
baseLog := logger.WithFields(logrus.Fields{
32-
"version": version,
33-
})
32+
baseLog := logrus.NewEntry(logger)
3433

3534
// Get System information
3635
systemInfo, err := GetSystemInfo()
3736
if err != nil {
3837
baseLog.WithError(err).Panic("Could not get system information.")
3938
}
4039

41-
baseLog = baseLog.WithFields(logrus.Fields{
42-
"machineId": systemInfo.MachineId,
43-
"os": systemInfo.Os,
44-
"arch": systemInfo.Arch,
45-
})
46-
47-
baseLog.Info("Dividat Driver starting")
40+
baseLog.Info(fmt.Sprintf("Dividat Driver (%s) starting", version))
4841

4942
// Setup log endpoint
5043
http.Handle("/log", originMiddleware(origins, baseLog, logServer))
@@ -72,9 +65,6 @@ func Start(logger *logrus.Logger, origins []string) context.CancelFunc {
7265
// Create a logger for server
7366
log := baseLog.WithField("package", "server")
7467

75-
// Start the monitor
76-
go startMonitor(baseLog.WithField("package", "monitor"))
77-
7868
// Setup HTTP Server
7969
server := http.Server{Addr: "127.0.0.1:" + serverPort}
8070

src/dividat-driver/server/monitor.go

Lines changed: 0 additions & 19 deletions
This file was deleted.

test/general.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ it('Opening a second instance of the driver fails.', (done) => {
3838
it('Get log entries from HTTP endpoint.', async () => {
3939
const logs = await getJSON('http://127.0.0.1:8382/log')
4040
expect(logs).to.be.an('array')
41-
expect(logs[0]).to.include({level: 'info', msg: 'Dividat Driver starting'})
41+
expect(logs[0].level).to.equal('info')
42+
expect(logs[0].msg).to.match(/Dividat Driver (.*) starting/)
4243
})

0 commit comments

Comments
 (0)