Java Code Examples for org.apache.log4j.FileAppender#getFile()

The following examples show how to use org.apache.log4j.FileAppender#getFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Log4jLoggerAdapter.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public Log4jLoggerAdapter() {
	try {
		org.apache.log4j.Logger logger = LogManager.getRootLogger();
           if (logger != null) {
               Enumeration<Appender> appenders = logger.getAllAppenders();
               if (appenders != null) {
                   while (appenders.hasMoreElements()) {
                       Appender appender = appenders.nextElement();
                       if (appender instanceof FileAppender) {
                           FileAppender fileAppender = (FileAppender)appender;
                           String filename = fileAppender.getFile();
                           file = new File(filename);
                           break;
                       }
                   }
               }
           }
       } catch (Throwable t) {
       }
}
 
Example 2
Source File: SystemLogPage.java    From nextreports-server with Apache License 2.0 6 votes vote down vote up
public SystemLogPage() {
	super();
	
	WebMarkupContainer cssContainer = new WebMarkupContainer("cssPath");
       cssContainer.add(new AttributeModifier("href", ThemesManager.getInstance().getThemeRelativePathCss()));
       add(cssContainer);
	
	FileAppender appender = (FileAppender) LogManager.getRootLogger().getAppender("FILE");
	File logFile = new File(appender.getFile());
	
	String content;
	try {
		content = FileUtils.readFileToString(logFile);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		content = e.toString();
	}
	
	add(new Label("size", FileUtils.byteCountToDisplaySize(logFile.length())));
	add(new Label("lastModified", new Date(logFile.lastModified()).toString()));
	add(new MultiLineLabel("log", content));
}
 
Example 3
Source File: LogPageHandler.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public LogPageHandler() {
    try {
		org.apache.log4j.Logger logger = LogManager.getRootLogger();
        if (logger != null) {
            Enumeration<Appender> appenders = logger.getAllAppenders();
            if (appenders != null) {
                while (appenders.hasMoreElements()) {
                    Appender appender = appenders.nextElement();
                    if (appender instanceof FileAppender) {
                        FileAppender fileAppender = (FileAppender)appender;
                        String filename = fileAppender.getFile();
                        file = new File(filename);
                        break;
                    }
                }
            }
        }
    } catch (Throwable t) {
    }
   }
 
Example 4
Source File: Log4jLoggerAdapter.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public Log4jLoggerAdapter() {
	try {
		org.apache.log4j.Logger logger = LogManager.getRootLogger();
           if (logger != null) {
               Enumeration<Appender> appenders = logger.getAllAppenders();
               if (appenders != null) {
                   while (appenders.hasMoreElements()) {
                       Appender appender = appenders.nextElement();
                       if (appender instanceof FileAppender) {
                           FileAppender fileAppender = (FileAppender)appender;
                           String filename = fileAppender.getFile();
                           file = new File(filename);
                           break;
                       }
                   }
               }
           }
       } catch (Throwable t) {
       }
}
 
Example 5
Source File: ActiveActor.java    From scheduling with GNU Affero General Public License v3.0 6 votes vote down vote up
private void addHostToFilename(FileAppender fileApp) {
    String hostID = Math.abs(UniqueID.getCurrentVMID().hashCode() % 100000) + "-" +
                    ProActiveInet.getInstance().getHostname();
    String fileName = fileApp.getFile();
    File filePath = new File(fileName);
    String hostFileName;
    String name = filePath.getName();
    String pathToFile = filePath.getParent();
    int point = name.indexOf('.');
    if (point == -1) {
        hostFileName = fileName + "-" + hostID;
    } else {
        String extension = name.substring(point + 1);
        String nameNoExtension = name.substring(0, point);
        hostFileName = (pathToFile != null ? pathToFile : "") + File.separator + nameNoExtension + "-" + hostID +
                       "." + extension;
    }
    System.out.println("New output file:" + hostFileName);
    fileApp.setFile(hostFileName);
    fileApp.activateOptions();
}
 
