0

I am new to java and have come to know (somebody told me) that logger.log(LEVEL.DEBUG,message/instance), is useful in terms of tracing or to get log information in Java language I googled and saw many SO threads, but couldn't get any help, I would like to know it's uses and how to get maximum benefit from this method.

2 Answers 2

1

I would start by doing the following Google search:

logging best practices java

The first link I get is this, which explains when to use each of the log levels.

Now, to give some context, log levels are used to easily slice-and-dice through massive log files. Imagine that you have a web application that creates a log for each user's specific click actions. If that web application has thousands of users logged in at the same time to the application, you could end up with millions of lines of logs per minute. How would you figure out which log is important to you? Browsing through millions of lines isn't really wise, is it?

As a developer, you might only be interested in ERROR or WARNs. A marketing guy might be interested in the INFO logs which indicate which web pages, or sections of the web site are getting most hits, a networks guy might want to look at logs that indicate SEVERE server problems.

SLF4J also allows you to create custom MARKERS. You could add markers to logs to derive even greater business value from your logs.

To conclude, I'd say, think of logs as your best friend once your code goes to production. The better you organize your logs with levels, markers, etc., the sooner you'll be able to debug issues and respond in time.

Sign up to request clarification or add additional context in comments.

Comments

0

It sounds like someone was giving you information about Apache's logging ("DEBUG"), but the purpose behind that and the log you displayed in your post are essentially the same: that is to provide information to developers (typically) that they would find useful when reviewing the code.

There are several logging levels available in java.util.logging.Level. These are used to control the level of logging being detailed in the logged messages.

SEVERE - indicates a serious problem WARNING - typically used to denote a potential problem INFO - used for information messages CONFIG - used for static configurational messages FINE - provides some trace information FINER - provides more trace information than FINE FINEST - provides the most trace inforamtion ALL - log all messages OFF - turns the logger off

If you are looking to provide information that a developer would find interesting within your code, you would probably use FINE, FINER, or FINEST, depending on how much information you want to provide. This is not usually recommended in a production environment though, as the extra level of details can become quite spammy. Usually in such an environment, a logging level of INFO or WARN is used, and then the tracing information is enabled only when a major issue is occurring and it cannot be reproduced locally with ease.

When you call log(LEVEL, String message), the level of the message is checked against the threshold (usually set in a properties file) to see if it needs to be logged. If so, the configured appender adds posts the message where appropriate (console, file, etc...).

I also recommend using a 3rd party library such as Apache's Log4J for enhanced logging features. See org.apache.log4j.Level. It essentially provides finer grained control over the logging and uses Level terms that more properly define the level of the message being appended.

DEBUG is similar to FINE, TRACE is similar to FINEST, ERROR and FATAL are similar to SEVERE, etc...

Does that help any?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.