package org.salamanca.commands;
import java.util.logging.Logger;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import org.jdoinstruments.boot.JDOinstrumentsLogFormatter;
import org.salamanca.broker.*;
import java.util.logging.Level;
import javax.jdo.*;
import java.io.IOException;
/**
* <p>Title: </p>
*
* <p>Description: Esa clase es simplemente un decorador
* de la cualquier ICommand para agregar la funcionalidad
* de realizar un archivo log con los comandos ejecutados </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class CommandLogger implements ICommand {
private static String lineSeparator = System.getProperty("line.separator");
ICommand actualCommand = null;
private static String logFilePath;
static boolean inicializado = false;
/**
* CommandLogger
*
* @param actualCommand ICommand
*/
public CommandLogger(ICommand actualCommand) {
this.actualCommand = actualCommand;
}
/**
* getCommandName
*
* @return String
*/
public String getCommandName() {
return actualCommand.getCommandName();
}
/**
* getBarDelimitedParameters
*
* @return String
*/
public String getBarDelimitedParameters() {
return actualCommand.getBarDelimitedParameters();
}
/**
* execute
*
* @throws Exception
*/
public void execute() throws Exception {
try {
log(Level.FINE, actualCommand.getCommandName() + "--" +
actualCommand.getBarDelimitedParameters());
actualCommand.execute();
} catch (Exception ex) {
log(Level.SEVERE, ex.fillInStackTrace());
throw ex;
}
}
/**
* inicializa el sistema de log , solo lo debe hacer una vez
*
* @throws SecurityException
* @throws IOException
*/
private static void init() throws SecurityException, IOException {
if (!inicializado) {
PersistenceManagerFactory pmf = BrokerServer.instance().getPMF();
//seteo el logger
int limit = 100000; // 1 Mb
int cantArchivos = 10;
logFilePath=System.getProperty("user.dir") +
"/salamanca0.log";
Handler fh = new FileHandler(System.getProperty("user.dir") +
"/salamanca%g.log", limit, limit, true);
fh.setFormatter(new JDOinstrumentsLogFormatter());
Logger logger = Logger.getLogger("salamanca");
logger.addHandler(fh);
Logger.getLogger("salamanca").setLevel(Level.FINE);
inicializado = true; // lo seteo a true para que no lo vuelva a inicializar
}
}
/**
* log
*
* @param level Level
* @param thw Throwable
*/
public static synchronized void log(Level level, Throwable thw) throws
IOException, SecurityException {
init();
String str = "";
str = thw.getMessage();
for (int i = 0; i < thw.getStackTrace().length; i++) {
StackTraceElement sTrace = thw.getStackTrace()[i];
str = str + lineSeparator + " " + sTrace.toString();
}
Logger.getLogger("salamanca").log(level, str);
}
/**
* log
*
* @param level Level
* @param thw Throwable
*/
public static synchronized void log(Level level, String str) throws
IOException, SecurityException {
init();
Logger.getLogger("salamanca").log(level, str);
}
public static String getLogFilePath() {
return logFilePath;
}
}