Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Local Development Environments Exploring Your IDE Running Your Code

Matthew Francis
Matthew Francis
6,967 Points

Newbie - What is logging?

Craig mentioned about the usage of logging, I've read more about it and came accross this:

Logging is the process of writing log messages during the execution of a program to a central place. This logging allows you to report and persist error and warning messages as well as info messages (e.g., runtime statistics) so that the messages can later be retrieved and analyzed.

Without getting into too much details, how does logging makes de-bugging/persisting errors easier(I'm guessing it's related to the log lvls..)? and when should you log something? an example would be appreciated!

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! This is a really simplified example, but let's give it a shot! Imagine for a moment that you have a program and its sole purpose it to go through a loop and add up the numbers 1 through 5. At the end of this process it will print out the result of this addition which is 15.

The code you write compiles with no errors. It runs perfectly with the very large exception that the number that is printed out is 120. But because there are no compile errors you might not immediately recognize the mistake. What you can do is put in a command that will log the value that you're looking for inside the execution of the loop. So every iteration you can check the value of that variable and see if it's what you think it is.

After watching this, you'll realize that you simply made a mathematical (but perfectly valid) mistake. Instead of adding all the values, you were multiplying them which is why you received a 120.

Hope this helps! :sparkles:

Matthew Francis
Matthew Francis
6,967 Points

Thanks!

I realized that it can get messy if you put log codes everywhere. Is it good practice to delete logs after you are done checking and want to give your code to another individual?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Matthew Francis I would say yes, just to keep your code clean and concise. The logging in most cases is for your own benefit and during testing. Once you work out the problem, you can remove them. And being that they're very simple to set up, if another person inherits your code and something is amiss they can set up their own logs :smiley: