Skip to content

feat: logging filter #1336

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

Merged
merged 16 commits into from
Nov 19, 2024
Merged

feat: logging filter #1336

merged 16 commits into from
Nov 19, 2024

Conversation

muhmud
Copy link
Contributor

@muhmud muhmud commented Oct 10, 2024

Allows you to filter the log messages that are outputted.

Should help with this

shouldLog := true
filter, ok := env["DIRENV_LOG_FILTER"]
if ok {
filterRegEx, err := regexp.Compile(filter)
Copy link
Member

Choose a reason for hiding this comment

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

If you want to use it on both sides, then this has to use the same regexp engine as bash. I believe it's PCRE.

Alternatively, it would be acceptable to introduce a direnv log command to invoke on the stdlib side.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added the direnv log command, and made stdlib call it

stdlib.sh Outdated
Comment on lines 136 to 143
local msg=$* color_normal=''
if [[ -t 2 ]]; then
color_normal="\e[m"
fi
local -r direnv_msg=$(direnv log "$msg" 2>&1)
if [[ -n "$direnv_msg" ]]; then
printf "${color_normal}%s\n" "${direnv_msg}" >&2
fi
Copy link
Member

Choose a reason for hiding this comment

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

Could you move the rest of the logic so that this function can be reduced to:

log_status() {
  direnv log -status -- "$*"
}

And port the log_error() too, and then finally, both sides will be fully in sync.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, sounds good

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've made the changes; the only issue was the shade of red used, as it was different in stdlib.sh ("\e[38;5;1m") as compared to the colour in go (\033[31m).

I used the colour in go.

@zimbatm
Copy link
Member

zimbatm commented Oct 27, 2024

thanks, I went a bit further and moved the config to the config file, but also broke something in the process. I might push that out as a follow-up PR instead, unless you also want to help out with this.

@muhmud
Copy link
Contributor Author

muhmud commented Oct 27, 2024

Sure, I don't mind taking a look

@muhmud
Copy link
Contributor Author

muhmud commented Nov 4, 2024

I think I fixed the issue: it wasn't applying the default format if there was no TOML config file, yay for tests!

This reverts commit fa89e53.

Logs are already sent to stderr
@muhmud
Copy link
Contributor Author

muhmud commented Nov 19, 2024

Are we good to merge?

@zimbatm zimbatm merged commit 6a666fb into direnv:master Nov 19, 2024
14 checks passed
@zimbatm
Copy link
Member

zimbatm commented Nov 19, 2024

yup, thank you

jparise added a commit to jparise/direnv that referenced this pull request Dec 26, 2024
These new configuration fields were recently introduced in direnv#1336 and
will be included in the next minor version (presumably v2.36.0).

This makes it clearer that these options aren't available in older
released versions.

The annotation format is the same as what is used for 'load_dotenv'.
zimbatm pushed a commit that referenced this pull request Mar 25, 2025
These new configuration fields were recently introduced in #1336 and
will be included in the next minor version (presumably v2.36.0).

This makes it clearer that these options aren't available in older
released versions.

The annotation format is the same as what is used for 'load_dotenv'.
9999years added a commit to 9999years/nixpkgs that referenced this pull request May 9, 2025
`direnv` 2.36.0 [changed how logging worked][1]. As a result, the
`$DIRENV_LOG_FORMAT` variable is no longer set when the `nix-direnv`
`direnvrc` is sourced. Previously, setting `$DIRENV_LOG_FORMAT` to the
empty string was [the recommended solution for disabling all log
output][2]. Therefore, `nix-direnv`'s warnings are completely silenced
when using the latest `direnv`.

`nix-direnv` 3.0.7 fixes this issue.

Warnings being silenced leads to very surprising behavior and output.
We use an `.envrc` that looks like this:

    nix_direnv_manual_reload
    use flake

`nix_direnv_manual_reload` means that `use flake` is ignored unless a
cache already exists. It's supposed to print a warning making this
clear, which looks like this:

    $ direnv allow
    direnv: loading ~/my-repo/.envrc
    direnv: using flake
    direnv: nix-direnv: cache does not exist. use "nix-direnv-reload" to create it
    direnv: export ~PATH

However, with the latest `direnv`, it prints this output instead:

    $ direnv allow
    direnv: loading ~/my-repo/.envrc
    direnv: using flake
    direnv: export ~PATH

This is very misleading; it prints `using flake` as if it's loading the
Flake, but actually nothing is happening and the Flake is being ignored
entirely!

[1]: direnv/direnv#1336
[2]: direnv/direnv#68 (comment)
FilipTLW pushed a commit to FilipTLW/nixpkgs that referenced this pull request May 10, 2025
`direnv` 2.36.0 [changed how logging worked][1]. As a result, the
`$DIRENV_LOG_FORMAT` variable is no longer set when the `nix-direnv`
`direnvrc` is sourced. Previously, setting `$DIRENV_LOG_FORMAT` to the
empty string was [the recommended solution for disabling all log
output][2]. Therefore, `nix-direnv`'s warnings are completely silenced
when using the latest `direnv`.

`nix-direnv` 3.0.7 fixes this issue.

Warnings being silenced leads to very surprising behavior and output.
We use an `.envrc` that looks like this:

    nix_direnv_manual_reload
    use flake

`nix_direnv_manual_reload` means that `use flake` is ignored unless a
cache already exists. It's supposed to print a warning making this
clear, which looks like this:

    $ direnv allow
    direnv: loading ~/my-repo/.envrc
    direnv: using flake
    direnv: nix-direnv: cache does not exist. use "nix-direnv-reload" to create it
    direnv: export ~PATH

However, with the latest `direnv`, it prints this output instead:

    $ direnv allow
    direnv: loading ~/my-repo/.envrc
    direnv: using flake
    direnv: export ~PATH

This is very misleading; it prints `using flake` as if it's loading the
Flake, but actually nothing is happening and the Flake is being ignored
entirely!

[1]: direnv/direnv#1336
[2]: direnv/direnv#68 (comment)
@safareli
Copy link

For future readers:

if you have output like this

direnv: loading ~/dev/my/repo/.envrc
direnv: export +BLA +BLA +BLA +BLA +BLA

and want to only see loading and not all exported env vars which takes lot of real estate this is what workes:

[global]
log_filter="^loading"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants