Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31897

Re: basic output question

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: basic output question
Date 2015-06-30 15:27 -0400
Organization A noiseless patient Spider
Message-ID <mmuqgk$214$1@dont-email.me> (permalink)
References <635be647-55fb-43f9-8609-0320306ca853@googlegroups.com> <450dd941-b331-4f46-9e9c-e7db4033e0ca@googlegroups.com> <mmu43k$tkm$1@dont-email.me> <91961835-9633-4c29-968d-027ce3fb9ff7@googlegroups.com>

Show all headers | View raw


On 6/30/2015 2:28 PM, e.d.programmer@gmail.com wrote:
>>> On Monday, June 29, 2015 at 2:40:05 PM UTC-4, Russ P. wrote:
>>>> Suppose I need a function (or method) for printing some (text-based) information to a file or to standard output. What is the recommended way to do this while minimizing duplication of code? Here's a simplification of what I came up with (in Scala, but using the Java standard library):
>>>>
>>>>       def printInformation(out: java.io.PrintStream = System.out) {
>>>>
>>>>           // print information here
>>>>           }
>>>>
>>>>       def printInformation(fileName: String) {
>>>>           printInformation(new java.io.PrintStream(fileName))
>>>>           }
>>>>
>>>> So if I call printInformation with no arguments, the output goes to standard output, but if I give it a filename the output goes to the file. Is this the best way to do it, or is there a simpler/better way? I'd also like to be able to append the output to an existing file as well as create a new file. Thanks.
>>>
>>> I never quite got the logging frameworks ie commons logging.
>>
>>       s/ie/eg/
>>
>>> You have to deal with someone else's API, [...]
>>
>>       That's called "component reuse," and most people think of it
>> as a Good Thing.  In moderation, of course.
>>
>>> [...] set warning levels, and include an extra jar in the classpath. [...]
>>
>>       There's also the built-in java.util.logging package, assuming you're
>> using a post-2002 Java version.
>>
>> --
>> esosman@comcast-dot-net.invalid
>> "Don't be afraid of work. Make work afraid of you." -- TLM
>
> Most people seem to be using the Apache Commons Logging.  I'm not sure why.
> I just looked at java.util.logging.Logger and most of that seems irrelevant when all you want to do is write messages to System.out.  In my static log class I just add the timestamp and calling method and line number to every output line.  I don't want to get an instance of a logger object for every line to print much less configure a handler or assign a level.

     If all you want to do is write to System.out, just do it -- don't
bother with any logging framework at all, either pre-packaged or
home-brewed.  Use a logging framework when you may want to change the
logging destination after the fact, even after deployment, and route it
to System.out/err, to a file, to a socket, to a remote log-monitoring
service, to multiple destinations simultaneously, ...

     Also, most people I've met have considered the level-sensitivity
of logging to be an advantage.  You can sprinkle the logging statements
throughout the code and turn them on and off (by level or by filter) at
run time, without recompiling or redeploying or whatever.  That way,
when trouble arises in the field you can crank up the logging level on
*exactly* the same code that's already installed.  Beats heck out of
"Hey, developer, can you send me a freshly-compiled version with a few
more println() calls in it?"

     Finally, there's no need to "get an instance of a Logger" for every
line of output.  For one thing, the Logger class caches the instances
and re-uses them.  If simply looking them up seems too expensive (as it
might well be for FINEST logging in a tight loop), just give yourself a
static field:

	public class Clown {
	    private static final Logger LOGGER =
	        Logger.getLogger(Clown.class.getName());
	    ...
	    LOGGER.info("Remember this moment, my son.");
	    ...
	}

Or you can create the Logger lazily:

	public class Clown {
	    private static Logger cachedLogger;
	    private static synchronized logger() {
	        if (cachedLogger == null) {
	            cachedLogger = Logger.getLogger(...);
	        }
	        return cachedLogger;
	    }
	    ...
	    logger().severe("That swan is black! Black, I say!");
	    ...
	}

     I haven't any personal experience with the Apache Commons logging
framework, and don't know why (or, truly, whether) people prefer it to
java.util.logging; perhaps it's better.  But even if Java's package is
less capable than log4j, it's certainly more capable and more versatile
than the home-brew implementation you've described, which makes me
wonder why you bothered.  Even if you like your own API better, I'd
suggest you re-implement it as a wrapper around one of these industrial-
strength frameworks to get the benefits of in-the-field adjustment.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

basic output question "Russ P." <russ.paielli@gmail.com> - 2015-06-29 11:39 -0700
  Re: basic output question Arne Vajhøj <arne@vajhoej.dk> - 2015-06-29 14:53 -0400
  Re: basic output question Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-06-29 15:32 -0400
  Re: basic output question e.d.programmer@gmail.com - 2015-06-30 05:45 -0700
    Re: basic output question Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-06-30 09:05 -0400
      Re: basic output question e.d.programmer@gmail.com - 2015-06-30 11:28 -0700
        Re: basic output question Arne Vajhøj <arne@vajhoej.dk> - 2015-06-30 14:55 -0400
          Re: basic output question e.d.programmer@gmail.com - 2015-06-30 12:14 -0700
            Re: basic output question Arne Vajhøj <arne@vajhoej.dk> - 2015-07-27 22:25 -0400
        Re: basic output question Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-06-30 15:27 -0400

csiph-web