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

Add Zerolog formatter #50

Merged
merged 4 commits into from
Mar 4, 2025
Merged

Add Zerolog formatter #50

merged 4 commits into from
Mar 4, 2025

Conversation

zzzLobster
Copy link
Contributor

@zzzLobster zzzLobster commented Mar 3, 2025

Add Zerolog formatter

Log entry looks like this:

{
  "level": "error",
  "stack": "Oops: permission denied\n  --- at ... main()",
  "error": {
    "code": "iam_authz_missing_permission",
    "domain": "authz",
    "context": {
      "hello": "world",
      "user_id": 1234,
      "permission": "post.create"
    },
    "user": {
      "firstname": "john",
      "id": "user-123",
      "lastname": "doe"
    },
    "error": "something failed: permission denied",
    "trace": "6710668a-2b2a-4de6-b8cf-3272a476a1c9",
    "hint": "Runbook: https://doc.acme.org/doc/abcd.md",
    "time": "2025-03-03T19:53:26.338249Z"
  },
  "time": "2025-03-03T22:53:26+03:00",
  "message": "something failed: permission denied"
}

@zzzLobster
Copy link
Contributor Author

@samber , Should I raise an issue before submitting a PR?

Copy link
Owner

@samber samber left a comment

Choose a reason for hiding this comment

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

Please add it to readme as well. ✌️

(ctrl-f logrus)

Makefile Outdated
watch-test:
reflex -t 50ms -s -- sh -c 'gotest -race -v ./...'
reflex -t 50ms -s -- sh -c 'gotest -race -v ./... && go -C loggers/zerolog test -race -v ./...'
Copy link
Owner

Choose a reason for hiding this comment

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

i'm pretty sure it is not necessary, since you declared this submodule in go.work

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, there is no a straight way to do that right now.
There is a proposal for that: golang/go#71294 (accepted, but it's not a part of cmd/go so far)

go test -race -v ./...
=== RUN   TestErrorsIs
--- PASS: TestErrorsIs (0.00s)
=== RUN   TestErrorsAs
--- PASS: TestErrorsAs (0.00s)
=== RUN   TestDereferencePointers
--- PASS: TestDereferencePointers (0.00s)
=== RUN   TestOopsWrap
--- PASS: TestOopsWrap (0.00s)
=== RUN   TestOopsWrap_wrapped
--- PASS: TestOopsWrap_wrapped (0.10s)
=== RUN   TestOopsWrapf
--- PASS: TestOopsWrapf (0.00s)
=== RUN   TestOopsFromContext
--- PASS: TestOopsFromContext (0.00s)
=== RUN   TestOopsNew
--- PASS: TestOopsNew (0.00s)
=== RUN   TestOopsErrorf
--- PASS: TestOopsErrorf (0.00s)
=== RUN   TestOopsCode
--- PASS: TestOopsCode (0.00s)
=== RUN   TestOopsTime
--- PASS: TestOopsTime (0.00s)
=== RUN   TestOopsSince
--- PASS: TestOopsSince (0.01s)
=== RUN   TestOopsDuration
--- PASS: TestOopsDuration (0.01s)
=== RUN   TestOopsIn
--- PASS: TestOopsIn (0.00s)
=== RUN   TestOopsTags
--- PASS: TestOopsTags (0.00s)
=== RUN   TestOopsTx
--- PASS: TestOopsTx (0.00s)
=== RUN   TestOopsTxSpanFromOtel
--- PASS: TestOopsTxSpanFromOtel (0.00s)
=== RUN   TestOopsWith
--- PASS: TestOopsWith (0.00s)
=== RUN   TestOopsWithContext
--- PASS: TestOopsWithContext (0.00s)
=== RUN   TestOopsWithLazyEvaluation
--- PASS: TestOopsWithLazyEvaluation (0.00s)
=== RUN   TestOopsHint
--- PASS: TestOopsHint (0.00s)
=== RUN   TestOopsPublic
--- PASS: TestOopsPublic (0.00s)
=== RUN   TestOopsOwner
--- PASS: TestOopsOwner (0.00s)
=== RUN   TestOopsUser
--- PASS: TestOopsUser (0.00s)
=== RUN   TestOopsTenant
--- PASS: TestOopsTenant (0.00s)
=== RUN   TestOopsRequest
--- PASS: TestOopsRequest (0.00s)
=== RUN   TestOopsMixed
--- PASS: TestOopsMixed (0.00s)
=== RUN   TestOopsMixedWithGetters
--- PASS: TestOopsMixedWithGetters (0.00s)
=== RUN   TestOopsLogValuer
--- PASS: TestOopsLogValuer (0.00s)
=== RUN   TestOopsFormatSummary
--- PASS: TestOopsFormatSummary (0.00s)
=== RUN   TestOopsFormatVerbose
--- PASS: TestOopsFormatVerbose (0.00s)
=== RUN   TestOopsMarshalJSON
--- PASS: TestOopsMarshalJSON (0.00s)
=== RUN   TestOopsGetPublic
--- PASS: TestOopsGetPublic (0.00s)
=== RUN   TestRemoveGoPath
--- PASS: TestRemoveGoPath (0.00s)
=== RUN   TestStacktrace
--- PASS: TestStacktrace (0.00s)
PASS
ok      github.com/samber/oops  (cached)

(it does not run the new test)

go -C loggers/zerolog test -race -v ./...
=== RUN   TestZerologFormatter
--- PASS: TestZerologFormatter (0.00s)
PASS
ok      github.com/samber/oops/loggers/zerolog  (cached)

Possible one-liner (I'm not sure if that's gonna work in CI):

