Intermittently, I see truncated json objects being written to loggly through my custom logging middleware. I've confirmed the outgoing records are in fact being truncated in my code.
Logs are formatted with zerolog and then T'd out between stdout and loggly.
The loggly package I'm using is pretty old, but appears to just implement an io.Writer
with a buffer https://github.com/segmentio/go-loggly.
My concern is gin is terminating the context before the logs are written to the buffer, the write is cut short? But in examples provided by gin docs I don't see anything wildly different. I've removed as much extraneous code that still experiences the issue.
The logs that are written to STDOUT are complete, but the logs being sent out through the loggly package are being truncated.
package main
import (
"io"
"os"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
"github.com/segmentio/go-loggly"
)
var logglyClient *loggly.Client
func init() {
logglyToken := "potato"
logglyClient = loggly.New(logglyToken)
}
func NewLogger() zerolog.Logger {
writers := []io.Writer{zerolog.ConsoleWriter{Out: os.Stdout}}
writers = append(writers, logglyClient)
multiWriter := zerolog.MultiLevelWriter(writers...)
logger := zerolog.New(multiWriter).With().Timestamp().Logger()
return logger
}
func GinMiddleware() gin.HandlerFunc {
return func(gctx *gin.Context) {
logger := NewLogger()
logger.Info().Msg("API request")
gctx.Next()
}
}
func main() {
router := gin.New()
router.Use(GinMiddleware())
logger := NewLogger()
router.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
logger.Info().Msg("Server Listening!")
router.Run(":8080")
}
with the following packages in use
github.com/gin-gonic/gin v1.7.7
github.com/rs/zerolog v1.26.0
github.com/segmentio/go-loggly v0.5.0