Java用log4j写日志

日志可以方便追踪和调试问题,以前用log4net写日志,换Java了改用log4j写日志,用法和log4net差不多。

到apache包下载下载log4j的包,解压后把下图两个jar包引入工程
Java用log4j写日志_第1张图片

先到网站根下加一个log4j2.xml的配置文件来配置日志的格式和参数
Java用log4j写日志_第2张图片


<Configuration status="ERROR">
  
  <Appenders>
    
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %F %logger{36} - %msg%n"/>
      
    Console>
    <File name="ExceptionRollingLogFileAppender" fileName="./Logs/异常日志.txt" append="true">
      <PatternLayout pattern="%date [%thread] %-5level %15logger %appdomain %location -- %message %exception %newline"/>
    File>
    <File name="DebugRollingLogFileAppender" fileName="./Logs/调试日志.txt" append="true">
      <PatternLayout pattern="%date [%thread] %-5level %15logger %appdomain %location -- %message %exception %newline"/>
    File>
    <File name="OperationRollingLogFileAppender" fileName="./Logs/方法日志.txt" append="true">
      <PatternLayout pattern="%date [%thread] %-5level %15logger %appdomain %location -- %message %exception %newline"/>
    File>
    <File name="SqlRollingLogFileAppender" fileName="./Logs/Sql语句日志.txt" append="true">
      <PatternLayout pattern="%date [%thread] %-5level %15logger %appdomain %location -- %message %exception %newline"/>
    File>
    <File name="CapabilityRollingLogFileAppender" fileName="./Logs/性能.txt" append="true">
      <PatternLayout pattern="%date [%thread] %-5level %15logger %appdomain %location -- %message %exception %newline"/>
    File>
    <File name="SecurityRollingLogFileAppender" fileName="./Logs/安全.txt" append="true">
      <PatternLayout pattern="%date [%thread] %-5level %15logger %appdomain %location -- %message %exception %newline"/>
    File>

    <RollingFile name="customscript" fileName="${LOG_HOME}${FILE_NAME}" filePattern="${LOG_HOME}${FILE_NAME}.%d{yyyy-MM-dd}.log">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %M %L - %msg%xEx%n"/>
      <Policies>
        <TimeBasedTriggeringPolicy />
      Policies>
    RollingFile>
  Appenders>
  <Loggers>
    
    <Root level="ERROR">
      
      <AppenderRef ref="Console"/>
      
      <AppenderRef ref="debuglog"/>
    Root>
    <Logger name="Exception" level="ALL" additivity="false">
      
      <AppenderRef ref="ExceptionRollingLogFileAppender"/>
    Logger>

    <Logger name="Operation" level="ALL" additivity="false">
      
      <AppenderRef ref="OperationRollingLogFileAppender"/>
    Logger>

    <Logger name="Debug" level="ALL" additivity="false">
      
      <AppenderRef ref="DebugRollingLogFileAppender"/>
    Logger>

    <Logger name="SqlLog" level="ALL" additivity="false">
      
      <AppenderRef ref="SqlRollingLogFileAppender"/>
    Logger>

    <Logger name="Capability" level="ALL" additivity="false">
      
      <AppenderRef ref="CapabilityRollingLogFileAppender"/>
    Logger>

    <Logger name="Security" level="ALL" additivity="false">
      
      <AppenderRef ref="SecurityRollingLogFileAppender"/>
    Logger>

  Loggers>
Configuration>

然后实现日志工具类

package LIS.Core.Util;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class LogUtils {
    //得到日志操作,默认写在Tomcat的Bin下
    //异常日志
    static final Logger loggerException = LogManager.getLogger("Exception");

    //操作日志
    static final Logger loggerOperation = LogManager.getLogger("Operation");

    //调试日志
    static final Logger loggerDebug = LogManager.getLogger("Debug");

    //sql日志
    static final Logger loggerSqlLog = LogManager.getLogger("SqlLog");

    //性能日志
    static final Logger loggerCapability = LogManager.getLogger("Capability");

    //安全日志
    static final Logger loggerSecurity = LogManager.getLogger("Security");


    /// 
    /// 书写调试日志
    /// 
    /// 日志内容
    public static void WriteDebugLog(String message)
    {
        loggerDebug.debug(message);
    }

    /// 
    /// 书写异常日志
    /// 
    /// /// 日志内容
    /// 异常对象
    public static void WriteExceptionLog(String message, Exception exception)
    {
        loggerException.error(message, exception);
    }

    /// 
    /// 书写性能日志
    /// 
    /// 日志内容
    public static void WriteCapabilityLog(String message)
    {
        loggerCapability.info(message);
    }

    /// 
    /// 书写安全日志
    /// 
    /// 日志内容
    public static void WriteSecurityLog(String message)
    {
        loggerSecurity.info(message);
    }

    /// 
    /// 书写操作日志
    /// 
    /// 日志内容
    public static void WriteOperationLog(String message)
    {
        loggerOperation.info(message);
    }


    /// 
    /// 书写调试日志
    /// 
    /// 日志内容
    public static void WriteSqlLog(String message)
    {
        loggerSqlLog.debug(message);
    }
}

调用写日志测试
Java用log4j写日志_第3张图片

Java用log4j写日志_第4张图片

在Tomcat的Bin下的Logs生成日志
Java用log4j写日志_第5张图片

Java用log4j写日志_第6张图片

Java用log4j写日志_第7张图片

后面就把System.out.println的打印日志换为LIS.Core.Util.LogUtils写日志了

你可能感兴趣的:(java,log4j,开发语言)