Create builders for Metadata, Record, and Level. #200
No reviewers
Labels
No labels
bug
duplicate
easy
enhancement
help wanted
invalid
question
rfc
structured-logging
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-rust-lang/log!200
Loading…
Reference in a new issue
No description provided.
Delete branch "record_metadata_construction"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Fixes #116.
Create builders to support the construction of
Metadata,Record, andLevel. I decided to takemut selfas the parameter for each builder method because it allows:Whereas taking
&mut selfleads to the temporary object created byRecordBuilder::new()being dropped beforebuild().Not done yet--still need to add examples/docs. Please review everything else!
@ -632,0 +638,4 @@/// The source file containing the message.pub fn file(&self) -> &str {self.fileNit: can you add a blank line here?
@ -632,0 +638,4 @@/// The source file containing the message.pub fn file(&self) -> &str {self.fileExample is missing closing backticks.
@omh1280 thank you.
This all looks right to me with the exception that the builder pattern we tend to prefer is with
&mut selfinstead ofself. Unless there's a exceptional reason to useselfhere can you switch them to&mut self?cc @alexcrichton can you peek at this? Do you agree that the builders should be by ref?
@ -632,0 +759,4 @@}/// Invoke the builder and return a `Record`pub fn build(&self) -> Record<'a> {Ditto about the blank line and missing backticks.
Please add blank line here too.
Ah yeah I agree that these should be
&mut selfacross the boardGotcha. I'll update next time I get a chance!
Fixed!
CopyandCloneto preventcannot move out of borrowed contentin thebuildfunctions. Is this OK? Is there a better way to do this?RecordBuilder::newneeds to take a reference toLocationbecause it cannot create a default which has the appropriate lifetime.Recordcontains a reference toLocationinstead of owning it?Thanks for the review 😄
Thanks @omh1280!
ping @alexcrichton
Oh, I guess I can continue the review myself...
In this case, this seems fine. I don't see a reason for Record and Metadata not to be Copy/Clone. @alexcrichton what you think? An alternative would be for build to take
self, but it looks to me like we prefer for builders to be reusable when possible.Yes.
I suspect this a subtle efficiency matter. This type is passed around during logging and copying one pointer is faster than copying the entire struct.
This patch looks fine to me now, but I'd be more confident if @alexcrichton took another look.
I think we may want to remove
Copyas it may be a bit restrictive, but addingCloneto these types seems fine by me!Some other points I'd have:
RecordBuilder::newperhaps we could also have something likeRecord::builder()? I think @sfackler has pioneered this pattern in the past and it can sometimes help the ergonomics of importing less items.Thanks Alex! Let me know if it still needs work (or if you want me to rebase to clean up commits)
Thanks @omh1280! @sfackler I'm curious, what do you think about the 3 builders here vs trying to collapse them all into one? Or perhaps having one builder with methods that delegate to the others?
These defaults are not going to be particularly useful - I'd probably lean towards making them empty strings.
I can't actually remember why
Locationexists as a separate construct fromRecordI'd probably say we just inline those fields?It does seem nice to have forwarding methods from the
Recordbuilder to theMetadatabuilder though.To confirm:
__module_path,__file, and__lineintoRecordand scrapLocation? (and change macros etc.)RecordBuildercan build entirely withoutMetadataBuilder?Yep!
If we're changing the macros, we may want to change the
__logfunction to be publicly exposed and take aLogRecorddirectly, now that you can construct that with the public API.Sorry for the long wait--I'm in the middle of doing this I promise :)
Updated!
This was a pretty big change, please let me know if I forgot something.
Locationcompletelymodule_path,line, andfileintoRecordtargetandlevelfunctions inRecordBuildertoMetadataBuilderYou now don't have to
useany builder structs:I didn't make
__logpublic: I can do that if that's agreed on /cc @alexcrichtonThis looks great to me! I'll leave @sfackler to sign off though.
@omh1280 it looks like there may be some CI failures as well?
Didn't see that--fixed!
Looks good to me but it needs a rebase.
Squashed + rebased