Example 6
Source File: Log4jLoggerAdapter.java    From tddl with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public Log4jLoggerAdapter(){
    try {
        org.apache.log4j.Logger logger = LogManager.getRootLogger();
        if (logger != null) {
            Enumeration<Appender> appenders = logger.getAllAppenders();
            if (appenders != null) {
                while (appenders.hasMoreElements()) {
                    Appender appender = appenders.nextElement();
                    if (appender instanceof FileAppender) {
                        FileAppender fileAppender = (FileAppender) appender;
                        String filename = fileAppender.getFile();
                        file = new File(filename);
                        break;
                    }
                }
            }
        }
    } catch (Throwable t) {
    }
}
 
Example 7
Source File: Log4jLoggerAdapter.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public Log4jLoggerAdapter() {
	try {
		org.apache.log4j.Logger logger = LogManager.getRootLogger();
           if (logger != null) {
               Enumeration<Appender> appenders = logger.getAllAppenders();
               if (appenders != null) {
                   while (appenders.hasMoreElements()) {
                       Appender appender = appenders.nextElement();
                       if (appender instanceof FileAppender) {
                           FileAppender fileAppender = (FileAppender)appender;
                           String filename = fileAppender.getFile();
                           file = new File(filename);
                           break;
                       }
                   }
               }
           }
       } catch (Throwable t) {
       }
}
 
Example 8
Source File: MR4CLogging.java    From mr4c with Apache License 2.0 6 votes vote down vote up
private Set<File> extractAppenderFiles(org.apache.log4j.Logger logger) {
	Set<File> result = new HashSet<File>();
	Enumeration<Appender> e = logger.getAllAppenders();
	while ( e.hasMoreElements() ) {
		Appender app = e.nextElement();
		if (app instanceof FileAppender ) {
			FileAppender fileApp = (FileAppender) app;
			String filePath = fileApp.getFile();
			if ( filePath==null ) {
				m_logger.warn("Logger {} has file appender {}  with no file", logger.getName(), app.getName());
			} else {
				result.add(new File(filePath));
			}
		}
	}
	return result;
}
 
Example 9
Source File: LogPageHandler.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public LogPageHandler() {
    try {
		org.apache.log4j.Logger logger = LogManager.getRootLogger();
        if (logger != null) {
            Enumeration<Appender> appenders = logger.getAllAppenders();
            if (appenders != null) {
                while (appenders.hasMoreElements()) {
                    Appender appender = appenders.nextElement();
                    if (appender instanceof FileAppender) {
                        FileAppender fileAppender = (FileAppender)appender;
                        String filename = fileAppender.getFile();
                        file = new File(filename);
                        break;
                    }
                }
            }
        }
    } catch (Throwable t) {
    }
   }
 
Example 10
Source File: LogPageHandler.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public LogPageHandler() {
    try {
		org.apache.log4j.Logger logger = LogManager.getRootLogger();
        if (logger != null) {
            Enumeration<Appender> appenders = logger.getAllAppenders();
            if (appenders != null) {
                while (appenders.hasMoreElements()) {
                    Appender appender = appenders.nextElement();
                    if (appender instanceof FileAppender) {
                        FileAppender fileAppender = (FileAppender)appender;
                        String filename = fileAppender.getFile();
                        file = new File(filename);
                        break;
                    }
                }
            }
        }
    } catch (Throwable t) {
    }
   }
 
Example 11
Source File: Log4jLoggerAdapter.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public Log4jLoggerAdapter() {
	try {
		org.apache.log4j.Logger logger = LogManager.getRootLogger();
           if (logger != null) {
               Enumeration<Appender> appenders = logger.getAllAppenders();
               if (appenders != null) {
                   while (appenders.hasMoreElements()) {
                       Appender appender = appenders.nextElement();
                       if (appender instanceof FileAppender) {
                           FileAppender fileAppender = (FileAppender)appender;
                           String filename = fileAppender.getFile();
                           file = new File(filename);
                           break;
                       }
                   }
               }
           }
       } catch (Throwable t) {
       }
}
 
Example 12
Source File: LoggerUtil.java    From Bats with Apache License 2.0 5 votes vote down vote up
public static LogFileInformation getLogFileInformation(Logger logger)
{
  if (logger == null) {
    logger = LogManager.getRootLogger();
  }
  FileAppender fileAppender = getFileAppender(logger);
  if (fileAppender != null) {
    File logFile = new File(fileAppender.getFile());
    LogFileInformation logFileInfo = new LogFileInformation(fileAppender.getFile(), logFile.length());
    return logFileInfo;
  }
  return null;
}
 
Example 13
Source File: LoggerInit.java    From diamond with Apache License 2.0 5 votes vote down vote up
private static void setFileAppender(FileAppender bizFileAppender, String logName) {
    FileAppender fileAppender = getFileAppender(Logger.getLogger(logName));
    String bizLogDir = new File(bizFileAppender.getFile()).getParent();

    File newLogFile = new File(bizLogDir, fileAppender.getFile());

    fileAppender.setFile(newLogFile.getAbsolutePath());
    fileAppender.activateOptions(); // 很重要,否则原有日志内容会被清空
    log.warn("成功为" + logName + "添加Appender. 输出路径:" + newLogFile.getAbsolutePath());
}
 
Example 14
Source File: LoggerUtil.java    From attic-apex-core with Apache License 2.0 5 votes vote down vote up
public static LogFileInformation getLogFileInformation(Logger logger)
{
  if (logger == null) {
    logger = LogManager.getRootLogger();
  }
  FileAppender fileAppender = getFileAppender(logger);
  if (fileAppender != null) {
    File logFile = new File(fileAppender.getFile());
    LogFileInformation logFileInfo = new LogFileInformation(fileAppender.getFile(), logFile.length());
    return logFileInfo;
  }
  return null;
}
 
Example 15
Source File: Log4jContainer.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public void start() {
    String file = ConfigUtils.getProperty(LOG4J_FILE);
    if (file != null && file.length() > 0) {
        String level = ConfigUtils.getProperty(LOG4J_LEVEL);
        if (level == null || level.length() == 0) {
            level = DEFAULT_LOG4J_LEVEL;
        }
        Properties properties = new Properties();
        properties.setProperty("log4j.rootLogger", level + ",application");
        properties.setProperty("log4j.appender.application", "org.apache.log4j.DailyRollingFileAppender");
        properties.setProperty("log4j.appender.application.File", file);
        properties.setProperty("log4j.appender.application.Append", "true");
        properties.setProperty("log4j.appender.application.DatePattern", "'.'yyyy-MM-dd");
        properties.setProperty("log4j.appender.application.layout", "org.apache.log4j.PatternLayout");
        properties.setProperty("log4j.appender.application.layout.ConversionPattern", "%d [%t] %-5p %C{6} (%F:%L) - %m%n");
        PropertyConfigurator.configure(properties);
    }
    String subdirectory = ConfigUtils.getProperty(LOG4J_SUBDIRECTORY);
    if (subdirectory != null && subdirectory.length() > 0) {
        Enumeration<org.apache.log4j.Logger> ls = LogManager.getCurrentLoggers();
        while (ls.hasMoreElements()) {
            org.apache.log4j.Logger l = ls.nextElement();
            if (l != null) {
                Enumeration<Appender> as = l.getAllAppenders();
                while (as.hasMoreElements()) {
                    Appender a = as.nextElement();
                    if (a instanceof FileAppender) {
                        FileAppender fa = (FileAppender)a;
                        String f = fa.getFile();
                        if (f != null && f.length() > 0) {
                            int i = f.replace('\\', '/').lastIndexOf('/');
                            String path;
                            if (i == -1) {
                                path = subdirectory;
                            } else {
                                path = f.substring(0, i);
                                if (! path.endsWith(subdirectory)) {
                                    path = path + "/" + subdirectory;
                                }
                                f = f.substring(i + 1);
                            }
                            fa.setFile(path + "/" + f);
                            fa.activateOptions();
                        }
                    }
                }
            }
        }
    }
}
 
Example 16
Source File: TaskLogger.java    From scheduling with GNU Affero General Public License v3.0 4 votes vote down vote up
private void removeTaskLogFile() {
    FileAppender fileAppender = (FileAppender) taskLogAppender.getAppender(FILE_APPENDER_NAME);
    if (fileAppender != null && fileAppender.getFile() != null) {
        FileUtils.deleteQuietly(new File(fileAppender.getFile()));
    }
}
 
Example 17
Source File: Log4jContainer.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public void start() {
    String file = ConfigUtils.getProperty(LOG4J_FILE);
    if (file != null && file.length() > 0) {
        String level = ConfigUtils.getProperty(LOG4J_LEVEL);
        if (level == null || level.length() == 0) {
            level = DEFAULT_LOG4J_LEVEL;
        }
        Properties properties = new Properties();
        properties.setProperty("log4j.rootLogger", level + ",application");
        properties.setProperty("log4j.appender.application", "org.apache.log4j.DailyRollingFileAppender");
        properties.setProperty("log4j.appender.application.File", file);
        properties.setProperty("log4j.appender.application.Append", "true");
        properties.setProperty("log4j.appender.application.DatePattern", "'.'yyyy-MM-dd");
        properties.setProperty("log4j.appender.application.layout", "org.apache.log4j.PatternLayout");
        properties.setProperty("log4j.appender.application.layout.ConversionPattern", "%d [%t] %-5p %C{6} (%F:%L) - %m%n");
        PropertyConfigurator.configure(properties);
    }
    String subdirectory = ConfigUtils.getProperty(LOG4J_SUBDIRECTORY);
    if (subdirectory != null && subdirectory.length() > 0) {
        Enumeration<org.apache.log4j.Logger> ls = LogManager.getCurrentLoggers();
        while (ls.hasMoreElements()) {
            org.apache.log4j.Logger l = ls.nextElement();
            if (l != null) {
                Enumeration<Appender> as = l.getAllAppenders();
                while (as.hasMoreElements()) {
                    Appender a = as.nextElement();
                    if (a instanceof FileAppender) {
                        FileAppender fa = (FileAppender)a;
                        String f = fa.getFile();
                        if (f != null && f.length() > 0) {
                            int i = f.replace('\\', '/').lastIndexOf('/');
                            String path;
                            if (i == -1) {
                                path = subdirectory;
                            } else {
                                path = f.substring(0, i);
                                if (! path.endsWith(subdirectory)) {
                                    path = path + "/" + subdirectory;
                                }
                                f = f.substring(i + 1);
                            }
                            fa.setFile(path + "/" + f);
                            fa.activateOptions();
                        }
                    }
                }
            }
        }
    }
}
 
Example 18
Source File: GridTestLog4jLogger.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Nullable @Override public String fileName() {
    FileAppender fapp = F.first(fileAppenders);

    return fapp != null ? fapp.getFile() : null;
}
 
