Skip to content

Commit

Permalink
Fix line handling when caller is not present, added support for log…
Browse files Browse the repository at this point in the history
…ger name
  • Loading branch information
maoueh committed Apr 21, 2022
1 parent 0c20b4b commit c6b911c
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/develop.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ on:

jobs:
build:
runs-on: ubuntu-18.04
runs-on: ubuntu-20.04
strategy:
matrix:
go: [ '1.15', '1.14' ]
go: [ '1.16', '1.17' ]
name: Go ${{ matrix.go }} sample
steps:
- name: Setup go
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Unreleased
## 0.3.0

- [Fix] Fix formatting when `caller` is not present
- [Improvement] The flag `-all` (or `--all`) can now be used to show fields that are filtered out by default
- [Improvement] More fields are filtered by defaults for Zapdriver format (`labels`, `serviceContext`, `logging.googleapis.com/labels` & `logging.googleapis.com/sourceLocation` are filtered out by default now).
- [Fix] Stacktrace are now properly printed when your log format is the Zap production standard one.
1 change: 1 addition & 0 deletions benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func BenchmarkZapdriver(b *testing.B) {

b.ResetTimer()
b.ReportAllocs()

b.SetBytes(byteCount)

for n := 0; n < b.N; n++ {
Expand Down
59 changes: 59 additions & 0 deletions bin/release
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env bash

ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"

main() {
pushd "$ROOT" &> /dev/null

dry_run="true"

while getopts "hf" opt; do
case $opt in
h) usage && exit 0;;
f) dry_run="false";;
\?) usage_error "Invalid option: -$OPTARG";;
esac
done
shift $((OPTIND-1))

version="$1"; shift

if [[ $version == "" ]]; then
usage_error "parameter <version> is required"
fi

args=""
if [[ $dry_run == "true" ]]; then
args="--skip-publish"
fi

goreleaser --rm-dist $args
}

usage_error() {
message="$1"
exit_code="$2"

echo "ERROR: $message"
echo ""
usage
exit ${exit_code:-1}
}

usage() {
echo "usage: release [-f] <version>"
echo ""
echo "Perform the commands necessary to release a new version of the project, mainly"
echo "it does its tasks by calling 'goreleaser' and performing some Git commands."
echo ""
echo "The script runs in dry run automatically unless the '-f' option is provided."
echo ""
echo "Tasks"
echo " ?"
echo ""
echo ""
echo "Options"
echo " -h Display help about this script"
}

main "$@"
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module github.com/maoueh/zap-pretty

go 1.16

require (
github.com/logrusorgru/aurora v0.0.0-20200102142835-e9ef32dff381
github.com/stretchr/testify v1.3.0
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e
)

go 1.13
50 changes: 42 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ func (p *processor) processLine(line string) {
}

