You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2021/03/19 20:38:32 UTC

[plc4x] branch develop updated: plc4go: refined logging + Added a convenience import to suppress too verbose logging for plc4go user which can be used like that `import _ "github.com/apache/plc4x/plc4go/pkg/plc4go/logging"` + Added log statements to driverManager.go

This is an automated email from the ASF dual-hosted git repository.

sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git


The following commit(s) were added to refs/heads/develop by this push:
     new 34cd8e7  plc4go: refined logging + Added a convenience import to suppress too verbose logging for plc4go user which can be used like that `import _ "github.com/apache/plc4x/plc4go/pkg/plc4go/logging"` + Added log statements to driverManager.go
34cd8e7 is described below

commit 34cd8e7cee3720ac0b4ffa10dc73185605f26ac3
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Fri Mar 19 21:38:15 2021 +0100

    plc4go: refined logging
    + Added a convenience import to suppress too verbose logging for plc4go user which can be used like that `import _ "github.com/apache/plc4x/plc4go/pkg/plc4go/logging"`
    + Added log statements to driverManager.go
---
 plc4go/cmd/main/initializetest/init.go             |  7 +++-
 plc4go/pkg/plc4go/driverManager.go                 | 43 +++++++++++++++++++---
 .../initializetest => pkg/plc4go/logging}/init.go  | 17 +++++++--
 3 files changed, 57 insertions(+), 10 deletions(-)

diff --git a/plc4go/cmd/main/initializetest/init.go b/plc4go/cmd/main/initializetest/init.go
index 5540f3f..7df81f6 100644
--- a/plc4go/cmd/main/initializetest/init.go
+++ b/plc4go/cmd/main/initializetest/init.go
@@ -26,5 +26,10 @@ import (
 
 func init() {
 	onJenkins := os.Getenv("JENKINS_URL") != ""
-	log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, NoColor: onJenkins})
+	log.Logger = log.
+		//// Enable below if you want to see the filenames
+		//With().Caller().Logger().
+		Output(zerolog.ConsoleWriter{Out: os.Stderr, NoColor: onJenkins}).
+		// TODO: set to INFO once log mining is done
+		Level(zerolog.TraceLevel)
 }
diff --git a/plc4go/pkg/plc4go/driverManager.go b/plc4go/pkg/plc4go/driverManager.go
index cd2336e..2ca314e 100644
--- a/plc4go/pkg/plc4go/driverManager.go
+++ b/plc4go/pkg/plc4go/driverManager.go
@@ -19,9 +19,10 @@
 package plc4go
 
 import (
-	"errors"
 	"github.com/apache/plc4x/plc4go/internal/plc4go/spi/transports"
 	"github.com/apache/plc4x/plc4go/pkg/plc4go/model"
+	"github.com/pkg/errors"
+	"github.com/rs/zerolog/log"
 	"net/url"
 )
 
