0

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.

0

3 Answers 3

3

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.

0
3

You can use the JDK's inbuilt logger

http://docs.oracle.com/javase/6/docs/api/java/util/logging/package-summary.html

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) {
        logger.info("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>
6
  • 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
    Commented Jul 22, 2013 at 5:34
  • @dhblah Updated the answer with an example. If it's still not working, can you paste your code?
    – talonx
    Commented Jul 22, 2013 at 5:56
  • So, let's say I've added logger.info("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
    Commented Jul 28, 2013 at 7:22
  • Updated the answer.
    – talonx
    Commented Jul 28, 2013 at 8:26
  • can this "turn off" be configured programmatically?
    – dhblah
    Commented Jul 28, 2013 at 13:25
2

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.

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