Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

When I'm coding small projects, I often use System.out.println to print valuable info. It's simple, easy and gets me info. The only drawback I found is that there is no simple way to disable parts of this output for finished application, except for commenting them out, which dirties my code. So, I d like to know, is there any logging techniques, which are as simple as System.out.println, but can be enabled/disabled somehow? Without additional xml configs and such. Because everything I've looked on, looked too much complex and just not worth effort for a small (1-2-3 screens size) application.

Thanks in advance.

share|improve this question

marked as duplicate by gnat, MichaelT, Florian Margaine, Dan Pichelman, GlenH7 Jul 21 '13 at 17:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

There is middle ground between System.out.println and a logging library: write your own object that shields the details. This object can be configured with a simple system property:

class LogUtils {
    private boolean isEnabled = false;

    // TODO: constructor 

    public static void log(String msg) {
        if (isEnabled) { System.out.println(msg); }

The danger here is that you may want to add features such as log levels (INFO, DEBUG, ERROR). If you add features, you're on the path of "reinventing the wheel" and should consider a proper logging library.

share|improve this answer
nice idea, thanks – dhblah Jul 22 '13 at 5:39

You can use the JDK's inbuilt logger

It's very simple to use and configure, yet flexible (e.g. logging levels) when you want it to be. Also, you don't have to include any third party libraries.

This example shows two ways of using the log method

import java.util.logging.Level;
import java.util.logging.Logger;


private static final Logger logger = Logger.getLogger(Test.class.getName());

    private void setup(String val1) {"Entering method with value [" + val1 + "]");
        //Do something here
        try {
        //Do something here
        } catch (Exception e) {
            logger.log(Level.SEVERE, "Failed to prune the hedge", e);

The logging levels are arranged according to a hierarchy - enabling logging for a specific Level implies logging will be done for that level and "higher" levels. E.g. enabling Level.INFO will log all messages logged with INFO, WARNING and SEVERE messages.

If you wish to turn off logging at a specific level, you can configure this in a properties file. E.g. a configuration file with only console output and logging enabled for Level.WARNING and above will look like this

handlers = java.util.logging.ConsoleHandler
.level = WARNING
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

This file needs to be included with a system property when you run your program

   java -Djava.util.logging.config.file -cp <your classpath> <your main class>
share|improve this answer
Can you please elaborate, how to use it easiest way? I mean, well, I tried to use it, so I created logger instance from current class. then used log method, but nothing appears on console. – dhblah Jul 22 '13 at 5:34
@dhblah Updated the answer with an example. If it's still not working, can you paste your code? – talonx Jul 22 '13 at 5:56
So, let's say I've added"something"); to print some data into output stream while I'm developing code. How can I turn off that print in final version of my program? – dhblah Jul 28 '13 at 7:22
Updated the answer. – talonx Jul 28 '13 at 8:26
can this "turn off" be configured programmatically? – dhblah Jul 28 '13 at 13:25

A small project might grow and the logging will become unwieldily if not properly taken care of. It will not be a hassle to setup a logger if you have a configuration that you preferred handy. Initially, it could take a bit of time to configure the logger to your whim but subsequently it is just copy the configuration into to the right project directory.

You can checkout these few popular logging tools out there, Log4j and Logback. You can use a logger abstractor like SLF4J where you can switch between different logger implementations. This will save time when comes a situation where only a specific logging library is provided or allowed. In that instance, you only configure the logger abstractors like SLF4J to use the available logger.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.