borntyping / rust-simple_logger

A rust logger that prints all messages with a readable output format.
https://crates.io/crates/simple_logger
MIT License
220 stars 48 forks source link

Errors from example code #47

Closed kief closed 2 years ago

kief commented 2 years ago

Hi,

I'm new to Rust, so may be missing something obvious to more experienced folks.

I created a project following the usage example, but get the error use of undeclared crate or module 'log' when I run cargo run. When I run cargo run --example init I get the error no example target named 'init'.

My project has a fail src/main.rs with a copy/paste of the example code:

use simple_logger::SimpleLogger;

fn main() {
    SimpleLogger::new().init().unwrap();

    log::warn!("This is an example message.");
}

My Cargo.toml is:

[package]
name = "example"
version = "0.1.0"
edition = "2021"

[dependencies]
simple_logger = "1.16.0"

When I run cargo run:

$ cargo run
    Updating crates.io index
   Compiling libc v0.2.112
   Compiling log v0.4.14
   Compiling cfg-if v1.0.0
   Compiling time-macros v0.2.3
   Compiling itoa v0.4.8
   Compiling lazy_static v1.4.0
   Compiling atty v0.2.14
   Compiling colored v1.9.3
   Compiling time v0.3.5
   Compiling simple_logger v1.16.0
   Compiling example v0.1.0 (/Users/kief/projects/rust/example)
error[E0433]: failed to resolve: use of undeclared crate or module `log`
 --> src/main.rs:6:5
  |
6 |     log::warn!("This is an example message.");
  |     ^^^ use of undeclared crate or module `log`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `example` due to previous error

Is the example is missing something (like a use statement) that a more experienced Rustacian would know to include? I've tried adding use 'log' and including a log crate in my Cargo.toml, but then I get entirely different error messages. I figured I should make sure I'm on the right track with the example and not going down a completely different rabbithole.

The example documentation also suggests running cargo run --example init. When I do this, I get this

$ cargo run --example init
error: no example target named `init`

Again, this smells like I'm missing a "Rust 101" concept. Clues would be appreciated!

borntyping commented 2 years ago

I think you maybe just need to add log to your [dependencies] in Cargo.toml, but my rust knowledge is very outdated these days.

kief commented 2 years ago

When I do that, I get the thread 'main' panicked at 'Could not determine the UTC offset on this system. error. I've tried Googling that error (and seen there was an issue that was fixed in the current version of simple_logger), and it sounds like something else going on. If it doesn't seem like I'm doing something obviously wrong in using the example code then I guess I can close this issue and dig into it.

borntyping commented 2 years ago

That's progress, at least the code compiles now! That second error actually is a known issue in the time crate, which means unfortunately the default example doesn't work in all contexts.

There should be a bit more detail to the error message - you're likely either running in a multi-threaded environment or on an OS the time crate does not support. I don't think cargo run --example ... uses threads by default, so it's likely that you're using an OS that time doesn't support (sys/local_offset_at/mod.rs in time)

There's a list of workarounds for simple_logger, depending on how you'd prefer to deal with the issue (mostly taken from https://github.com/borntyping/rust-simple_logger/issues/43).

jakewan commented 2 years ago

@kief I thought I recognized that username! Thank you for Infrastructure as Code! 😄

kief commented 2 years ago

Thanks @jakewan, glad you found it useful! And thanks @borntyping. The error message doesn't give much more detail than your suggestions (MacOS 11.6.1):

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.08s
     Running `target/debug/example`
thread 'main' panicked at 'Could not determine the UTC offset on this system. Possible causes are that the time crate does not implement "local_offset_at" on your system, or that you are running in a multi-threaded environment and the time crate is returning "None" from "local_offset_at" to avoid unsafe behaviour. See the time crate's documentation for more information. (https://time-rs.github.io/internal-api/time/index.html#feature-flags): IndeterminateOffset', /Users/kief/.cargo/registry/src/github.com-1ecc6299db9ec823/simple_logger-1.16.0/src/lib.rs:409:85
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

I'll find a way to work around it. Clearly not an issue with simple_logger, so, . Thanks!

borntyping commented 2 years ago

UTC timestamps are now the default in simple_logger 2.0.0.