log4j 之 console/file/数据库输出

下面是定义的三种输出方式,已经在eclipse中测试过,文件名为:log4j.properties,放到src文件下,即可自动加载;需要log4j.jar包:

# An example log4j configuration file that outputs both to System.out
# and a file named 'test'.

# For the general syntax of property based configuration files see the
# documenation of org.apache.log4j.PropertyConfigurator.

# WARNING: Location information can be useful but is very costly in
# terms of computation.
# The root logger uses the appender called A1. 

# The root logger uses the appenders called A1 and A2. Since no level
# is specified, note the empty string between the comma (",") and the
# equals sign ("="), the level of the root logger remains
# untouched. Log4j always initializes the level for the root logger to
# DEBUG. The root logger is the only logger that has a default
# level. Bu default, all other loggers do not have an assigned level,
# such that they inherit their level instead.

log4j.rootLogger=info, A1, A2, A3
rootLogger.encoding=UTF-8
# A1 is set to be ConsoleAppender sending its output to System.out
log4j.appender.A1=org.apache.log4j.ConsoleAppender


# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout

# The conversion pattern consists of date in ISO8601 format, level,
# thread name, logger name truncated to its rightmost two components
# and left justified to 17 characters, location information consisting
# of file name (padded to 13 characters) and line number, nested
# diagnostic context, the and the application supplied message

log4j.appender.A1.layout.ConversionPattern=data(日期:):%d level:%-5p thread:[%t] logger:%-17c{2} (%13F:%L) %3x -content: %m%n

# Appender A2 writes to the file "test".
# 原有的log4j.appender.A2=org.apache.log4j.FileAppender
# 原有的log4j.appender.A2.File=${catalina.home}/webapps/10/logs/test.txt
# 更过又改的log4j.appender.A2.File=./Log/test.txt

log4j.appender.A2=org.apache.log4j.RollingFileAppender
# log4j.appender.A2.Threshold=ERROR
log4j.appender.A2.File=${catalina.home}/logs/logs_tomcat.log
log4j.appender.A2.Append=true
log4j.appender.A2.MaxFileSize=100000KB
log4j.appender.A2.MaxBackupIndex=100

# Truncate 'test' if it aleady exists.
# log4j.appender.A2.Append=false

# Appender A2 uses the PatternLayout.
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
# 原来的log4j.appender.A2.layout.ConversionPattern=%-5r %-5p [%t] %c{2}   %d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p]%m%n
log4j.appender.A2.layout.ConversionPattern=<%d>[%5p] %c - %m%n


#log4j.appender.A3=org.apache.log4j.jdbc.JDBCAppender
#log4j.appender.A3.driver=com.mysql.jdbc.Driver
#log4j.appender.A3.URL=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
#log4j.appender.A3.user=root
#log4j.appender.A3.password=root
#log4j.appender.A3.sql=insert into log_table (cdate, message) VALUES ('%d{yyyy-MM-dd hh:mm:ss}', '%c %p %m %n')
#log4j.appender.A3.layout=org.apache.log4j.PatternLayout
#直接使用log4j包中的文件  
log4j.appender.A3=org.apache.log4j.jdbc.JDBCAppender
#与数据库建立连接  
log4j.appender.A3.URL=jdbc:mysql://localhost:3306/test
log4j.appender.A3.driver=com.mysql.jdbc.Driver
log4j.appender.A3.user=root
log4j.appender.A3.password=root
#指定要插入数据库的格式,具体参数的格式看一开始给出的文档  
log4j.appender.A3.sql=INSERT INTO log_table (log_date, log_level, location, message) VALUES ('%d{ISO8601}', '%p', '%C,%L', '%m')

# In this example, we are not interested in INNER loop or SWAP
# messages.  You might try to set INNER and SWAP to DEBUG for more
# verbose output.

log4j.logger.org.apache.log4j.examples.SortAlgo.INNER=warn
log4j.logger.org.apache.log4j.examples.SortAlgo.SWAP=warn

下面是使用log输出的信息,某一个.java文件

package com.mr.aop;

import java.lang.reflect.Method;

import org.apache.log4j.Logger;
import org.springframework.aop.MethodBeforeAdvice;
import javax.servlet.http.HttpServletRequest;

public class BeforeAdivsor implements MethodBeforeAdvice {
    
    private static Logger logger = Logger
            .getLogger(AfterAdvice.class.getName());
    private static Logger log = Logger.getLogger(AfterAdvice.class);
    
    public void before(Method arg0, Object[] arg1, Object arg2)
            throws Throwable {
        logger.info("Before通知开始。。。。。。。");
        logger.debug("just for debug Before通知开始。。。。。。。");
        logger.warn("just for warn Before通知开始。。。。。。。");
        logger.error("just for error Before通知开始。。。。。。。");
        if (arg2 instanceof UserInterface) {
            UserInterface di = (UserInterface) arg2;// arg2为目标对象
            di.getConn();//调用getConn()创建连接
        }      
        //以下是将getConn()创建的连接状态,传递给ExecuteInsert实现类
        ConnClass ci = (ConnClass) arg2;//转换为抽象类对象
        ExecuteInsert bi = (ExecuteInsert) arg2;//转换为实现类对象
        //将连接状态设置给实现类。目的是让execute方法执行前获先得连接
        bi.setState(ci.getStmt());
        logger.error("just for error after通知开始。。。。。。。");
    }
    
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值