6.log4j.properties例子:同时指定多个appender

来源:互联网 发布:oracle awr sql 编辑:程序博客网 时间:2024/05/16 04:50
package com.v512.log4j;import org.apache.log4j.Logger;public class HelloLog4J {// 构造记录器,形参是记录器所在的类,表示要在该类做日志private static Logger logger = Logger.getLogger(HelloLog4J.class);/**  * @param args */public static void main(String[] args) {getMessage();}private static void getMessage() {// 记录下各种级别的信息,这些信息放在哪儿,以哪种方式存放,在log4j.properties文件中配置.logger.debug("This is debug message.");logger.info("This is a info message.");logger.warn("This is a warn message.");logger.error("This is a error message.");}}

log4j.rootLogger=info,file,stdoutlog4j.appender.file=org.apache.log4j.FileAppenderlog4j.appender.file.File=Message.loglog4j.appender.file.layout=org.apache.log4j.TTCCLayoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.TTCCLayout

控制台:[main] INFO com.v512.log4j.HelloLog4J - This is a info message.[main] WARN com.v512.log4j.HelloLog4J - This is a warn message.[main] ERROR com.v512.log4j.HelloLog4J - This is a error message.文件:Message.log(工程目录)[main] INFO com.v512.log4j.HelloLog4J - This is a info message.[main] WARN com.v512.log4j.HelloLog4J - This is a warn message.[main] ERROR com.v512.log4j.HelloLog4J - This is a error message.

原创粉丝点击