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 41f7efc2aa feat(plc4go): ZerlogInterfaceMarshal for Serializable
41f7efc2aa is described below

commit 41f7efc2aa634a90a8afb253a2b88080733c4c0b
Author: Sebastian Rühl <[email protected]>
AuthorDate: Tue Nov 18 20:37:27 2025 +0100

    feat(plc4go): ZerlogInterfaceMarshal for Serializable
---
 plc4go/pkg/api/logging/ZerologInterfaceMarshal.go | 93 +++++++++++++++++++++++
 plc4go/pkg/api/logging/init.go                    | 71 -----------------
 plc4go/spi/testutils/TestUtils.go                 | 10 ++-
 3 files changed, 102 insertions(+), 72 deletions(-)

diff --git a/plc4go/pkg/api/logging/ZerologInterfaceMarshal.go 
b/plc4go/pkg/api/logging/ZerologInterfaceMarshal.go
new file mode 100644
index 0000000000..3b3a82c2e0
--- /dev/null
+++ b/plc4go/pkg/api/logging/ZerologInterfaceMarshal.go
@@ -0,0 +1,93 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+// Deprecated: use options to configure logging
+package logging
+
+import (
+       "context"
+       "encoding/json"
+
+       "github.com/apache/plc4x/plc4go/spi/utils"
+       "github.com/pkg/errors"
+       "github.com/rs/zerolog"
+)
+
+// PLCMessageFormat defines the format of the PLCMessage that is logged
+type PLCMessageFormat uint8
+
+const (
+       // PLCMessageAsJSON defines the format of the PLCMessage as JSON
+       PLCMessageAsJSON PLCMessageFormat = iota
+       // PLCMessageAsString defines the format of the PLCMessage as a string
+       PLCMessageAsString
+       // PLCMessageAsXML defines the format of the PLCMessage as XML
+       PLCMessageAsXML
+       // PLCMessageAsByte defines the format of the PLCMessage as a byte array
+       PLCMessageAsByte
+)
+
+// ZerologInterfacePLCMessageFormat defines the format of the PLCMessage that 
is logged by the zerolog interface marshal function
+var ZerologInterfacePLCMessageFormat = PLCMessageAsString
+
+// ZerologDefaultInterfaceMarshalFunc is the default marshal function used by 
zerolog
+var ZerologDefaultInterfaceMarshalFunc = zerolog.InterfaceMarshalFunc
+
+// ZerologMessageInterfaceMarshalFunc is the marshal function used by zerolog 
to serialize PLCMessages.
+// To use it just do a zerolog.InterfaceMarshalFunc = 
ZerologMessageInterfaceMarshalFunc in a init function.
+var ZerologMessageInterfaceMarshalFunc = func(v interface{}) ([]byte, error) {
+       if plcMessage, ok := v.(utils.Serializable); ok {
+               switch ZerologInterfacePLCMessageFormat {
+               case PLCMessageAsJSON:
+                       jsonWriteBuffer := utils.NewJsonWriteBuffer()
+                       if err := 
plcMessage.SerializeWithWriteBuffer(context.Background(), jsonWriteBuffer); err 
!= nil {
+                               return nil, errors.Wrap(err, "error serializing 
PLCMessage")
+                       }
+                       jsonString, err := jsonWriteBuffer.GetJsonString()
+                       if err != nil {
+                               return nil, errors.Wrapf(err, "error getting 
JSON string from PLCMessage")
+                       }
+                       return []byte(jsonString), nil
+               case PLCMessageAsString:
+                       boxWriteBuffer := utils.NewWriteBufferBoxBased(
+                               utils.WithWriteBufferBoxBasedMergeSingleBoxes(),
+                               utils.WithWriteBufferBoxBasedOmitEmptyBoxes())
+                       if err := 
plcMessage.SerializeWithWriteBuffer(context.Background(), boxWriteBuffer); err 
!= nil {
+                               return nil, errors.Wrap(err, "error serializing 
PLCMessage")
+                       }
+                       boxString := boxWriteBuffer.GetBox().String()
+                       return json.Marshal(boxString)
+               case PLCMessageAsXML:
+                       xmlWriteBuffer := utils.NewXmlWriteBuffer()
+                       if err := 
plcMessage.SerializeWithWriteBuffer(context.Background(), xmlWriteBuffer); err 
!= nil {
+                               return nil, errors.Wrap(err, "error serializing 
PLCMessage")
+                       }
+                       xmlString := xmlWriteBuffer.GetXmlString()
+                       return json.Marshal(xmlString)
+               case PLCMessageAsByte:
+                       byteWriteBuffer := utils.NewWriteBufferByteBased()
+                       if err := 
plcMessage.SerializeWithWriteBuffer(context.Background(), byteWriteBuffer); err 
!= nil {
+                               return nil, errors.Wrap(err, "error serializing 
PLCMessage")
+                       }
+                       bytes := byteWriteBuffer.GetBytes()
+                       return json.Marshal(bytes)
+               }
+       }
+       return ZerologDefaultInterfaceMarshalFunc(v)
+}
diff --git a/plc4go/pkg/api/logging/init.go b/plc4go/pkg/api/logging/init.go
deleted file mode 100644
index 5d1c1bf20d..0000000000
--- a/plc4go/pkg/api/logging/init.go
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-// Deprecated: use options to configure logging
-package logging
-
-import (
-       "github.com/rs/zerolog"
-       globalLog "github.com/rs/zerolog/log"
-)
-
-var oldLogger zerolog.Logger
-
-// Deprecated: use config.WithCustomLogger
-// init is used for _ imports for easy globalLog config
-func init() {
-       oldLogger = globalLog.Logger
-       globalLog.Logger = globalLog.Logger.Level(zerolog.ErrorLevel)
-}
-
-// Deprecated: use config.WithCustomLogger
-// ErrorLevel configures zerolog to WarnLevel
-func ErrorLevel() {
-       globalLog.Logger = globalLog.Logger.Level(zerolog.ErrorLevel)
-}
-
-// Deprecated: use config.WithCustomLogger
-// WarnLevel configures zerolog to WarnLevel
-func WarnLevel() {
-       globalLog.Logger = globalLog.Logger.Level(zerolog.WarnLevel)
-}
-
-// Deprecated: use config.WithCustomLogger
-// InfoLevel configures zerolog to InfoLevel
-func InfoLevel() {
-       globalLog.Logger = globalLog.Logger.Level(zerolog.InfoLevel)
-}
-
-// Deprecated: use config.WithCustomLogger
-// DebugLevel configures zerolog to DebugLevel
-func DebugLevel() {
-       globalLog.Logger = globalLog.Logger.Level(zerolog.DebugLevel)
-}
-
-// Deprecated: use config.WithCustomLogger
-// TraceLevel configures zerolog to TraceLevel
-func TraceLevel() {
-       globalLog.Logger = globalLog.Logger.Level(zerolog.TraceLevel)
-}
-
-// Deprecated: use config.WithCustomLogger
-// ResetLogging can be used to reset to the old globalLog settings
-func ResetLogging() {
-       globalLog.Logger = oldLogger
-}
diff --git a/plc4go/spi/testutils/TestUtils.go 
b/plc4go/spi/testutils/TestUtils.go
index 23b98bf7b2..d2ebb9498c 100644
--- a/plc4go/spi/testutils/TestUtils.go
+++ b/plc4go/spi/testutils/TestUtils.go
@@ -34,6 +34,7 @@ import (
 
        "github.com/ajankovic/xdiff"
        "github.com/ajankovic/xdiff/parser"
+       "github.com/apache/plc4x/plc4go/pkg/api/logging"
        "github.com/fatih/color"
        "github.com/pkg/errors"
        "github.com/rs/zerolog"
@@ -308,11 +309,18 @@ func ProduceTestingLogger(t TestingLog) zerolog.Logger {
                        return r.String()
                }
        })
+       interfaceMarshallerSetter.Do(func() {
+               logging.ZerologInterfacePLCMessageFormat = 
logging.PLCMessageAsString
+               zerolog.InterfaceMarshalFunc = 
logging.ZerologMessageInterfaceMarshalFunc
+       })
        logger = logger.With().Stack().Logger()
        return logger
 }
 
-var stackSetter sync.Once
+var (
+       stackSetter               sync.Once
+       interfaceMarshallerSetter sync.Once
+)
 
 // EnrichOptionsWithOptionsForTesting appends options useful for testing to 
config.WithOption s
 func EnrichOptionsWithOptionsForTesting(t *testing.T, _options 
...options.WithOption) []options.WithOption {

Reply via email to