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

Custom marshaller, improve stacktrace #4

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

max107
Copy link
Contributor

@max107 max107 commented Feb 11, 2024

zerolog с недавних пор умеет в multiline rs/zerolog#416, что удобно для локальной работы. В данном МР унифицировано получение / форматирование stacktrace для werr и работы с zerolog

log.Output(zerolog.ConsoleWriter{
	Out:           w,
	NoColor:       true,
	TimeFormat:    time.RFC3339,
	FieldsExclude: []string{zerolog.ErrorStackFieldName},
	FormatExtra: func(evt map[string]interface{}, buf *bytes.Buffer) error {
		if stack, ok := evt["stack"]; ok {
			buf.WriteString("\nstacktrace:\n")
			for _, line := range strings.Split(stack.(string), ",") {
				buf.WriteString(line + "\n")
			}
		}

		return nil
	},
})

пример

t.Run("pretty_stack_werr", func(t *testing.T) {
		t.Parallel()

		werr.ErrorStackMarshaler = func(err error, msg string, frames stacktrace.Frames) string {
			result := werr.UnwrapAll(err).Error()
			if len(msg) > 0 {
				result = result + " " + msg
			}

			return result
		}

		var buf bytes.Buffer

		l := logger.NewWithOut(true, zerolog.DebugLevel, &buf)
		l.Err(werrErr()).Msg("hello")

		raw, err := io.ReadAll(&buf)
		require.NoError(t, err)

		out := strings.TrimSpace(string(raw))

		require.Contains(t, out, `example error`)
		require.Contains(t, out, `ERR logger/logger_test.go:82 > hello error="example error"
stacktrace:
zerolog/log.go:377#(*Logger).Err
logger/logger_test.go:82#TestLogger.func3`)
	})
image

@max107 max107 changed the title marshaller with line number Custom marshaller, improve stacktrace Feb 11, 2024
@max107
Copy link
Contributor Author

max107 commented Feb 18, 2024

@AlexanderMint ping

@max107
Copy link
Contributor Author

max107 commented Jun 18, 2024

@AlexanderMint почему закрыл?

@max107 max107 reopened this Jun 18, 2024
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