Skip to content
This repository was archived by the owner on Mar 31, 2023. It is now read-only.

Fix JSONFormat to ensure outputting JSON on NaN and Infinities #29

Merged
merged 4 commits into from
Jan 20, 2023

Conversation

itchyny
Copy link
Contributor

@itchyny itchyny commented Mar 14, 2022

Currently (v1.6.1) JSONFormat emits non-JSON on NaN and Infinities.
This PR fixes JSONFormat to ensure the output to be JSON on NaN and Infinities.
Also added tests for Logfmt and PlainFormat to cover these values (behavior not changed).

Sample code

package main

import (
	"encoding/json"
	"fmt"
	"math"
	"os"
	"time"

	"github.com/cybozu-go/log"
)

func main() {
	var err error
	l := log.NewLogger()
	f := log.JSONFormat{Utsname: "test"}
	t := time.Now()

	buf := make([]byte, 0, 256)
	buf, err = f.Format(buf, l, t, log.LvDebug, "test", map[string]interface{}{
		"x": math.NaN(),
		"y": math.Inf(1),
		"z": math.Inf(-1),
	})
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		os.Exit(1)
	}
	fmt.Printf("%s\n", buf)

	var data interface{}
	err = json.Unmarshal(buf, &data)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		os.Exit(1)
	}
	fmt.Printf("%v\n", data)
}

Result

{"topic":"main","logged_at":"2022-03-14T01:37:50.253201Z","severity":"debug","utsname":"test","message":"test","x":NaN,"y":+Inf,"z":-Inf}

invalid character 'N' looking for beginning of value
exit status 1

Expected behavior

I expect this logger either to emit valid JSON or to return error.
Considering the expectation against loggers, I think it is better to emit JSON to allow restoration of the original data.

  • Return an error just like encoding/json: Losing log data on invalid data is unacceptable as a logger.
  • Output "NaN", "+Inf", "-Inf" like zap: Allows restoration of the original data, except for the confusion against string values.
    • This PR implements this behavior.
  • Output null, 1.7976931348623157e+308, -1.7976931348623157e+308 like jq: Familiar behavior of jq user, and keeps the number type, but cannot distinguish null and NaN and this is confusing.

Copy link
Contributor

@morimoto-cybozu morimoto-cybozu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for toooooooo late comments.
If you've lost interest in this PR, please let me know. I'll add a small fix and finish this PR.

Copy link
Contributor

@morimoto-cybozu morimoto-cybozu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
Thanks!

@morimoto-cybozu morimoto-cybozu merged commit b179b74 into cybozu-go:main Jan 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants