****Error LoggerUtil(String propsFile, String className): java.io.FileNotFoundException: \usr\share\apache-tomcat-7.0.25\webapps\ExtSolutions\logs\defaultLog.log (El sistema no puede hallar la ruta especificada)
No se puede crear fichero log: \usr\share\apache-tomcat-7.0.25\webapps\ExtSolutions\logs
Donde LoggerUtil, es un Servlet creado con estas caracteristicas:
package util;
import java.io.*;
import java.util.Properties;
import javax.naming.InitialContext;
public class LoggerUtil{
private File logFile;
private String logFName;
private String logDName;
private File logDir;
private long logMaxSize;
private RandomAccessFile logRAF;
private int level = 0;
public final static int LOG_ERROR = 1;
public final static int LOG_INFO = 2;
public final static int LOG_DEBUG = 3;
public final static int LOG_IGNORE = 4;
public String className = "";
private synchronized void configure(String props, String className){
try{
InputStream is = new FileInputStream(props);
Properties fileProps = new Properties();
fileProps.load(is);
// Definicion de las propiedades del Log.
//Obtenemos las propiedades por defecto por si no hay propiedades para una clase en concreto
String defaultFName = fileProps.getProperty("defaultLogFileName");
String defaultDName = fileProps.getProperty("defaultLogDir");
String defaultMaxSize = fileProps.getProperty("defaultLogMaxLength");
String defaultLogLevel = fileProps.getProperty("defaultLogLevel");
this.logFName = fileProps.getProperty(className+".logFileName", defaultFName);
this.logDName = fileProps.getProperty(className+".logDir", defaultDName);
this.logMaxSize = Long.parseLong(fileProps.getProperty(className+".l ogMaxLength", defaultMaxSize));
String logLevel = fileProps.getProperty(className+".logLevel", defaultLogLevel);
if(logLevel.equals("ALL")){
this.level = 4;
}else if(logLevel.equals("DEBUG")){
this.level = 3;
}else if(logLevel.equals("INFO")){
this.level = 2;
}else if(logLevel.equals("ERROR")){
this.level = 1;
}else if(logLevel.equals("NONE")){
this.level = 0;
}
}catch(Exception e){
System.out.println("***Error al obtener las propiedades del fichero LOG (LoggerUtil.configure): "+e);
e.printStackTrace();
}
}
public LoggerUtil(String className) {
/*
String fileConfigLog=null;
try{
InitialContext ctx = new InitialContext();
fileConfigLog = (String) ctx.lookup("java:comp/env/config/logs");
}catch(Exception e){
System.out.println("initLog(): ****Error al incializar Contenido Log:"+e);
}
*/
try {
String fileConfigLog=null;
File f = new File(LoggerUtil.class.getResource("/").toURI());
String logs = f.getParentFile().getParentFile().getAbsolutePath( );
fileConfigLog=logs+"/logs/ConfigLogs.properties";
configure(fileConfigLog,className);
Calendario ahora = new Calendario();
this.logDir = new File(logDName);
this.className = className;
logFile = new File(logDir, logFName);
logRAF = new RandomAccessFile(logFile,"rw");
if (logRAF.length() == 0) {
logRAF.writeBytes(ahora.getFechaFormato("dd.MM.yy HH:mm:ss") + " " + "Log creado\r\n");
}
logRAF.close();
}catch (IOException e) {
System.out.println("****Error LoggerUtil(String propsFile, String className): "+e);
System.out.println("No se puede crear fichero log: " + logDir);
//throw new IOException("No se puede crear fichero log: " + logDir);
}catch (Exception e){
System.out.println("****Error LoggerUtil(String propsFile, String className): "+e);
}
}
public synchronized void write(String msg,int logLevel) throws IOException {
try {
if(this.level >= logLevel){
logRAF = new RandomAccessFile(logFile, "rw");
if (logRAF.length() >= logMaxSize) {
logRAF.close();
this.rotate();
logRAF = new RandomAccessFile(logFile, "rw");
}
logRAF.seek(logRAF.length());
Calendario ahora = new Calendario();
msg = ahora.getFechaFormato("dd.MM.yy HH:mm:ss") + " " + msg;
logRAF.writeBytes(msg + "\r\n");
logRAF.close();
}
}catch (IOException e) {
System.out.println("****Error write(String msg,int logLevel): "+e);
System.out.println("****Error write(String msg,int logLevel) TRAZA: "+msg);
throw new IOException("Log Error");
}catch (Exception e){
System.out.println("****Error write(String msg,int logLevel): "+e);
System.out.println("****Error write(String msg,int logLevel) TRAZA: "+msg);
}
}
public synchronized void write(Exception e) throws IOException {
try {
logRAF = new RandomAccessFile(logFile, "rw");
if (logRAF.length() >= logMaxSize) {
logRAF.close();
this.rotate();
logRAF = new RandomAccessFile(logFile, "rw");
}
logRAF.seek(logRAF.length());
Calendario ahora = new Calendario();
String msg = ahora.getFechaFormato("dd.MM.yy HH.mm:ss") + " " + e.getMessage();
logRAF.writeBytes(msg + "\r\n");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
PrintWriter p = new PrintWriter(bytes, true);
e.printStackTrace(p);
logRAF.writeBytes(bytes.toString());
FileOutputStream fout = new FileOutputStream( "error" );
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fout));
logRAF.close();
}catch (IOException ioe) {
System.out.println("****Error write(Exception e): "+ioe);
System.out.println("****Error write(Exception e) TRAZA: "+e);
throw new IOException("Log Error");
}catch (Exception ex){
System.out.println("****Error write(Exception e): "+ex);
System.out.println("****Error write(Exception e) TRAZA: "+e);
}
}
public synchronized void rotate() throws IOException {
try {
Calendario ahora = new Calendario();
String hoy = "";
// Generamos el formato AAAAMMDDHHMMSS
hoy = ahora.getFechaFormato("yyyyMMddHHmmss");
// Formateamos el nuevo nombre del log
File oldLogFile = new File(logDir, logFName + "." + hoy + ".rot");
// delete older log file
if (oldLogFile.exists()) {oldLogFile.delete();}
// rename the actual one to "*.log_old"
logFile.renameTo(oldLogFile);
// create a new one
logRAF = new RandomAccessFile(logFile, "rw");
logRAF.writeBytes(ahora.getFechaFormato("dd.MM.yy HH:mm:ss") + " " + "Log creado\r\n");
logRAF.close();
}catch (IOException e) {
System.out.println("****Error rotate(): "+e);
throw new IOException("Log Error");
}catch (Exception ex){
System.out.println("****Error rotate(): "+ex);
}
}
/**
* Método que genera una traza de informaciín en el log.
* @param msg Texto a imprimir.
*/
public void infoLog(String msg){
try {
write("INFO: "+className+"."+msg,LoggerUtil.LOG_INFO);
} catch (IOException e){
System.out.println("Error al escribir el log "+className+e+" MSG: "+msg);
}
}
/**
* Método que genera una traza de error en el log.
* @param msg Texto a imprimir.
*/
public void errorLog(String msg){
try {
write("ERROR: "+className+"."+msg,LoggerUtil.LOG_ERROR);
} catch (IOException e){
System.out.println("Error al escribir el log "+className+" MSG: "+msg);
}
}
/**
* Método que genera una traza de debug en el log.
* @param msg Texto a imprimir.
*/
public void debugLog(String msg){
try {
write("DEBUG: "+this.className+"."+msg,LoggerUtil.LOG_DEBUG) ;
} catch (IOException e){
System.out.println("Error al escribir el log "+className+e+" MSG: "+msg);
}
}
} //END EventLog
Alguien me puede ayudar con este problema, si no haya la ruta especifica no va funcionar mi sistema
