java日志工具类

想法

软件运行需要记录日志,然后有的时候公司里的日志就很low,全部都写在的info里面。看着难受。
因此我需要想一下怎么写日志。想法如下:

  1. 日志分级别,info日志写在info日志文件中;warn日志写在warn文件中。
  2. 提供一个日志Util可以直接使用,不需要每次日志还需要new一个对象

logback配置每个日志等级一个文件



<configuration scan="true" scanPeriod="60 seconds" debug="false" packagingData="false">
    <property name="LOG_HOME" value="logs" />

    <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [%X{traceId}] %msg%npattern>
        layout>
    appender>

    <appender name="request" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <File>${LOG_HOME}/request.logFile>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFOlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [%X{traceId}] %msg%npattern>
            
        encoder> 
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 
            <fileNamePattern>${LOG_HOME}/%d{yyyy-MM-dd_HH-mm,aux}/request.%d{yyyy-MM-dd_HH}.log.gzfileNamePattern>
            <maxHistory>4maxHistory>
        rollingPolicy>
    appender>

    
    <appender name="info" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_HOME}/info.logFile>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFOlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [%X{traceId}] %msg%npattern>
            
        encoder> 
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 
            <fileNamePattern>${LOG_HOME}/logs/%d{yyyy-MM-dd,aux}/info.%d{yyyy-MM-dd_HH-mm}.log.gzfileNamePattern>
            <maxHistory>4maxHistory>
        rollingPolicy>
    appender>

    <appender name="error" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${LOG_HOME}/error.logFile>
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERRORlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [%X{traceId}] %msg%npattern>
            <charset>UTF-8charset>
        encoder> 
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 
            <fileNamePattern>${LOG_HOME}S/%d{yyyy-MM-dd,aux}/error.%d{yyyy-MM-dd_HH}.log.gzfileNamePattern>
            <maxHistory>4maxHistory>
        rollingPolicy>
    appender>

    <logger name="requestLogger" additivity="false" level="INFO">
        <appender-ref ref="request"/>
    logger>

    <logger name="infoLogger" additivity="false" level="INFO">
        <appender-ref ref="info"/>
    logger>

    <logger name="errorLogger" additivity="false" level="ERROR">
        <appender-ref ref="error"/>
    logger>

    <root level="info">
        <appender-ref ref="consoleLog"/>
    root>

configuration>

java日志工具类

需要用到java的堆栈

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LogUtils {
    private static final Logger infoLogger = LoggerFactory.getLogger("infoLogger");
    private static final Logger requestLogger = LoggerFactory.getLogger("requestLogger");
    private static final Logger errorLogger = LoggerFactory.getLogger("errorLogger");

    public static void info(String msg) {
        StringBuilder builder = getStringBuilder();
        infoLogger.info(builder.append(msg).toString());
    }

    public static void request(String msg) {
        StringBuilder builder = getStringBuilder();
        requestLogger.info(builder.append(msg).toString());
    }

    public static void error(String msg) {
        StringBuilder builder = getStringBuilder();
        errorLogger.error(builder.append(msg).toString());
    }
    
    private static StringBuilder getStringBuilder() {
        StackTraceElement[] stackTraceArr = Thread.currentThread().getStackTrace();
        StackTraceElement stackTrace = stackTraceArr[3];
        String className = stackTrace.getClassName();
        int lineNumber = stackTrace.getLineNumber();
        String methodName = stackTrace.getMethodName();
        StringBuilder builder = new StringBuilder();
        builder.append("[").append(className).append(".").append(methodName).append("] ")
                .append(lineNumber)
                .append(" - ");
        return builder;
    }
}

使用

    @RequestMapping("/log")
    public String log() {
        autoInterface.test();
        for(int i = 0 ; i < 1000; ++i) {
            LogUtils.error("-------error");
            LogUtils.info("-------info");
            LogUtils.request("-------re");
        }
        return "------";
    }

你可能感兴趣的:(jave,web,java,python,前端)