{"id":13566393,"url":"https://github.com/rust-cli/env_logger","last_synced_at":"2025-05-12T15:33:02.403Z","repository":{"id":40945636,"uuid":"96382888","full_name":"rust-cli/env_logger","owner":"rust-cli","description":"A logging implementation for `log` which is configured via an environment variable.","archived":false,"fork":false,"pushed_at":"2025-04-03T12:07:37.000Z","size":699,"stargazers_count":923,"open_issues_count":40,"forks_count":139,"subscribers_count":14,"default_branch":"main","last_synced_at":"2025-04-23T17:19:14.448Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://docs.rs/env_logger","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rust-cli.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2017-07-06T03:07:16.000Z","updated_at":"2025-04-23T07:41:32.000Z","dependencies_parsed_at":"2024-02-12T16:53:35.727Z","dependency_job_id":"11263e88-3f5a-4c7b-8e10-a05af605fc26","html_url":"https://github.com/rust-cli/env_logger","commit_stats":{"total_commits":473,"total_committers":76,"mean_commits":6.223684210526316,"dds":0.6109936575052854,"last_synced_commit":"dc1a01a79729d9a43f9dfaf32080c5e7bdf05090"},"previous_names":[],"tags_count":60,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rust-cli%2Fenv_logger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rust-cli%2Fenv_logger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rust-cli%2Fenv_logger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rust-cli%2Fenv_logger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rust-cli","download_url":"https://codeload.github.com/rust-cli/env_logger/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250477820,"owners_count":21437049,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T13:02:08.680Z","updated_at":"2025-04-23T17:19:21.390Z","avatar_url":"https://github.com/rust-cli.png","language":"Rust","readme":"# env_logger\n\n[![crates.io](https://img.shields.io/crates/v/env_logger.svg)](https://crates.io/crates/env_logger)\n[![Documentation](https://docs.rs/env_logger/badge.svg)](https://docs.rs/env_logger)\n\nImplements a logger that can be configured via environment variables.\n\n## Usage\n\n### In libraries\n\n`env_logger` makes sense when used in executables (binary projects). Libraries should use the [`log`](https://docs.rs/log) crate instead.\n\n### In executables\n\nIt must be added along with `log` to the project dependencies:\n\n```console\n$ cargo add log env_logger\n```\n\n`env_logger` must be initialized as early as possible in the project. After it's initialized, you can use the `log` macros to do actual logging.\n\n```rust\nuse log::info;\n\nfn main() {\n    env_logger::init();\n\n    info!(\"starting up\");\n\n    // ...\n}\n```\n\nThen when running the executable, specify a value for the **`RUST_LOG`**\nenvironment variable that corresponds with the log messages you want to show.\n\n```bash\n$ RUST_LOG=info ./main\n[2018-11-03T06:09:06Z INFO  default] starting up\n```\n\nThe letter case is not significant for the logging level names; e.g., `debug`,\n`DEBUG`, and `dEbuG` all represent the same logging level. Therefore, the\nprevious example could also have been written this way, specifying the log\nlevel as `INFO` rather than as `info`:\n\n```bash\n$ RUST_LOG=INFO ./main\n[2018-11-03T06:09:06Z INFO  default] starting up\n```\n\nSo which form should you use? For consistency, our convention is to use lower\ncase names. Where our docs do use other forms, they do so in the context of\nspecific examples, so you won't be surprised if you see similar usage in the\nwild.\n\nThe log levels that may be specified correspond to the [`log::Level`][level-enum]\nenum from the `log` crate. They are:\n\n   * `error`\n   * `warn`\n   * `info`\n   * `debug`\n   * `trace`\n\n[level-enum]:  https://docs.rs/log/latest/log/enum.Level.html  \"log::Level (docs.rs)\"\n\nThere is also a pseudo logging level, `off`, which may be specified to disable\nall logging for a given module or for the entire application. As with the\nlogging levels, the letter case is not significant.\n\n`env_logger` can be configured in other ways besides an environment variable. See [the examples](https://github.com/rust-cli/env_logger/tree/main/examples) for more approaches.\n\n### In tests\n\nTests can use the `env_logger` crate to see log messages generated during that test:\n\n```console\n$ cargo add log\n$ cargo add --dev env_logger\n```\n\n```rust\nfn add_one(num: i32) -\u003e i32 {\n    info!(\"add_one called with {}\", num);\n    num + 1\n}\n\n#[cfg(test)]\nmod tests {\n    use super::*;\n    use log::info;\n\n    fn init() {\n        let _ = env_logger::builder().is_test(true).try_init();\n    }\n\n    #[test]\n    fn it_adds_one() {\n        init();\n\n        info!(\"can log from the test too\");\n        assert_eq!(3, add_one(2));\n    }\n\n    #[test]\n    fn it_handles_negative_numbers() {\n        init();\n\n        info!(\"logging from another test\");\n        assert_eq!(-7, add_one(-8));\n    }\n}\n```\n\nAssuming the module under test is called `my_lib`, running the tests with the\n`RUST_LOG` filtering to info messages from this module looks like:\n\n```bash\n$ RUST_LOG=my_lib=info cargo test\n     Running target/debug/my_lib-...\n\nrunning 2 tests\n[INFO my_lib::tests] logging from another test\n[INFO my_lib] add_one called with -8\ntest tests::it_handles_negative_numbers ... ok\n[INFO my_lib::tests] can log from the test too\n[INFO my_lib] add_one called with 2\ntest tests::it_adds_one ... ok\n\ntest result: ok. 2 passed; 0 failed; 0 ignored; 0 measured\n```\n\nNote that `env_logger::try_init()` needs to be called in each test in which you\nwant to enable logging. Additionally, the default behavior of tests to\nrun in parallel means that logging output may be interleaved with test output.\nEither run tests in a single thread by specifying `RUST_TEST_THREADS=1` or by\nrunning one test by specifying its name as an argument to the test binaries as\ndirected by the `cargo test` help docs:\n\n```bash\n$ RUST_LOG=my_lib=info cargo test it_adds_one\n     Running target/debug/my_lib-...\n\nrunning 1 test\n[INFO my_lib::tests] can log from the test too\n[INFO my_lib] add_one called with 2\ntest tests::it_adds_one ... ok\n\ntest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured\n```\n\n## Configuring log target\n\nBy default, `env_logger` logs to stderr. If you want to log to stdout instead,\nyou can use the `Builder` to change the log target:\n\n```rust\nuse std::env;\nuse env_logger::{Builder, Target};\n\nlet mut builder = Builder::from_default_env();\nbuilder.target(Target::Stdout);\n\nbuilder.init();\n```\n\n## Stability of the default format\n\nThe default format won't optimise for long-term stability, and explicitly makes no guarantees about the stability of its output across major, minor or patch version bumps during `0.x`.\n\nIf you want to capture or interpret the output of `env_logger` programmatically then you should use a custom format.\n","funding_links":[],"categories":["Rust"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frust-cli%2Fenv_logger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frust-cli%2Fenv_logger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frust-cli%2Fenv_logger/lists"}