taoensso / timbre

Pure Clojure/Script logging library
https://www.taoensso.com/timbre
Eclipse Public License 1.0
1.44k stars 171 forks source link

First log message ever is very slow, >5 seconds, rest are normal #323

Closed pjlegato closed 3 years ago

pjlegato commented 3 years ago

I'm trying out Timbre as per the instructions in the README, just trying to log to STDOUT or STDERR, nothing fancy. I did the project.clj dependency to [com.taoensso/timbre "5.1.0"]. It works fine, except that the very first call to log something takes >5 seconds. The rest then seem normal.

Did I miss some critical configuration step somewhere?

test.core> (time (timbre/warn "foo"))
2020-10-20T22:54:20.005Z UnknownHost WARN [test.core:46] - foo
"Elapsed time: 5017.544778 msecs"
nil
test.core> (time (timbre/warn "foo"))
2020-10-20T22:54:28.421Z UnknownHost WARN [test.core:50] - foo
"Elapsed time: 1.059658 msecs"
nil
test.core> (time (timbre/warn "foo"))
2020-10-20T22:54:30.019Z UnknownHost WARN [test.core:54] - foo
"Elapsed time: 0.88344 msecs"
nil
ptaoussanis commented 3 years ago

@pjlegato Hi Paul, this'll be because of the "UnknownHost" you're seeing there.

Timbre will attempt to fetch your Hostname, and try for up to 5 seconds before giving up. Then the result is cached for a minute.

If you can fix whatever's causing the Hostname lookup to block and fail, then this delay will go away.

Cheers!

pjlegato commented 3 years ago

Thanks for the pointer.

It appears that MacOS (Sierra and later) do a reverse DNS lookup by default when java.net.InetAddress/getLocalHost is called. Someone made a tester at https://github.com/thoeni/inetTester .

As a workaround, you can add your hostname to /etc/hosts manually, as per https://stackoverflow.com/questions/39636792/jvm-takes-a-long-time-to-resolve-ip-address-for-localhost/39698914#39698914 . This solved my immediate issue. I'm not sure whether there's a better underlying solution.

There appear to be many similar issues with other JVM projects, e.g. https://github.com/spring-projects/spring-boot/issues/7087

ptaoussanis commented 3 years ago

Thanks for adding the extra info Paul 👍

It's not obvious to me that there's anything Timbre could or should try do from its end about this, so I'm inclined to close and suggest that others running into the same issue take the same steps (to add a hostname to /etc/hosts).

But you or anyone else reading: please feel free to reopen if you have a practical idea of something that Timbre could do to help blunt this sharp edge.

Cheers! :-)