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

How to add log caller info in a fixed position for pretty logging style #34

Closed
lday0321 opened this issue Feb 7, 2018 · 4 comments
Closed

Comments

@lday0321
Copy link

lday0321 commented Feb 7, 2018

i'm try to output log with pretty logging style:

logger := zerolog.New(os.Stdout).With().Timestamp().Logger().Output(zerolog.ConsoleWriter{Out: os.Stdout})

logger.Info().Str("foo", "bar").Msg("Hello world")

and the output will be pretty good as:

2018-02-07T15:09:21+08:00 |INFO| Hello world foo=bar

is it possible to add caller info, such as file name, line no info at a fixed position , such as:

2018-02-07T15:09:21+08:00 |INFO|main.go|74| Hello world foo=bar

i try to add hook for logger to parse the caller info:

func (h CallerHook) Run(e *zerolog.Event, level zerolog.Level, msg string) {
	if _, file, line, ok := runtime.Caller(3); ok {
		e.Str("file", path.Base(file)).Int("line", line)
	}
}

logger := zerolog.New(os.Stdout).With().Timestamp().Logger().Hook(CallerHook{}).Output(zerolog.ConsoleWriter{Out: os.Stdout})

obviously, the file/line info are output as normal fields, such as:

2018-02-07T15:16:35+08:00 |INFO| Hello world file=man.go foo=bar func=main line=74
@rs
Copy link
Owner

rs commented Feb 7, 2018

What do you mean by fixed position?

@rs rs closed this as completed in 27e0a22 Feb 7, 2018
@lday0321
Copy link
Author

lday0321 commented Feb 8, 2018

Sorry for bad explanation. I mean output log with file name and line no like this format:

2018-02-07T15:09:21+08:00 |INFO|main.go|74| Hello world foo=bar
2018-02-07T15:09:21+08:00 |INFO|main.go|75| Hello world foo=bar
2018-02-07T15:09:21+08:00 |INFO|main.go|76| Hello world foo=bar

@rs
Copy link
Owner

rs commented Feb 8, 2018

You'll have to customize the ConsoleWriter for that.

@lday0321
Copy link
Author

lday0321 commented Feb 8, 2018

ok, i will try it. thanks.

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

No branches or pull requests

2 participants