Log4j在没有属性文件的情况下运行

时间:2019-02-22 11:26:25

标签: java log4j

我需要为我所在的项目配置log4j。 Log4j正常工作,但是我在任何地方都找不到log4j-properties.xml。 这些是我的webApplication中与log4j相关的类,第一个是名为“ Log4JInitServlet.java”的servlet:

公共类Log4JInitServlet扩展了HttpServlet {

private static final long serialVersionUID = 1L;


public void init(ServletConfig config) throws ServletException {
    System.out.println("Log4JInitServlet is initializing log4j");
    String log4jLocation = config.getInitParameter("log4j-properties-location");

    ServletContext sc = config.getServletContext();


    if (log4jLocation == null) {
        System.err.println("*** No log4j-properties-location init param, so initializing log4j with BasicConfigurator");
        BasicConfigurator.configure();
    } else {
        String webAppPath = sc.getRealPath("/");
        String log4jProp = webAppPath + log4jLocation;
        File yoMamaYesThisSaysYoMama = new File(log4jProp);

        if (yoMamaYesThisSaysYoMama.exists()) {
            System.out.println("Initializing log4j with: " + log4jProp);                
            PropertyConfigurator.configure(log4jProp);
        } else {
            System.err.println("*** " + log4jProp + " file not found, so initializing log4j with BasicConfigurator");
            BasicConfigurator.configure();
        }
    }
    super.init(config);
}

/**
 * Log4JInitServlet?reloadPropertiesFile=true
 * Log4JInitServlet?logLevel=XXX
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("This is the Log4JInitServlet<br/>");
    String logLevel = request.getParameter("logLevel");
    String reloadPropertiesFile = request.getParameter("reloadPropertiesFile");
    if (logLevel != null) {
        setLogLevelWithParameter(out, logLevel);
    } else if (reloadPropertiesFile != null) {
        out.println("Attempting to reload log4j properties file<br/>");
        loadLog4jPropertiesFile(out);
    } else {
        out.println("no logLevel or reloadPropertiesFile parameters were found<br/>");
    }
}

private void setLogLevelWithParameter(PrintWriter out, String logLevel) {
    Logger root = Logger.getRootLogger();
    boolean logLevelRecognized = true;
    if ("DEBUG".equalsIgnoreCase(logLevel)) {
        root.setLevel(Level.DEBUG);
    } else if ("INFO".equalsIgnoreCase(logLevel)) {
        root.setLevel(Level.INFO);
    } else if ("WARN".equalsIgnoreCase(logLevel)) {
        root.setLevel(Level.WARN);
    } else if ("ERROR".equalsIgnoreCase(logLevel)) {
        root.setLevel(Level.ERROR);
    } else if ("FATAL".equalsIgnoreCase(logLevel)) {
        root.setLevel(Level.FATAL);
    } else {
        logLevelRecognized = false;
    }

    if (logLevelRecognized) {
        out.println("Log level has been set to: " + logLevel + "<br/>");
    } else {
        out.println("logLevel parameter '" + logLevel + "' level not recognized<br/>");
    }
}

private void loadLog4jPropertiesFile(PrintWriter out) {
    ServletContext sc = getServletContext();
    String log4jLocation = getInitParameter("log4j-properties-location");

    if (log4jLocation == null) {
        out.println("*** No log4j-properties-location init param, so initializing log4j with BasicConfigurator<br/>");
        BasicConfigurator.configure();
    } else {
        String webAppPath = sc.getRealPath("/");
        String log4jProp = webAppPath + log4jLocation;
        File log4jFile = new File(log4jProp);
        if (log4jFile.exists()) {
            out.println("Initializing log4j with: " + log4jProp + "<br/>");
            PropertyConfigurator.configure(log4jProp);
        } else {
            out.println("*** " + log4jProp + " file not found, so initializing log4j with BasicConfigurator<br/>");
            BasicConfigurator.configure();
        }
    }
}

}

第二个是名为LogAgent.java的类:

公共类LogAgent {

public LogAgent(String pathLogName)
{
    FILE_LOG_NAME = "";
    FILE_LOG_NAME = pathLogName;
}

public void log(String messageInput)
{
    Calendar now = Calendar.getInstance();
    String message = (new StringBuilder("<")).append(now.get(5)).append("/").append(now.get(2) + 1).append("/").append(now.get(1)).append(" ").append(now.get(11)).append(":").append(now.get(12)).append(":").append(now.get(13)).append(":").append(now.get(14)).append(">").append("<").append(messageInput).append(">").toString();
    try
    {
        File t = new File(FILE_LOG_NAME);
        if(t.length() > LOG_MAXSIZE)
        {
            String newName = (new StringBuilder(String.valueOf(FILE_LOG_NAME))).append(".").append(now.get(5)).append("_").append(now.get(2) + 1).append("_").append(now.get(1)).append("_").append(now.get(10)).append("_").append(now.get(12)).append("_").append(now.get(13)).toString();
            File dest = new File(newName);
            t.renameTo(dest);
            t.delete();
        }
        FileWriter f = new FileWriter(FILE_LOG_NAME, true);
        f.write((new StringBuilder(String.valueOf(message))).append("\n").toString());
        f.close();
    }
    catch(Exception e)
    {
        System.err.println((new StringBuilder("LogAgent : log () : ")).append(e).toString());
    }
}

private static long LOG_MAXSIZE = 0x16E360;
private String FILE_LOG_NAME;

}

如何配置它?

0 个答案:

没有答案