@@ -54,6 +55,7 @@ type PlcDriverManger struct {
 }
 
 func NewPlcDriverManager() PlcDriverManager {
+	log.Trace().Msg("Creating plc driver manager")
 	return PlcDriverManger{
 		drivers:    map[string]PlcDriver{},
 		transports: map[string]transports.Transport{},
@@ -61,20 +63,28 @@ func NewPlcDriverManager() PlcDriverManager {
 }
 
 func (m PlcDriverManger) RegisterDriver(driver PlcDriver) {
+	if driver == nil {
+		panic("driver must not be nil")
+	}
+	log.Debug().Str("protocolName", driver.GetProtocolName()).Msg("Registering driver")
 	// If this driver is already registered, just skip resetting it
 	for driverName := range m.drivers {
 		if driverName == driver.GetProtocolCode() {
+			log.Warn().Str("protocolName", driver.GetProtocolName()).Msg("Already registered")
 			return
 		}
 	}
 	m.drivers[driver.GetProtocolCode()] = driver
+	log.Info().Str("protocolName", driver.GetProtocolName()).Msgf("Driver for %s registered", driver.GetProtocolName())
 }
 
 func (m PlcDriverManger) ListDriverNames() []string {
+	log.Trace().Msg("Listing driver names")
 	var driverNames []string
 	for driverName := range m.drivers {
 		driverNames = append(driverNames, driverName)
 	}
+	log.Trace().Msgf("Found %d driver(s)", len(driverNames))
 	return driverNames
 }
 
@@ -82,44 +92,57 @@ func (m PlcDriverManger) GetDriver(driverName string) (PlcDriver, error) {
 	if val, ok := m.drivers[driverName]; ok {
 		return val, nil
 	}
-	return nil, errors.New("couldn't find driver " + driverName)
+	return nil, errors.Errorf("couldn't find driver %s", driverName)
 }
 
 func (m PlcDriverManger) RegisterTransport(transport transports.Transport) {
+	if transport == nil {
+		panic("transport must not be nil")
+	}
+	log.Debug().Str("transportName", transport.GetTransportName()).Msg("Registering transport")
 	// If this transport is already registered, just skip resetting it
 	for transportName := range m.transports {
 		if transportName == transport.GetTransportCode() {
+			log.Warn().Str("transportName", transport.GetTransportName()).Msg("Transport already registered")
 			return
 		}
 	}
 	m.transports[transport.GetTransportCode()] = transport
+	log.Info().Str("transportName", transport.GetTransportName()).Msgf("Transport for %s registered", transport.GetTransportName())
 }
 
 func (m PlcDriverManger) ListTransportNames() []string {
+	log.Trace().Msg("Listing transport names")
 	var transportNames []string
 	for transportName := range m.transports {
 		transportNames = append(transportNames, transportName)
 	}
+	log.Trace().Msgf("Found %d transports", len(transportNames))
 	return transportNames
 }
 
 func (m PlcDriverManger) GetTransport(transportName string, connectionString string, options map[string][]string) (transports.Transport, error) {
+	// TODO: what are the parameters above for? If not needed we can blank ("_") them
 	if val, ok := m.transports[transportName]; ok {
+		log.Debug().Str("transportName", transportName).Msg("Returning transport name")
 		return val, nil
 	}
 	return nil, errors.New("couldn't find transport " + transportName)
 }
 
 func (m PlcDriverManger) GetConnection(connectionString string) <-chan PlcConnectionConnectResult {
+	log.Debug().Str("connectionString", connectionString).Msgf("Getting connection for %s", connectionString)
 	// Parse the connection string.
 	connectionUrl, err := url.Parse(connectionString)
 	if err != nil {
+		log.Error().Err(err).Msg("Error parsing connection")
 		ch := make(chan PlcConnectionConnectResult)
 		go func() {
-			ch <- NewPlcConnectionConnectResult(nil, errors.New("error parsing connection string: "+err.Error()))
+			ch <- NewPlcConnectionConnectResult(nil, errors.Wrap(err, "error parsing connection string"))
 		}()
 		return ch
 	}
+	log.Debug().Stringer("connectionUrl", connectionUrl).Msg("parsed connection URL")
 
 	// The options will be used to configure both the transports as well as the connections/drivers
 	configOptions := connectionUrl.Query()
@@ -128,9 +151,10 @@ func (m PlcDriverManger) GetConnection(connectionString string) <-chan PlcConnec
 	driverName := connectionUrl.Scheme
 	driver, err := m.GetDriver(driverName)
 	if err != nil {
+		log.Err(err).Str("driverName", driverName).Msgf("Couldn't get driver for %s", driverName)
 		ch := make(chan PlcConnectionConnectResult)
 		go func() {
-			ch <- NewPlcConnectionConnectResult(nil, errors.New("error getting driver for connection string: "+err.Error()))
+			ch <- NewPlcConnectionConnectResult(nil, errors.Wrap(err, "error getting driver for connection string"))
 		}()
 		return ch
 	}
@@ -140,23 +164,31 @@ func (m PlcDriverManger) GetConnection(connectionString string) <-chan PlcConnec
 	var transportName string
 	var transportConnectionString string
 	if len(connectionUrl.Opaque) > 0 {
+		log.Trace().Msg("we handling a opaque connectionUrl")
 		connectionUrl, err := url.Parse(connectionUrl.Opaque)
 		if err != nil {
+			log.Err(err).Str("connectionUrl.Opaque", connectionUrl.Opaque).Msg("Couldn't get transport due to parsing error")
 			ch := make(chan PlcConnectionConnectResult)
 			go func() {
-				ch <- NewPlcConnectionConnectResult(nil, errors.New("error parsing connection string: "+err.Error()))
+				ch <- NewPlcConnectionConnectResult(nil, errors.Wrap(err, "error parsing connection string"))
 			}()
 			return ch
 		}
 		transportName = connectionUrl.Scheme
 		transportConnectionString = connectionUrl.Host
 	} else {
+		log.Trace().Msg("we handling a non-opaque connectionUrl")
 		// If no transport was provided the driver has to provide a default transport.
 		transportName = driver.GetDefaultTransport()
 		transportConnectionString = connectionUrl.Host
 	}
+	log.Debug().
+		Str("transportName", transportName).
+		Str("transportConnectionString", transportConnectionString).
+		Msgf("got a transport %s", transportName)
 	// If no transport has been specified explicitly or per default, we have to abort.
 	if transportName == "" {
+		log.Error().Msg("got a empty transport")
 		ch := make(chan PlcConnectionConnectResult)
 		go func() {
 			ch <- NewPlcConnectionConnectResult(nil, errors.New("no transport specified and no default defined by driver"))
@@ -169,6 +201,7 @@ func (m PlcDriverManger) GetConnection(connectionString string) <-chan PlcConnec
 		Scheme: transportName,
 		Host:   transportConnectionString,
 	}
+	log.Debug().Stringer("transportUrl", &transportUrl).Msg("Assembled transport url")
 
 	// Create a new connection
 	return driver.GetConnection(transportUrl, m.transports, configOptions)
diff --git a/plc4go/cmd/main/initializetest/init.go b/plc4go/pkg/plc4go/logging/init.go
similarity index 75%
copy from plc4go/cmd/main/initializetest/init.go
copy to plc4go/pkg/plc4go/logging/init.go
index 5540f3f..e110e63 100644
--- a/plc4go/cmd/main/initializetest/init.go
+++ b/plc4go/pkg/plc4go/logging/init.go
@@ -16,15 +16,24 @@
 // specific language governing permissions and limitations
 // under the License.
 //
-package initializetest
+package logging
 
 import (
 	"github.com/rs/zerolog"
 	"github.com/rs/zerolog/log"
-	"os"
 )
 
+// init is used for _ imports for easy log config
 func init() {
-	onJenkins := os.Getenv("JENKINS_URL") != ""
-	log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, NoColor: onJenkins})
+	log.Logger.Level(zerolog.ErrorLevel)
+}
+
+// Info configures zerolog to InfoLevel
+func Info() {
+	log.Logger.Level(zerolog.InfoLevel)
+}
+
+// Debug configures zerolog to InfoLevel
+func Debug() {
+	log.Logger.Level(zerolog.DebugLevel)
 }