0

I want to export the log of my app in a html file,until now the log is displayed on the console of eclipse. in all my classes logs are defined by private static Logger logger = Logger.getLogger (classname.class.getName ());

      does anyone know how I can do this in java?
JV_MISR
  • 25
  • 8
  • Instead of e.printStackTrace() make a Scanner method and write 'e' into a file. – slanecek Oct 31 '13 at 09:41
  • what I want is to capture all the console log because I have several classes, therefore the trace of the execution of these classes must be added to this file – JV_MISR Oct 31 '13 at 09:48

2 Answers2

1

Re-configure a particular logger:

private static final Logger LOGGER
  = Logger.getLogger(ClassName.class.getName());
static
{
  try
  {
    LOGGER.addHandler(new FileHandler("mylog.xml"));
    // if you don’t want additional console output:
    LOGGER.setUseParentHandlers(false);
  } catch(IOException ex)
  {
    throw new ExceptionInInitializerError(ex);
  }
}

Or change the global configuration:

  1. Create a properties file like this:

    handlers=java.util.logging.FileHandler
    java.util.logging.FileHandler.pattern=mylog2.xml
    # add more options if you like
    
  2. Run your application with -Djava.util.logging.config.file=<path to the file above>.


In either case:

Study http://docs.oracle.com/javase/7/docs/api/java/util/logging/LogManager.html and http://docs.oracle.com/javase/7/docs/technotes/guides/logging/overview.html

Holger
  • 285,553
  • 42
  • 434
  • 765
0

You may do that using Log4j or some library.

You can export as XML for example with http://logging.apache.org/chainsaw/

There must be something similar with HTML, have a look at http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/HTMLLayout.html

Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85
  • Can you explain *why* he should use a third-party library instead of Java’s built-in logging API? – Holger Oct 31 '13 at 10:15
  • This is just my opinion, not the only solution, we could talk about this for hours as has been done here: http://stackoverflow.com/questions/31840/java-logging-vs-log4j – Christophe Roussy Oct 31 '13 at 10:20
  • There might be dozens of reasons to use such a library but is there a reason regarding the *question* you are answering here? – Holger Oct 31 '13 at 11:54
  • It is just one possible example of logging as HTML, I cannot list all possible examples. The user did not request to achieve this without any external libraries and I do not see any package names either. – Christophe Roussy Oct 31 '13 at 12:08