Example 19
Source File: Log4jContainer.java    From dubbox with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public void start() {
    String file = ConfigUtils.getProperty(LOG4J_FILE);
    if (file != null && file.length() > 0) {
        String level = ConfigUtils.getProperty(LOG4J_LEVEL);
        if (level == null || level.length() == 0) {
            level = DEFAULT_LOG4J_LEVEL;
        }
        Properties properties = new Properties();
        properties.setProperty("log4j.rootLogger", level + ",application");
        properties.setProperty("log4j.appender.application", "org.apache.log4j.DailyRollingFileAppender");
        properties.setProperty("log4j.appender.application.File", file);
        properties.setProperty("log4j.appender.application.Append", "true");
        properties.setProperty("log4j.appender.application.DatePattern", "'.'yyyy-MM-dd");
        properties.setProperty("log4j.appender.application.layout", "org.apache.log4j.PatternLayout");
        properties.setProperty("log4j.appender.application.layout.ConversionPattern", "%d [%t] %-5p %C{6} (%F:%L) - %m%n");
        PropertyConfigurator.configure(properties);
    }
    String subdirectory = ConfigUtils.getProperty(LOG4J_SUBDIRECTORY);
    if (subdirectory != null && subdirectory.length() > 0) {
        Enumeration<org.apache.log4j.Logger> ls = LogManager.getCurrentLoggers();
        while (ls.hasMoreElements()) {
            org.apache.log4j.Logger l = ls.nextElement();
            if (l != null) {
                Enumeration<Appender> as = l.getAllAppenders();
                while (as.hasMoreElements()) {
                    Appender a = as.nextElement();
                    if (a instanceof FileAppender) {
                        FileAppender fa = (FileAppender)a;
                        String f = fa.getFile();
                        if (f != null && f.length() > 0) {
                            int i = f.replace('\\', '/').lastIndexOf('/');
                            String path;
                            if (i == -1) {
                                path = subdirectory;
                            } else {
                                path = f.substring(0, i);
                                if (! path.endsWith(subdirectory)) {
                                    path = path + "/" + subdirectory;
                                }
                                f = f.substring(i + 1);
                            }
                            fa.setFile(path + "/" + f);
                            fa.activateOptions();
                        }
                    }
                }
            }
        }
    }
}
 
Example 20
Source File: Log4jContainer.java    From dubbo-2.6.5 with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void start() {
    String file = ConfigUtils.getProperty(LOG4J_FILE);
    if (file != null && file.length() > 0) {
        String level = ConfigUtils.getProperty(LOG4J_LEVEL);
        if (level == null || level.length() == 0) {
            level = DEFAULT_LOG4J_LEVEL;
        }
        Properties properties = new Properties();
        properties.setProperty("log4j.rootLogger", level + ",application");
        properties.setProperty("log4j.appender.application", "org.apache.log4j.DailyRollingFileAppender");
        properties.setProperty("log4j.appender.application.File", file);
        properties.setProperty("log4j.appender.application.Append", "true");
        properties.setProperty("log4j.appender.application.DatePattern", "'.'yyyy-MM-dd");
        properties.setProperty("log4j.appender.application.layout", "org.apache.log4j.PatternLayout");
        properties.setProperty("log4j.appender.application.layout.ConversionPattern", "%d [%t] %-5p %C{6} (%F:%L) - %m%n");
        PropertyConfigurator.configure(properties);
    }
    String subdirectory = ConfigUtils.getProperty(LOG4J_SUBDIRECTORY);
    if (subdirectory != null && subdirectory.length() > 0) {
        Enumeration<org.apache.log4j.Logger> ls = LogManager.getCurrentLoggers();
        while (ls.hasMoreElements()) {
            org.apache.log4j.Logger l = ls.nextElement();
            if (l != null) {
                Enumeration<Appender> as = l.getAllAppenders();
                while (as.hasMoreElements()) {
                    Appender a = as.nextElement();
                    if (a instanceof FileAppender) {
                        FileAppender fa = (FileAppender) a;
                        String f = fa.getFile();
                        if (f != null && f.length() > 0) {
                            int i = f.replace('\\', '/').lastIndexOf('/');
                            String path;
                            if (i == -1) {
                                path = subdirectory;
                            } else {
                                path = f.substring(0, i);
                                if (!path.endsWith(subdirectory)) {
                                    path = path + "/" + subdirectory;
                                }
                                f = f.substring(i + 1);
                            }
                            fa.setFile(path + "/" + f);
                            fa.activateOptions();
                        }
                    }
                }
            }
        }
    }
}