maven 创建log4j

  1. 添加依赖包
  2.   
  3.         <dependency>  
  4.             <groupId>org.slf4jgroupId>  
  5.             <artifactId>slf4j-log4j12artifactId>  
  6.             <version>1.7.25version>  
  7.         dependency>  
    1.   
    2.         <dependency>  
    3.             <groupId>javax.mailgroupId>  
    4.             <artifactId>mailartifactId>  
    5.             <version>1.4version>  
    6.         dependency>  
    7.         <dependency>  
    8.             <groupId>javax.activationgroupId>  
    9.             <artifactId>activationartifactId>  
    10.             <version>1.1version>  
    11.         dependency>  
    12.          

  8. 配置文件log4j.properties
  9. log4j.rootLogger = INFO,DEBUG, console, infoFile, errorFile ,debugfile,mail   
    LocationInfo=true      
    
    log4j.appender.console = org.apache.log4j.ConsoleAppender    
    log4j.appender.console.Target = System.out    
    log4j.appender.console.layout = org.apache.log4j.PatternLayout   
    ##
    log4j.appender.console.layout.ConversionPattern =[%d{yyyy-MM-dd HH:mm:ss,SSS}]-[%p]:%m   %x %n   
    
    log4j.appender.infoFile = org.apache.log4j.DailyRollingFileAppender    
    log4j.appender.infoFile.Threshold = INFO    
    log4j.appender.infoFile.File = D:/logs/log   
    log4j.appender.infoFile.DatePattern = '.'yyyy-MM-dd'.log'    
    log4j.appender.infoFile.Append=true  
    log4j.appender.infoFile.layout = org.apache.log4j.PatternLayout    
    log4j.appender.infoFile.layout.ConversionPattern =[%d{yyyy-MM-dd HH:mm:ss,SSS}]-[%p]:%m  %x %n   
    
    log4j.appender.errorFile = org.apache.log4j.DailyRollingFileAppender    
    log4j.appender.errorFile.Threshold = ERROR    
    log4j.appender.errorFile.File = D:/logs/error    
    log4j.appender.errorFile.DatePattern = '.'yyyy-MM-dd'.log'    
    log4j.appender.errorFile.Append=true    
    log4j.appender.errorFile.layout = org.apache.log4j.PatternLayout    
    log4j.appender.errorFile.layout.ConversionPattern =[%d{yyyy-MM-dd HH:mm:ss,SSS}]-[%p]:%m  %x %n  
    ##mail
    #log4j.appender.mail=org.apache.log4j.net.SMTPAppender
    #log4j.appender.mail.Threshold=ERROR
    #log4j.appender.mail.BufferSize=1
    #log4j.appender.mail.From [email protected]
    #log4j.appender.mail.SMTPHost=mail.qq.com
    #log4j.appender.mail.Subject=Log4J Message
    #[email protected]
    #log4j.appender.mail.SMTPUsername=123
    #log4j.appender.mail.SMTPPassword=123456
    #log4j.appender.mail.layout=org.apache.log4j.PatternLayout
    #log4j.appender.mail.layout=org.apache.log4j.PatternLayout
    log4j.appender.mail.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %n 

  10. 邮箱功能未验证

        日志功能测试类

package com.great.commonUtil;

import org.apache.log4j.Logger;

/**
 * log4j打印错误日志
 */
public class Log4jUtil {
    /**
     * 打印错误信息
     * @param object 获取对象
     * @param e 错误信息
     */
    public static void logError(Object object,Exception e){
        Logger logger = Logger.getLogger(object.getClass());
        logger.error("run error:",e);
    }

    public static void logInfo(Object object,Object message){
        Logger logger = Logger.getLogger(object.getClass());
        logger.info(message);
    }
}

你可能感兴趣的:(maven 创建log4j)