Skip to content

Commit

Permalink
Merge pull request #10 from go-x-pkg/flag-n-compact-json-output
Browse files Browse the repository at this point in the history
Added flag -n to make long JSONs more compact.
  • Loading branch information
maoueh authored Oct 2, 2023
2 parents e2082a6 + 37929bb commit 0f7a4af
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ zap_instrumented | zap-pretty --all

- `--all` - Show all fields of the line, even those filtered out by default for the active logger format (default `false`).
- `--version` - Show version information.
- `-n` - Format JSON as multiline if got more than n elements in data (default 3).

### Troubleshoot

Expand Down
17 changes: 11 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ var (
date = "unknown"
)

var debug = log.New(ioutil.Discard, "", 0)
var debugEnabled = false
var severityToColor map[string]Color
var (
debug = log.New(ioutil.Discard, "", 0)
debugEnabled = false
severityToColor map[string]Color
)

var errNonZapLine = errors.New("non-zap line")

Expand Down Expand Up @@ -70,8 +72,11 @@ type processor struct {
showAllFields bool
}

var showAllFlag = flag.Bool("all", false, "Show ")
var versionFlag = flag.Bool("version", false, "Prints version information and exit")
var (
showAllFlag = flag.Bool("all", false, "Show ")
versionFlag = flag.Bool("version", false, "Prints version information and exit")
multilineJSONStartingFromLenFlag = flag.Int("n", 3, "Format JSON as multiline if got more than n elements in data")
)

var showAll = false

Expand Down Expand Up @@ -456,7 +461,7 @@ func (p *processor) writeJSON(buffer *bytes.Buffer, data map[string]interface{})
var jsonBytes []byte
var err error

if len(data) <= 3 {
if len(data) <= *multilineJSONStartingFromLenFlag {
jsonBytes, err = json.Marshal(data)
} else {
jsonBytes, err = json.MarshalIndent(data, "", " ")
Expand Down

0 comments on commit 0f7a4af

Please sign in to comment.