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

feat: allow pretty print logging in tests #5354

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

hstove
Copy link
Contributor

@hstove hstove commented Oct 22, 2024

This allows STACKS_LOG_PP=1 to be used in tests.

We previously had two make_logger functions - one for tests and one for not tests. The only differences were:

  • The tests logger didn't allow pretty printing
  • The tests logger uses slog_term::TestStdoutWriter

This PR makes it so that we only have one make_logger function. Inside of that function, I've added a flag to use TestStdoutWriter in tests.

@hstove hstove requested a review from a team as a code owner October 22, 2024 17:30
Comment on lines 224 to 227
#[cfg(not(any(test, feature = "testing")))]
let decorator = slog_term::PlainSyncDecorator::new(std::io::stderr());
#[cfg(any(test, feature = "testing"))]
let decorator = slog_term::PlainSyncDecorator::new(slog_term::TestStdoutWriter);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the canonical way to do this is a little cleaner:

let decorator = {
    #[cfg(not(any(test, feature = "testing")))]
    {
        slog_term::PlainSyncDecorator::new(std::io::stderr())
    }
    #[cfg(any(test, feature = "testing"))]
    {
        slog_term::PlainSyncDecorator::new(slog_term::TestStdoutWriter)
    }
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even better would be to put the entire declaration block into its own function, and have one function for testing and one for prod.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, I pulled it out into its own function 👍🏼

@hstove hstove requested a review from obycode October 24, 2024 14:31
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.

3 participants