Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This merges the rs/zerolog master branch into our fork #17

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
2bbbcb4
Add new FatalFunc method to logger to override behavior for Fatal-lev…
autarch Jan 10, 2024
d935b1f
Disable coverage job in Actions
autarch Jan 25, 2024
10bab96
Only run tests with Go 1.20.x
autarch Jan 25, 2024
3324071
Only run Actions on PR
autarch Jan 25, 2024
620ba86
added support for NO_COLOR (#586)
Gaardsholt Sep 5, 2023
96c0be1
Update Build Status Badge (#589)
ohbus Sep 22, 2023
7b33608
Update dependencies
rs Sep 25, 2023
7efca75
Bump actions/checkout from 3 to 4 (#588)
dependabot[bot] Sep 25, 2023
beaa022
Add Type function to context (#592)
clinta Oct 3, 2023
5eeafcf
feat: display error stack when using `.Fields()` (#560)
julienrbrt Oct 4, 2023
4f79555
Add FormatPrepare to ConsoleWriter. (#595)
mitar Oct 13, 2023
4687ac6
prettylog: add support for file input
rs Oct 13, 2023
22a4ddd
Update example image to how it is currently rendered. (#596)
mitar Oct 13, 2023
5f06f54
Make console output more readable. (#597)
mitar Oct 13, 2023
0a89e62
Do not colorize if not necessary. (#598)
mitar Oct 14, 2023
8a127bc
hlog: add HostHandler (#604)
michiomochi Oct 20, 2023
b24079d
Add Println top level method (#533)
Estavaringo Oct 20, 2023
fdf4e9b
Use map for formatted levels. (#599)
mitar Oct 20, 2023
2f7759d
Zero Logger behaves like Nop logger. (#606)
mitar Oct 21, 2023
fdfb462
chore: use buf.String() instead of string(buf.Bytes()) (#608)
testwill Oct 24, 2023
2bcded3
Add additional hlog logging handlers (#607)
mitar Nov 8, 2023
f033a3c
stop using deprecated io/ioutils package (#620) (#620)
kerolasa Nov 24, 2023
070362d
Add TriggerLevelWriter. (#602)
mitar Nov 28, 2023
0224af3
fixed documentation for tracing hook (#621)
nobodyme Nov 29, 2023
b2c6d2d
Refactor: change Hook(h Hook) to Hook(hooks ...Hook) (#629)
Halimao Dec 24, 2023
ca44f52
Add stacktrace to Context (#630)
mcdoker18 Jan 3, 2024
21264c7
Fixed failing tests (#626)
nobodyme Jan 4, 2024
0dc4c75
add: ContextLogger Interface LogObjectMarshaler (#623)
Hyuga-Tsukui Jan 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
on: [push, pull_request]
on: pull_request
name: Test
jobs:
test:
strategy:
matrix:
go-version: [1.18.x, 1.21.x]
go-version: [1.20.x]
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
Expand All @@ -13,7 +13,7 @@ jobs:
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
- uses: actions/cache@v3
with:
path: ~/go/pkg/mod
Expand All @@ -24,13 +24,13 @@ jobs:
run: go test -race -bench . -benchmem ./...
- name: Test CBOR
run: go test -tags binary_log ./...
coverage:
runs-on: ubuntu-latest
steps:
- name: Update coverage report
uses: ncruces/go-coverage-report@main
with:
report: 'true'
chart: 'true'
amend: 'true'
continue-on-error: true
# coverage:
# runs-on: ubuntu-latest
# steps:
# - name: Update coverage report
# uses: ncruces/go-coverage-report@main
# with:
# report: 'true'
# chart: 'true'
# amend: 'true'
# continue-on-error: true
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Zero Allocation JSON Logger

[![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/rs/zerolog) [![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/rs/zerolog/master/LICENSE) [![Build Status](https://travis-ci.org/rs/zerolog.svg?branch=master)](https://travis-ci.org/rs/zerolog) [![Go Coverage](https://github.com/rs/zerolog/wiki/coverage.svg)](https://raw.githack.com/wiki/rs/zerolog/coverage.html)
[![godoc](http://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/rs/zerolog) [![license](http://img.shields.io/badge/license-MIT-red.svg?style=flat)](https://raw.githubusercontent.com/rs/zerolog/master/LICENSE) [![Build Status](https://github.com/rs/zerolog/actions/workflows/test.yml/badge.svg)](https://github.com/rs/zerolog/actions/workflows/test.yml) [![Go Coverage](https://github.com/rs/zerolog/wiki/coverage.svg)](https://raw.githack.com/wiki/rs/zerolog/coverage.html)

The zerolog package provides a fast and simple logger dedicated to JSON output.

Expand Down Expand Up @@ -547,7 +547,7 @@ and facilitates the unification of logging and tracing in some systems:
type TracingHook struct{}

func (h TracingHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
ctx := e.Ctx()
ctx := e.GetCtx()
spanId := getSpanIdFromContext(ctx) // as per your tracing framework
e.Str("span-id", spanId)
}
Expand Down
20 changes: 10 additions & 10 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package zerolog
import (
"context"
"errors"
"io/ioutil"
"io"
"net"
"testing"
"time"
Expand All @@ -15,7 +15,7 @@ var (
)

func BenchmarkLogEmpty(b *testing.B) {
logger := New(ioutil.Discard)
logger := New(io.Discard)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Expand All @@ -25,7 +25,7 @@ func BenchmarkLogEmpty(b *testing.B) {
}

func BenchmarkDisabled(b *testing.B) {
logger := New(ioutil.Discard).Level(Disabled)
logger := New(io.Discard).Level(Disabled)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Expand All @@ -35,7 +35,7 @@ func BenchmarkDisabled(b *testing.B) {
}

func BenchmarkInfo(b *testing.B) {
logger := New(ioutil.Discard)
logger := New(io.Discard)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Expand All @@ -45,7 +45,7 @@ func BenchmarkInfo(b *testing.B) {
}

func BenchmarkContextFields(b *testing.B) {
logger := New(ioutil.Discard).With().
logger := New(io.Discard).With().
Str("string", "four!").
Time("time", time.Time{}).
Int("int", 123).
Expand All @@ -60,7 +60,7 @@ func BenchmarkContextFields(b *testing.B) {
}

func BenchmarkContextAppend(b *testing.B) {
logger := New(ioutil.Discard).With().
logger := New(io.Discard).With().
Str("foo", "bar").
Logger()
b.ResetTimer()
Expand All @@ -72,7 +72,7 @@ func BenchmarkContextAppend(b *testing.B) {
}

func BenchmarkLogFields(b *testing.B) {
logger := New(ioutil.Discard)
logger := New(io.Discard)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
Expand Down Expand Up @@ -102,7 +102,7 @@ func BenchmarkLogArrayObject(b *testing.B) {
obj1 := obj{"a", "b", 2}
obj2 := obj{"c", "d", 3}
obj3 := obj{"e", "f", 4}
logger := New(ioutil.Discard)
logger := New(io.Discard)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
Expand Down Expand Up @@ -224,7 +224,7 @@ func BenchmarkLogFieldType(b *testing.B) {
return e.Object("k", objects[0])
},
}
logger := New(ioutil.Discard)
logger := New(io.Discard)
b.ResetTimer()
for name := range types {
f := types[name]
Expand Down Expand Up @@ -358,7 +358,7 @@ func BenchmarkContextFieldType(b *testing.B) {
return c.Timestamp()
},
}
logger := New(ioutil.Discard)
logger := New(io.Discard)
b.ResetTimer()
for name := range types {
f := types[name]
Expand Down
1 change: 0 additions & 1 deletion binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"

// "io/ioutil"
stdlog "log"
"time"
)
Expand Down
40 changes: 34 additions & 6 deletions cmd/prettylog/prettylog.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"bufio"
"errors"
"fmt"
"io"
"os"
Expand All @@ -14,13 +16,39 @@ func isInputFromPipe() bool {
}

func main() {
if !isInputFromPipe() {
fmt.Println("The command is intended to work with pipes.")
fmt.Println("Usage: app_with_zerolog | 2> >(prettylog)")
writer := zerolog.NewConsoleWriter()

if isInputFromPipe() {
_, _ = io.Copy(writer, os.Stdin)
} else if len(os.Args) > 1 {
for _, filename := range os.Args[1:] {
// Scan each line from filename and write it into writer
r, err := os.Open(filename)
if err != nil {
fmt.Printf("%s open: %v", filename, err)
os.Exit(1)
}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
_, err = writer.Write(scanner.Bytes())
if err != nil {
if errors.Is(err, io.EOF) {
break
}
fmt.Printf("%s write: %v", filename, err)
os.Exit(1)
}
}
if err := scanner.Err(); err != nil {
fmt.Printf("%s scan: %v", filename, err)
os.Exit(1)
}
}
} else {
fmt.Println("Usage:")
fmt.Println(" app_with_zerolog | 2> >(prettylog)")
fmt.Println(" prettylog zerolog_output.jsonl")
os.Exit(1)
return
}

writer := zerolog.NewConsoleWriter()
_, _ = io.Copy(writer, os.Stdin)
}
60 changes: 35 additions & 25 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ type ConsoleWriter struct {
FormatErrFieldValue Formatter

FormatExtra func(map[string]interface{}, *bytes.Buffer) error

FormatPrepare func(map[string]interface{}) error
}

// NewConsoleWriter creates and initializes a new ConsoleWriter.
Expand Down Expand Up @@ -124,6 +126,13 @@ func (w ConsoleWriter) Write(p []byte) (n int, err error) {
return n, fmt.Errorf("cannot decode event: %s", err)
}

if w.FormatPrepare != nil {
err = w.FormatPrepare(evt)
if err != nil {
return n, err
}
}

for _, p := range w.PartsOrder {
w.writePart(buf, evt, p)
}
Expand Down Expand Up @@ -272,7 +281,7 @@ func (w ConsoleWriter) writePart(buf *bytes.Buffer, evt map[string]interface{},
}
case MessageFieldName:
if w.FormatMessage == nil {
f = consoleDefaultFormatMessage
f = consoleDefaultFormatMessage(w.NoColor, evt[LevelFieldName])
} else {
f = w.FormatMessage
}
Expand Down Expand Up @@ -310,8 +319,13 @@ func needsQuote(s string) bool {
return false
}

// colorize returns the string s wrapped in ANSI code c, unless disabled is true.
// colorize returns the string s wrapped in ANSI code c, unless disabled is true or c is 0.
func colorize(s interface{}, c int, disabled bool) string {
e := os.Getenv("NO_COLOR")
if e != "" || c == 0 {
disabled = true
}

if disabled {
return fmt.Sprintf("%s", s)
}
Expand Down Expand Up @@ -373,27 +387,16 @@ func consoleDefaultFormatLevel(noColor bool) Formatter {
return func(i interface{}) string {
var l string
if ll, ok := i.(string); ok {
switch ll {
case LevelTraceValue:
l = colorize("TRC", colorMagenta, noColor)
case LevelDebugValue:
l = colorize("DBG", colorYellow, noColor)
case LevelInfoValue:
l = colorize("INF", colorGreen, noColor)
case LevelWarnValue:
l = colorize("WRN", colorRed, noColor)
case LevelErrorValue:
l = colorize(colorize("ERR", colorRed, noColor), colorBold, noColor)
case LevelFatalValue:
l = colorize(colorize("FTL", colorRed, noColor), colorBold, noColor)
case LevelPanicValue:
l = colorize(colorize("PNC", colorRed, noColor), colorBold, noColor)
default:
l = colorize(ll, colorBold, noColor)
level, _ := ParseLevel(ll)
fl, ok := FormattedLevels[level]
if ok {
l = colorize(fl, LevelColors[level], noColor)
} else {
l = strings.ToUpper(ll)[0:3]
}
} else {
if i == nil {
l = colorize("???", colorBold, noColor)
l = "???"
} else {
l = strings.ToUpper(fmt.Sprintf("%s", i))[0:3]
}
Expand All @@ -420,11 +423,18 @@ func consoleDefaultFormatCaller(noColor bool) Formatter {
}
}

func consoleDefaultFormatMessage(i interface{}) string {
if i == nil {
return ""
func consoleDefaultFormatMessage(noColor bool, level interface{}) Formatter {
return func(i interface{}) string {
if i == nil || i == "" {
return ""
}
switch level {
case LevelInfoValue, LevelWarnValue, LevelErrorValue, LevelFatalValue, LevelPanicValue:
return colorize(fmt.Sprintf("%s", i), colorBold, noColor)
default:
return fmt.Sprintf("%s", i)
}
}
return fmt.Sprintf("%s", i)
}

func consoleDefaultFormatFieldName(noColor bool) Formatter {
Expand All @@ -445,6 +455,6 @@ func consoleDefaultFormatErrFieldName(noColor bool) Formatter {

func consoleDefaultFormatErrFieldValue(noColor bool) Formatter {
return func(i interface{}) string {
return colorize(fmt.Sprintf("%s", i), colorRed, noColor)
return colorize(colorize(fmt.Sprintf("%s", i), colorBold, noColor), colorRed, noColor)
}
}
Loading
Loading