go list -f '{{.Dir}}' -m | xargs go test -race -v
?       github.com/samber/oops/examples/log     [no test files]
?       github.com/samber/oops/examples/panic   [no test files]
?       github.com/samber/oops/examples/logrus  [no test files]
?       github.com/samber/oops/examples/segfault        [no test files]
?       github.com/samber/oops/examples/slog    [no test files]
?       github.com/samber/oops/examples/sources [no test files]
?       github.com/samber/oops/examples/zerolog [no test files]
=== RUN   TestErrorsIs
--- PASS: TestErrorsIs (0.00s)
=== RUN   TestErrorsAs
--- PASS: TestErrorsAs (0.00s)
=== RUN   TestDereferencePointers
--- PASS: TestDereferencePointers (0.00s)
=== RUN   TestOopsWrap
--- PASS: TestOopsWrap (0.00s)
=== RUN   TestOopsWrap_wrapped
--- PASS: TestOopsWrap_wrapped (0.10s)
=== RUN   TestOopsWrapf
--- PASS: TestOopsWrapf (0.00s)
=== RUN   TestOopsFromContext
--- PASS: TestOopsFromContext (0.00s)
=== RUN   TestOopsNew
--- PASS: TestOopsNew (0.00s)
=== RUN   TestOopsErrorf
--- PASS: TestOopsErrorf (0.00s)
=== RUN   TestOopsCode
--- PASS: TestOopsCode (0.00s)
=== RUN   TestOopsTime
--- PASS: TestOopsTime (0.00s)
=== RUN   TestOopsSince
--- PASS: TestOopsSince (0.01s)
=== RUN   TestOopsDuration
--- PASS: TestOopsDuration (0.01s)
=== RUN   TestOopsIn
--- PASS: TestOopsIn (0.00s)
=== RUN   TestOopsTags
--- PASS: TestOopsTags (0.00s)
=== RUN   TestOopsTx
--- PASS: TestOopsTx (0.00s)
=== RUN   TestOopsTxSpanFromOtel
--- PASS: TestOopsTxSpanFromOtel (0.00s)
=== RUN   TestOopsWith
--- PASS: TestOopsWith (0.00s)
=== RUN   TestOopsWithContext
--- PASS: TestOopsWithContext (0.00s)
=== RUN   TestOopsWithLazyEvaluation
--- PASS: TestOopsWithLazyEvaluation (0.00s)
=== RUN   TestOopsHint
--- PASS: TestOopsHint (0.00s)
=== RUN   TestOopsPublic
--- PASS: TestOopsPublic (0.00s)
=== RUN   TestOopsOwner
--- PASS: TestOopsOwner (0.00s)
=== RUN   TestOopsUser
--- PASS: TestOopsUser (0.00s)
=== RUN   TestOopsTenant
--- PASS: TestOopsTenant (0.00s)
=== RUN   TestOopsRequest
--- PASS: TestOopsRequest (0.00s)
=== RUN   TestOopsMixed
--- PASS: TestOopsMixed (0.00s)
=== RUN   TestOopsMixedWithGetters
--- PASS: TestOopsMixedWithGetters (0.00s)
=== RUN   TestOopsLogValuer
--- PASS: TestOopsLogValuer (0.00s)
=== RUN   TestOopsFormatSummary
--- PASS: TestOopsFormatSummary (0.00s)
=== RUN   TestOopsFormatVerbose
--- PASS: TestOopsFormatVerbose (0.00s)
=== RUN   TestOopsMarshalJSON
--- PASS: TestOopsMarshalJSON (0.00s)
=== RUN   TestOopsGetPublic
--- PASS: TestOopsGetPublic (0.00s)
=== RUN   TestRemoveGoPath
--- PASS: TestRemoveGoPath (0.00s)
=== RUN   TestStacktrace
--- PASS: TestStacktrace (0.00s)
PASS
ok      github.com/samber/oops  (cached)
testing: warning: no tests to run
PASS
ok      github.com/samber/oops/loggers/logrus   (cached) [no tests to run]
=== RUN   TestZerologFormatter
--- PASS: TestZerologFormatter (0.00s)
PASS
ok      github.com/samber/oops/loggers/zerolog  (cached)
?       github.com/samber/oops/recovery/gin     [no test files]

Copy link
Owner

Choose a reason for hiding this comment

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

Oh yes, sorry. I did this in another project:

MODULES=$(shell go list -m)

test:
	go test -race -v ${MODULES} ./...

Copy link
Owner

@samber samber Mar 4, 2025

Choose a reason for hiding this comment

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

feel free to change the readme and add logrus submodule

the limit of both methods is that go.work version must be compatible with submodule

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure I precisely understand what you mean.
What readme do you mean in this case?

I propose to use your approach from that another project:

MODULES=$(shell go list -m)

build:
	go build -v ./...

test:
	go test -race -v ${MODULES} ./...
watch-test:
	reflex -t 50ms -s -- sh -c 'gotest -race -v ${MODULES} ./...'

and update other goals same way

Copy link
Owner

Choose a reason for hiding this comment

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

sorry, I mean the Makefile :-(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please take a look at the latest commit with the changes in Makefile.
Same issue is applicable to golangci-lint, the difference is that it wants directories, not packages (updated it as well and it forced me to fix a couple of files)

@zzzLobster
Copy link
Contributor Author

Please add it to readme as well. ✌️

(ctrl-f logrus)

I've updated the root README. I hope I've not missed anything

@samber
Copy link
Owner

samber commented Mar 4, 2025

Thanks!

Let's merge ;)

[edit] i'm going to make a release as soon as the other pr is done

@samber samber merged commit eeb65a7 into samber:main Mar 4, 2025
1 check passed
@zzzLobster zzzLobster deleted the feat/zerolog branch March 5, 2025 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants