How to manually roll a log file using JDK Logging

I have an application that uses JDK Logging with a logging.properties file that configures several old log files through java.util.logging.FileHandler.count.

In some application cases, I would like to start manual rollover of the log file in order to start a new log file, for example. before starting a scheduled action.

Is this possible with JDK Logging?

In Log4j I use the following, however in this case I would like to use JDK Logging!

Logger logger = Logger.getRootLogger(); Enumeration<Object> appenders = logger.getAllAppenders(); while(appenders.hasMoreElements()) { Object obj = appenders.nextElement(); if(obj instanceof RollingFileAppender) { ((RollingFileAppender)obj).rollOver(); } } 
+1
source share
3 answers

You will need to write your own handler to make rotation work. Something like JBoss Log Manager works with JDK registration and just replaces the parts of logmanger. It already has several different rotating handlers .

+2
source

You can trigger the rotation by creating a FileHandler roll with a zero limit and without adding.

new FileHandler(pattern, 0, count, false).close();

  • Remove and close the existing FileHandler
  • Create and close the FileHandler rotation.
  • Create and add a FileHandler using the default settings.

Otherwise, you can resort to using reflection:

  Method m = FileHandler.class.getDeclaredMethod("rotate"); m.setAccessible(true); if (!Level.OFF.equals(f.getLevel())) { //Assume not closed. m.invoke(f); } 
+3
source
 RollingFileHandler fileHandler = new RollingFileHandler(path); fileHandler.setFormatter( newFormatter ); fileHandler.setLevel(level); logger.addHandler(fileHandler); import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.logging.ErrorManager; import java.util.logging.LogRecord; import java.util.logging.StreamHandler; public class RollingFileHandler extends StreamHandler { private static String dateFormat = "yyyy-MM-dd"; //default private String path = null; // Standard-Log-Puffer (vergrâßert sich on Demand) static ByteArrayOutputStream bas = new ByteArrayOutputStream(1024*200*25); /** * Constructor. */ public RollingFileHandler() { super(); setOutputStream(bas); // Write old data in Multithread-Environment flush(); } public RollingFileHandler(String path) { this.path = path; } /** * Overwrites super. */ public synchronized void publish(LogRecord record) { if (!isLoggable(record)) { return; } super.publish(record); } @Override public synchronized void flush() { // Puffer ist leer if (bas.size() == 0) { return; } super.flush(); File file = null; try { String dateString = null; SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.getDefault()); dateString = sdf.format(new Date()); String fileName = "dummyFile_" + dateString + ".log"; try { // Auf den Servern mapping, bei lokalem Test am Arbeitsplatz brauche ich kein Logfile-Mapping boolean windows = System.getProperty("os.name").toLowerCase().indexOf("win") >= 0 ; if (!windows && path != null) { File unixLogDir = new File(path); if (unixLogDir.exists()) { // Versuchen in das neue Directory zu speichern File logfile = new File (path + "/" + fileName); if (!logfile.exists()) { logfile.createNewFile(); } file = logfile; } } } catch(Exception e) { e.printStackTrace(); } if (file == null) { // Fallback - Umzug hat nicht geklappt file = new File(fileName); if (!file.exists()) { file.createNewFile(); } } FileOutputStream fos = new FileOutputStream(file,true); bas.flush(); bas.writeTo(fos); bas.reset(); fos.close(); } catch (Exception fnfe) { reportError(null, fnfe, ErrorManager.GENERIC_FAILURE); fnfe.printStackTrace(System.err); setOutputStream(System.out); //fallback stream try { bas.writeTo(System.out); } catch (IOException e) { // Da kann man nichts machen } } } 

}

-1
source

All Articles