ConsoleAppender appender = new ConsoleAppender(new PatternLayout());
FileAppender appender = null; try { appender = new FileAppender(new PatternLayout(),"filename"); } catch(Exception e) {}上面用到的构造函数:
FileAppender(Layout layout, String filename, boolean append)实例化一个FileAppender并且打开变量"filename"指定的文件。
WriterAppender appender = null; try { appender = new WriterAppender(new PatternLayout(),new FileOutputStream("filename")); } catch(Exception e) {}这个WriterAppender使用的构造函数带有PatternLayout和OutputStream参数,在这种情况下, FileOutputStream用于向一个文件输出。当然,它还具有其他可用的构造函数。
import java.io.*; import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.HTMLLayout; import org.apache.log4j.WriterAppender; public class htmlandwrite { static Logger logger = Logger.getLogger(htmlandwrite.class); public static void main(String args[]) { HTMLLayout layout = new HTMLLayout(); WriterAppender appender = null; try { FileOutputStream output = new FileOutputStream("output2.html"); appender = new WriterAppender(layout,output); } catch(Exception e) {} logger.addAppender(appender); logger.setLevel((Level) Level.DEBUG); logger.debug("Here is some DEBUG"); logger.info("Here is some INFO"); logger.warn("Here is some WARN"); logger.error("Here is some ERROR"); logger.fatal("Here is some FATAL"); } }
import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.log4j.PatternLayout; import org.apache.log4j.ConsoleAppender; public class consandpatt { static Logger logger = Logger.getLogger(consandpatt.class); public static void main(String args[]) { // Note, %n is newline String pattern = "Milliseconds since program start: %r %n"; pattern += "Classname of caller: %C %n"; pattern += "Date in ISO8601 format: %d{ISO8601} %n"; pattern += "Location of log event: %l %n"; pattern += "Message: %m %n %n"; PatternLayout layout = new PatternLayout(pattern); ConsoleAppender appender = new ConsoleAppender(layout); logger.addAppender(appender); logger.setLevel((Level) Level.DEBUG); logger.debug("Here is some DEBUG"); logger.info("Here is some INFO"); logger.warn("Here is some WARN"); logger.error("Here is some ERROR"); logger.fatal("Here is some FATAL"); } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.SimpleLayout"/> </appender> <root> <priority value ="debug" /> <appender-ref ref="ConsoleAppender"/> </root> </log4j:configuration> 文件以标准的XML声明作为开始,后面跟着指出DTD(文档类型定义)的DOCTYPE声明,它定义了XML文件的结构,例如,什么元素可以嵌入在其他元素中等等。上面文件在log4j发行版的src/java/org/apache/log4j/xml目录中。 接着看看封装所有元素的 log4j:configuration 元素,它在DOCTYPE声明中被指定为根元素。嵌入在根元素中有两个结构: <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.SimpleLayout"/> </appender>
<root> <priority value ="debug" /> <appender-ref ref="ConsoleAppender"/> </root>
import org.apache.log4j.Logger; import org.apache.log4j.xml.DOMConfigurator; public class externalxmltest { static Logger logger = Logger.getLogger(filetest.class); public static void main(String args[]) { DOMConfigurator.configure("xmllog4jconfig.xml"); logger.debug("Here is some DEBUG"); logger.info("Here is some INFO"); logger.warn("Here is some WARN"); logger.error("Here is some ERROR"); logger.fatal("Here is some FATAL"); } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="appender" class="org.apache.log4j.FileAppender"> <param name="File" value="Indentify-Log.txt"/> <param name="Append" value="false"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d [%t] %p - %m%n"/> </layout> </appender> <root> <priority value ="debug"/> <appender-ref ref="appender"/> </root> </log4j:configuration>
# initialise root logger with level DEBUG and call it BLAHlog4j.rootLogger=DEBUG, BLAH# add a ConsoleAppender to the logger BLAHlog4j.appender.BLAH= org.apache.log4j.ConsoleAppender# set set that layout to be SimpleLayoutlog4j.appender.BLAH.layout= org.apache.log4j.SimpleLayout
import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class externalplaintest { static Logger logger = Logger.getLogger(externalplaintest.class); public static void main(String args[]) { PropertyConfigurator.configure("plainlog4jconfig.xml"); logger.debug("Here is some DEBUG"); logger.info("Here is some INFO"); logger.warn("Here is some WARN"); logger.error("Here is some ERROR"); logger.fatal("Here is some FATAL"); } }
下面是我自己的写的两个小例子:
先看配置文件:
log4j.logger.signStampTimeInfo=info,test #log4j.appender.test=org.apache.log4j.FileAppender log4j.appender.test=org.apache.log4j.DailyRollingFileAppender log4j.appender.test.layout=org.apache.log4j.PatternLayout log4j.appender.test.layout.ConversionPattern=[%-5p] [%d{HH:mm:ss:sss}] %c - %m%n log4j.appender.test.datePattern='.'yyyy-MM-dd #log4j.appender.test.append=true log4j.appender.test.File=${webApp.root}/logs/zklog4j/timeInfo.log #log4j.appender.test.File=D:/logs/zklog4j/timeInfo.log
1.级别,如上面的info 其中test是signStampTimeInfo的别名
2.布局,就是中间的很多的部分,以什么样的形式展现
3.输出目录:放到任意你想放的目录,或者返回流的形式,利用网络传递给目的地
运用:1)
package com.wfc.test; import org.apache.log4j.Logger; public class YY { private static final Logger loggerTime = Logger.getLogger("signStampTimeInfo"); public void shuchu(){ loggerTime.info( " debug " ); System.out.println("shuchu-------》执行!"); loggerTime.info( " error " ); } }
package com.wfc.test; public class Test1 { public static void main(String[] args){ YY testy=new YY(); testy.shuchu(); System.out.println("YYYYYY"); } }
运用2 )
下面是一个Servlet
package com.wfc.test; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; public class first extends HttpServlet { private static final Logger loggerTime = Logger.getLogger("signStampTimeInfo"); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { loggerTime.info("ServletdoGet方法执行开始。。。。。。"); PrintWriter out = response.getWriter(); response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8"); out.write("恭喜您注册成功!3秒后回到主页....."); response.setHeader("refresh", "3;url=/LogTest/index.jsp"); loggerTime.info("ServletdoGet方法执行完毕。。。。。"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
<context-param> <strong> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value></strong> </context-param> <context-param> <strong> <param-name>log4jConfigLocation</param-name> <param-value>WEB-INF/classes/log4j.properties</param-value> </strong> <!-- <param-value>WEB-INF/classes/log4j.properties</param-value> --> </context-param> <!-- Spring刷新Log4j配置文件变动的间隔,单位为毫秒 --> <context-param> <strong><param-name>log4jRefreshInterval</param-name> <param-value>10000</param-value> </strong> </context-param> <context-param> <param-name>webAppRootKey</param-name> <param-value>webApp.root</param-value> </context-param>