import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
public class DynamicLoggerConfigurator implements PropertyChangeListener
{
final static public String PN_DYN_LOG_CONFIG_DELAY = "logger_reconfig_delay";
final static public String PN_DYN_LOG_CONFIG_PERIOD = "logger_reconfig_period";
private Logger m_logger = null;
private byte[] m_configContent = null;
private static final Logger logger = Logger.getLogger("TEST-LOGGER");
public void initLoggingConfiguration(Properties props, final Logger logger)
{
String numStr = props.getProperty(PN_DYN_LOG_CONFIG_DELAY, "180");
long delay = 180;
try
{
delay = Long.parseLong(numStr.trim());
}
catch (NumberFormatException e)
{
logger.warning("logger_reconfig_period needs to have just digits but is:"+numStr);
}
numStr = props.getProperty(PN_DYN_LOG_CONFIG_PERIOD, "120");
long period = 120;
try
{
period = Long.parseLong(numStr.trim());
}
catch (NumberFormatException e)
{
logger.warning("logger_reconfig_period needs to have just digits but is:"+numStr);
}
Timer logConfigTimer = new Timer(true);
logConfigTimer.schedule(new TimerTask()
{
public void run()
{
try
{
String fname = System.getProperty("java.util.logging.config.file");
if (fname == null)
{
fname = System.getProperty("java.home");
if (fname == null)
{
throw new Error("Can't find java.home ");
}
File f = new File(fname, "lib");
f = new File(f, "logging.properties");
fname = f.getCanonicalPath();
}
System.out.println("logging config file='"+fname+"'");
InputStream in = new FileInputStream(fname);
BufferedInputStream bin = new BufferedInputStream(in);
int initSize = m_configContent==null ? 4069 : Math.max(4069, m_configContent.length);
ByteArrayOutputStream baos = new ByteArrayOutputStream(initSize);
BufferedOutputStream bos = new BufferedOutputStream(baos,initSize);
int c;
while ((c = bin.read()) != -1)
{
bos.write(c);
}
bos.flush();
byte[] configContent = baos.toByteArray();
boolean isContentEqual = true;
if (m_configContent == null)
{
// everything is fine since it is the first look on the file
// logically the content is identical
m_configContent = configContent;
}
else if (m_configContent.length==configContent.length)
{
for (int i=0; i<m_configContent.length && isContentEqual; i++)
{
if(configContent[i]!=m_configContent[i])
{
isContentEqual = false;
}
}
}
else
{
isContentEqual = false;
}
if (!isContentEqual)
{
m_configContent = configContent;
LogManager.getLogManager().readConfiguration();
System.out.println("new Config should have been read");
}
else
{
System.out.println("Config has not been changed");
}
}
catch (IOException ioEx)
{
logger.log(Level.SEVERE, "Logger could not reread Configuration", ioEx);
}
catch (Throwable th)
{
logger.log(Level.SEVERE, "generell Problem occured:", th);
}
}
},
delay*1000,
period*1000);
LogManager.getLogManager().addPropertyChangeListener(this);
m_logger = logger;
System.out.println("Logger-Object="+m_logger);
}
public void propertyChange(PropertyChangeEvent evt)
{
System.out.println("Logger-Object="+m_logger);
m_logger.info("Logging-Properties have changed");
}
static public void main(String args[])
{
String propsFileName = args.length>0 ? args[0] : null;
Properties props = new Properties();
try
{
if (propsFileName!=null)
{
props.load(new BufferedInputStream(new FileInputStream(propsFileName)));
}
else
{
props.setProperty(PN_DYN_LOG_CONFIG_DELAY, "5");
props.setProperty(PN_DYN_LOG_CONFIG_PERIOD, "5");
}
DynamicLoggerConfigurator configurator = new DynamicLoggerConfigurator();
configurator.initLoggingConfiguration(props, logger);
while (true)
{
logger.severe("test1");
logger.warning("test2");
logger.info("test3");
logger.fine("test4");
logger.finer("test5");
logger.finest("test6");
Thread.sleep(3000);
}
}
catch (Throwable th)
{
th.printStackTrace();
}
}
}