首页 > 代码库 > log4j配置
log4j配置
在src目录下增加
log4j.properties:
log4j.appender.logFile=org.apache.log4j.FileAppenderlog4j.appender.logFile.Threshold=DEBUG log4j.appender.logFile.ImmediateFlush=truelog4j.appender.logFile.Append=truelog4j.appender.logFile.File=F:/Workspaces/logs/log.log4jlog4j.appender.logFile.layout=org.apache.log4j.PatternLayoutlog4j.appender.logFile.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l\: %m %x %n
包 Acton
Action.java:
package Action;public interface Action { public String execute(String str);}
LowerAction.java:
package Action;public class LowerAction implements Action { private String message; public String getMessage() { return message; } public void setMessage(String string) { message = string; } public String execute(String str) { return (getMessage() + str).toLowerCase(); }}
UpperAction.java:
package Action;public class UpperAction implements Action { private String message; public String getMessage() { return message; } public void setMessage(String string) { message = string; } public String execute(String str) { return ((getMessage() + str).toUpperCase()); }}
Test.java:
package Action;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;public class Test { public static Log log = LogFactory.getLog("Test"); public static void main(String Args[]) { log.info("Start..."); // String[] locations = { "beans.xml" }; /*ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Action action = (Action) ctx.getBean("beanID"); System.out.println(action.execute(" Joson"));*/ Resource resource=new ClassPathResource("beans.xml"); @SuppressWarnings("deprecation") BeanFactory factory=new XmlBeanFactory(resource); Action action=(Action)factory.getBean("beanID"); System.out.println(action.execute(" The new future")); log.info("End..."); }}
beans.xml:
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <description>Spring Quick Start</description> <bean id="beanID" class="Action.LowerAction"> <property name="message" value="http://www.mamicode.com/hello"/> </bean></beans>
然后加载Spring和log4j相应的jar包
log4j配置
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。