func (p *processor) maybePrettyPrintLine(line string, lineData map[string]interface{}) (string, error) {
if lineData["level"] != nil && lineData["ts"] != nil && lineData["caller"] != nil && lineData["msg"] != nil {
if lineData["level"] != nil && lineData["ts"] != nil && lineData["msg"] != nil {
return p.maybePrettyPrintZapLine(line, lineData)
}

if lineData["severity"] != nil && (lineData["time"] != nil || lineData["timestamp"] != nil) && lineData["caller"] != nil && lineData["message"] != nil {
if lineData["severity"] != nil && (lineData["time"] != nil || lineData["timestamp"] != nil) && lineData["message"] != nil {
return p.maybePrettyPrintZapdriverLine(line, lineData)
}

Expand All @@ -206,13 +206,26 @@ func (p *processor) maybePrettyPrintZapLine(line string, lineData map[string]int
return "", fmt.Errorf("unable to process field 'ts': %s", err)
}

var caller *string
if v := lineData["caller"]; v != nil {
callerStr := v.(string)
caller = &callerStr
}

var logger *string
if v := lineData["logger"]; v != nil {
loggerStr := v.(string)
logger = &loggerStr
}

var buffer bytes.Buffer
p.writeHeader(&buffer, logTimestamp, lineData["level"].(string), lineData["caller"].(string), lineData["msg"].(string))
p.writeHeader(&buffer, logTimestamp, lineData["level"].(string), caller, logger, lineData["msg"].(string))

// Delete standard stuff from data fields
delete(lineData, "level")
delete(lineData, "ts")
delete(lineData, "caller")
delete(lineData, "logger")
delete(lineData, "msg")

stacktrace := ""
Expand Down Expand Up @@ -265,12 +278,25 @@ func (p *processor) maybePrettyPrintZapdriverLine(line string, lineData map[stri
return "", fmt.Errorf("unable to process field 'time': %s", err)
}

p.writeHeader(&buffer, &parsedTime, lineData["severity"].(string), lineData["caller"].(string), lineData["message"].(string))
var caller *string
if v := lineData["caller"]; v != nil {
callerStr := v.(string)
caller = &callerStr
}

var logger *string
if v := lineData["logger"]; v != nil {
loggerStr := v.(string)
logger = &loggerStr
}

p.writeHeader(&buffer, &parsedTime, lineData["severity"].(string), caller, logger, lineData["message"].(string))

// Delete standard stuff from data fields
delete(lineData, timeField)
delete(lineData, "severity")
delete(lineData, "caller")
delete(lineData, "logger")
delete(lineData, "message")

if !p.showAllFields {
Expand Down Expand Up @@ -301,14 +327,22 @@ func (p *processor) maybePrettyPrintZapdriverLine(line string, lineData map[stri
return buffer.String(), nil
}

func (p *processor) writeHeader(buffer *bytes.Buffer, timestamp *time.Time, severity string, caller string, message string) {
func (p *processor) writeHeader(buffer *bytes.Buffer, timestamp *time.Time, severity string, caller *string, logger *string, message string) {
buffer.WriteString(fmt.Sprintf("[%s]", timestamp.Format("2006-01-02 15:04:05.000 MST")))

buffer.WriteByte(' ')
buffer.WriteString(p.colorizeSeverity(severity).String())

buffer.WriteByte(' ')
buffer.WriteString(Gray(12, fmt.Sprintf("(%s)", caller)).String())
if logger != nil && caller != nil {
buffer.WriteByte(' ')
buffer.WriteString(Gray(12, fmt.Sprintf("(%s, %s)", *logger, *caller)).String())
} else if logger != nil {
buffer.WriteByte(' ')
buffer.WriteString(Gray(12, fmt.Sprintf("(%s)", *logger)).String())
} else if caller != nil {
buffer.WriteByte(' ')
buffer.WriteString(Gray(12, fmt.Sprintf("(%s)", *caller)).String())
}

buffer.WriteByte(' ')
buffer.WriteString(Blue(message).String())
Expand Down Expand Up @@ -442,7 +476,7 @@ func (p *processor) colorizeSeverity(severity string) aurora.Value {
color = BlueFg
}

return Colorize(severity, color)
return Colorize(strings.ToUpper(severity), color)
}

func (p *processor) unformattedPrintLine(line string, message string, args ...interface{}) {
Expand Down
18 changes: 16 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,21 @@ func TestStandardNewProduction(t *testing.T) {
`{"level":"info","ts":1545445711.144533,"caller":"c","msg":"m"}`,
},
expectedLines: []string{
"[2018-12-21 21:28:31.144 EST] \x1b[32minfo\x1b[0m \x1b[38;5;244m(c)\x1b[0m \x1b[34mm\x1b[0m",
"[2018-12-21 21:28:31.144 EST] \x1b[32mINFO\x1b[0m \x1b[38;5;244m(c)\x1b[0m \x1b[34mm\x1b[0m",
},
},
})
}

func TestStandardNewProduction2(t *testing.T) {
runLogTests(t, []logTest{
{
name: "single_log_line",
lines: []string{
`{"severity":"INFO","timestamp":"2022-04-21T14:50:18.382974069-04:00","logger":"l","message":"m"}`,
},
expectedLines: []string{
"[2022-04-21 14:50:18.382 EDT] \x1b[32mINFO\x1b[0m \x1b[38;5;244m(l)\x1b[0m \x1b[34mm\x1b[0m",
},
},
})
Expand All @@ -71,7 +85,7 @@ func TestStandardFieldTs_ISO8601_string(t *testing.T) {
`{"level":"info","ts":"2019-12-06T19:40:20.627Z","caller":"c","msg":"m"}`,
},
expectedLines: []string{
"[2019-12-06 14:40:20.627 EST] \x1b[32minfo\x1b[0m \x1b[38;5;244m(c)\x1b[0m \x1b[34mm\x1b[0m",
"[2019-12-06 14:40:20.627 EST] \x1b[32mINFO\x1b[0m \x1b[38;5;244m(c)\x1b[0m \x1b[34mm\x1b[0m",
},
},
})
Expand Down

0 comments on commit c6b911c

Please sign in to comment.