Help with getCurrentLogger() and setCurrentLogger()

I’m new to Juce, and I am currently working through Getting Started with JUCE by Martin Robinson. The first example in chapter 3 aims to log “Hello world!” to the console with this code:

Logger* log = Logger::getCurrentLogger();
String message = “Hello world!”;

However, nothing logs to the console. I added an if statement to check if getCurrentLogger() is returning a nullptr and, sure enough, it is.

Has there been a change in the JUCE framework that would cause this issue (the book is from 2012)? How do I go about setting the current logger so it isn’t a nullptr? I understand this is basic stuff, but having trouble finding a solution via Google.


I think this is fairly straight forward: JUCE: Logger Class Reference

Call ::setCurrentLogger and the beginning of your app (and for good measures ::setCurrentLogger(nullptr) before shutdown, I think there is also an assert for it).

JUCE has a pretty decent implementation ready that logs into a file: JUCE: FileLogger Class Reference
Make sure to create that in a unique_ptr. ::setCurrentLogger won’t take ownership. If you don’t destruct the file logger properly, you might end up with some hard to track bugs where log messages falling through at the applications shutdown time.

I think under debug builds, ::writeToLog always also logs into the console.

Awesome. Thanks for taking the time to answer.

If that is the same book available from Amazon as the first Google result, that seems to now be over 10 years old, so lot’s of stuff in that is going to be out of date.

It is and I’m starting to realize just how out of date it is. Any resources you’d recommend other than the tutorials on the JUCE website?

You need awesome-juce:

I have cloned all of these repos and update them every week, watching the progress on a huge set of JUCE projects out there in open-source land … and it is without doubt one of the nicest ways to keep abreast of JUCE development. Watching everyone elses progress is also very inspirational…

1 Like

wow, thank you