

















关键字:jakarta, commons, configuration
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
......
public class Log4jTest {
private static Logger logger = Logger.getLogger(Log4jTest.class.getName());
......
public static void main(String[] args) {
BasicConfigurator.configure();
logger.info("log4j has been configurated with BasicConfigurator.configuration successfully!");
}
}
0 [main] INFO Log4jTest - log4j has been configed with BasicConfigurator.configure() successfully!
# For the general syntax of property based configuration files see the
# documenation of org.apache.log4j.PropertyConfigurator.
# The root category uses the appender called A1. Since no priority is
# specified, the root category assumes the default priority for root
# which is DEBUG in log4j. The root category is the only category that
# has a default priority. All other categories need not be assigned a
# priority in which case they inherit their priority from the
# hierarchy.
log4j.rootCategory=INFO, A1, A2
# A1 is set to be a FileAppender which outputs to the file
# "factor.log". Start the server NumberCruncherServer and two
# NumberCruncherClients, and ask to factor two numbers
# near-simultaneously. Notice that the log output from these two
# requests are logged in the file factor.log. Nevertheless, the logs
# of these requests can still be distinguished given their distinct
# nested diagnostic contexts.
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File=WEB-INF/logs/portal.log
log4j.appender.A1.DatePattern='.'yyyy-MM-dd
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A2=org.apache.log4j.ConsoleAppender
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
# Note the %x conversion specifier for NDC printing.
# %d date time
# %-5p debug level
# %m messages
# %l class with method and line number (slowly! dubug only, on release use %c{2} in release version)
# %n \n or \r\n
#debug version
log4j.appender.A1.layout.ConversionPattern=%d [%-5p] %l - %m%n
log4j.appender.A2.layout.ConversionPattern=%d [%-5p] %l - %m%n
#release version
# log4j.appender.A1.layout.ConversionPattern=%d [%-5p] %c{2} - %m%n
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Logger;
......
public class Log4jTestServlet {
private static Logger logger = Logger.getLogger(Log4jTestServlet.class.getName());
......
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
String file = getRealPath(getInitParameter("log4j-init-file"));
PropertyConfigurator.configure(file);
/*
*PropertyConfigurator.configure(Properties);
*PropertyConfigurator.configure(URL);
*PropertyConfigurator.configureAndWatch(file, 30); //delay 30 second
*/
logger.info("log4j has been configurated with PropertyConfigurator.configuration successfully!");
}
}
Log4j short mannul
http://logging.apache.org/log4j/docs/manual.html
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。