3

I have created a GUI program that uses a class to perform some tasks.

I would like to add logging in both the JFrame and the class. Logs should be kept in a file and displayed in a JTextArea concurrently.

What is a convient solution for this?

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
GorillaApe
  • 3,611
  • 10
  • 63
  • 106
  • with regard to logging, try [log4j](http://logging.apache.org/log4j/1.2/) – mre May 31 '11 at 15:50
  • 1
    What have you done so far? It sounds like you need to pick your favorite Logging framework (either java.util.logging or log4j) and wrap it with some class/instance that simply writes to your `JTextArea`. – pickypg May 31 '11 at 15:51
  • Checkout http://stackoverflow.com/a/33657637/808901 – Rod Lima Nov 18 '15 at 21:20

3 Answers3

7

Create a wrapper that

  1. updates your JTextArea
  2. logs via log4j, SLF4J or Apache Commons Logging or another logging framework
public void log(String msg) {
      appendToJTextArea(msg);
      LOG.info(msg);
 }
dertkw
  • 7,798
  • 5
  • 37
  • 45
  • And why not simply "write to a file using a printstream"? – Martijn Courteaux May 31 '11 at 16:37
  • Because a logging framework is doing nothing else. PLUS you can customize it, turn it on/off easily, create customer appenders/loggers, have different loglevels, ... So why reinvent the wheel? – dertkw May 31 '11 at 16:43
4

I suggest using a simple System.out.println() call and then running your application like this:

java -cp path/to/my/class/or/jar/MyClass.class MyClass -debug > myLog.log

Which will create a text pane for you (-debug parameter), as well as put them in a log file for you.

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
  • 4
    Come on, this is ridiculous! Logging frameworks are easy to use and if you one don't know about them it's investment for the future to learn this. `System.out.println()` is ok for a HelloWorld or debugging problems with you logging framework, but not for real applications. Please look at timbooo's answer describing a wrapper class. – Thor May 31 '11 at 16:21
  • @Thor: Indeed, real applications should not work this way. But I gave a +1 because of a simple alternative, using something that already exists. I didn't know this option. And yes, I gave timbooo also a +1. – Martijn Courteaux May 31 '11 at 16:39
  • @Thor, I fully understand logging, I have made my own framework for that in objective-c. However, I meant this to be a simple way that the OP can log data, without any extra writing of code. – Richard J. Ross III Jun 09 '11 at 14:41
0

One of the ways you can log to a file easily is to use a FileWriter:
http://download.oracle.com/javase/1.4.2/docs/api/java/io/FileWriter.html

Additionally, as you log data you can always update the text of a JTextArea with JTextArea.append():
http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JTextArea.html

If you're logging to a file, might I suggest logging to multiple timestamped files? This ensures that you're saving the data and that you won't lose too much in the event of a program crash.

Hope this helped!

Jonathan Pitre
  • 2,355
  • 3
  • 22
  • 38