-
Notifications
You must be signed in to change notification settings - Fork 122
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
Make it easy to define a slightly different format (without writing a formatter from scratch) #48
Comments
I agree, it would be nice to be able to tweak the default format without having to start from scratch if all you want to do is disable timestamps, and I don't think a few Having said that, I'm not against this. I think it'll be nice and easy to discover and use. |
how about a format string? something like https://github.com/vitiral/strfmt |
@aep Format strings have the disadvantage that the programmer can write incorrect syntax. Therefore you'd need to return a format_builder()
.level()
.msg() // a space is added between level and msg automatically
.str(" [")
.mod_path()
.str("]")
// Results in something like:
// INFO this is a log message [test::main] This could be cooperated into the already existing builder. Internally it would have to build up some kind of simple data structure, like a Sure, a format string might be easier to read; at least for some. I personally dislike most format strings, because one always has to remember a new magic syntax (or rather: look it up every time I need to write a format string). But this is just an idea I wanted to propose. |
@LukasKalbertodt builder patterns are nice! |
Personally, I'm more inclined to simple methods like |
@KodrAus I'm fine with just a few simple methods, too! So which methods on the builder do we need? How about?
I guess disabling the actual log message doesn't make any sense in the general case (and if a user wants it, a custom formatter can solve that for them). Is disabling the level equally useless in most cases? |
@LukasKalbertodt those all sound good to me 👍 it's another candidate for an environment variable too. I think it might be a bit weird to disable the level, but maybe if the user is just using For usage, I was thinking it might look something like: let mut builder = Builder::new();
builder.format(DefaultFormat::new().with_timestamp(false).build()); So we have a specific We could put these on the builder directly: let mut builder = Builder::new();
builder.with_timestamp(false); My only concern with that is that those methods only make sense when you use the default format, so scoping them to the format they apply to might be clearer than: let mut builder = Builder::new();
// this doesn't make sense
builder.format(|buf, record| { ... }).with_timestamp(false); I'm not fully sold either way though, maybe we could solve this with naming, What do you think? |
I haven't thought about the problem of using both Maybe you could generalize the |
A generalised format method that doesn't require extra method calls sounds good. I thought we'd run into inference problems with closure arguments by adding indirection, but it looks like we can work around that just fine. EDIT: Actually we can just use a trait. Not sure why I thought that wouldn't work, it must've been an |
Sounds good to me! |
Ah so there is a problem with inferring the types of the closure when we're not explicitly binding a generic to one of the
So I think we are back to adding a |
@KodrAus Is your code online somewhere? Or could you write some minimal example? Would be curious to play with the code. I had hoped this should be possible ... |
@LukasKalbertodt Sure! I've pushed what I've done to a branch. We can make this work if we add a separate method for the default format, or we require you call |
So I'm thinking the easiest way forward here is to put the methods on the
It's a bit of a mouthful but it should be clear that if you call Does that sound reasonable? |
The next question is whether |
@KodrAus Sorry for the late response :( I'm not quite in the loop about I don't quite like the methods on the If breaking changes are still possible, maybe something like the following would be ok?
|
Unfortunately the ship has sailed on After going through the process of upgrading a few crates to the new version I've actually come around to the |
@KodrAus yeah, you're problably right. Then maybe this crate should really go the easy way for now. If the next breaking version bump happens, one can still think about a better way to handle this ^_^ |
This has been rolled in to the |
Hi there!
While it is possible to define a custom formatting via the
Builder
, often one only wants to adjust small things. For example, the timestamp is way too noisy for my application, so I want to disable it. But now I have to do everything else by myself. Especially right now I cannot use colors, so I cannot implement the formatting I want. It would be nice to say something likebuilder.with_timestamps(false)
. Sure, this would introduce one (or a few, if we want to add options for other parts -- like the module path -- as well) boolean flag that has to be checked for each log message. But that should be fine, right?The text was updated successfully, but these errors were encountered: