pop icon indicating copy to clipboard operation
pop copied to clipboard

Public pop.SetLogger is unusable because it accepts a private type

Open mpontillo opened this issue 4 years ago • 2 comments

I was trying to get my [POP] logging to print to stderr rather than the default of stdout. I created a custom logging function with the same parameters as the original, as follows:

var popStderrLogger = func(lvl logging.Level, s string, args ...interface{}) {
...
}

Then I called the function as follows:

	pop.SetLogger(popStderrLogger)

However, this results in:

cannot use popStderrLogger (type func("github.com/gobuffalo/pop/logging".Level, string, ...interface {})) as type pop.logger in argument to pop.SetLogger

I then tried casting the function to the private type as well; I didn't expect it to work, but it shows what I believe is the true cause of the error:

	pop.SetLogger(popStderrLogger.(pop.logger))
cannot refer to unexported name pop.logger

mpontillo avatar Feb 09 '21 23:02 mpontillo

Can the Logger type be made public so that callers can use it, or am I missing a better way to do this?

If I could simply pass in a stderr based logger to use instead of replicating the entire function, that would also work.

mpontillo avatar Feb 09 '21 23:02 mpontillo

While I agree with you that the logger interface should be public (for documentation purposes and more), you should still be able to set a custom logger without any code changes.

Your popStderrLogger just seems to use the wrong type for logging.Level:

github.com/gobuffalo/pop/logging.Level

instead of

github.com/gobuffalo/pop/v5/logging.Level

fasmat avatar Nov 23 '21 13:11 fasmat

(For someone who faced a similar issue, and seeing this issue)

While #622's direction could be considerable, the issue's title is not true.

SetLogger(func(logging.Level, string, ...interface{}) works fine and exporting the variable logger is not necessary. (the real reason of the issue was already explained above comment)

The proper usages are something like:

	pop.SetLogger(func(lvl logging.Level, s string, args ...interface{}) {
		fmt.Println("XXX LOG ---", lvl, s, args)
	})

or

var poplog = func(lvl logging.Level, s string, args ...interface{}) {
	fmt.Println("XXX LOG ---", lvl, s, args)
}

<...>
	pop.SetLogger(poplog)

No type assertion to pop.logger is required.

sio4 avatar Sep 17 '22 15:09 sio4