zoukankan      html  css  js  c++  java
  • 日志

    1.使用springboot自带日志

    1.1 pom文件

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test.xmmc</groupId>
    <artifactId>test20191204</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--继承SpringBoot父类包-->
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    </parent>
    <!--项目编码设定-->
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    </properties>
    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>

    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.2</version>
    <scope>compile</scope>
    </dependency>


    </dependencies>

    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
    <finalName>operate_oss</finalName>
    <mainClass>com.tzj.operate.oss.Application</mainClass>
    </configuration>
    <executions>
    <execution>
    <goals>
    <goal>repackage</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    </configuration>
    </plugin>

    </plugins>
    </build>

    </project>

    1.2 logback-spring.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <!--https://blog.csdn.net/tianyaleixiaowu/article/details/73321610-->
    <configuration debug="true">
    <!-- 项目名称 -->
    <property name="PROJECT_NAME" value="ReptileAllocation"/>

    <!--定义日志文件的存储地址 勿在 LogBack 的配置中使用相对路径-->
    <property name="LOG_HOME" value="logs"/>

    <!-- 控制台输出 -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
    <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] %highlight([%-5level] %logger{50} - %msg%n)</pattern>
    <charset>UTF-8</charset>
    </encoder>
    </appender>

    <!-- 系统错误日志文件 -->
    <appender name="SYSTEM_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <!-- 过滤器,只打印ERROR级别的日志 -->
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
    <level>ERROR</level>
    <onMatch>ACCEPT</onMatch>
    <onMismatch>DENY</onMismatch>
    </filter>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
    <!--日志文件输出的文件名-->
    <FileNamePattern>${LOG_HOME}/error.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
    <!--日志文件保留天数-->
    <MaxHistory>7</MaxHistory>
    <!--日志文件最大的大小-->
    <MaxFileSize>10MB</MaxFileSize>
    </rollingPolicy>

    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
    <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] - %msg%n</pattern>
    <charset>UTF-8</charset>
    </encoder>
    </appender>
    <logger name="system_error" additivity="true">
    <appender-ref ref="SYSTEM_ERROR"/>
    </logger>

    <!-- 自己打印的日志文件,用于记录重要日志信息 -->
    <appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <!-- 过滤器,只打印INFO级别的日志 -->
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
    <level>INFO</level>
    <onMatch>ACCEPT</onMatch>
    <onMismatch>DENY</onMismatch>
    </filter>
    <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
    <!--日志文件输出的文件名-->
    <FileNamePattern>${LOG_HOME}/info.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
    <!--日志文件保留天数-->
    <MaxHistory>7</MaxHistory>
    <!--日志文件最大的大小-->
    <MaxFileSize>10MB</MaxFileSize>
    </rollingPolicy>

    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
    <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] - %msg%n</pattern>
    <charset>UTF-8</charset>
    </encoder>
    </appender>
    <logger name="info_file" additivity="true">
    <appender-ref ref="INFO_FILE"/>
    <appender-ref ref="CONSOLE"/>
    </logger>
    <root level="DEBUG">
    <appender-ref ref="CONSOLE"/>
    </root>
    </configuration>

    1.3 logUtils工具类

    package com.test.xmmc.common.util;

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


    public class LogUtil {


    private final static String LOG_INFO = "info_file";
    private final static String LOG_ERROR = "system_error";

    /**
    * 业务日志
    *
    * @param type 类别
    * @param operate 操作
    * @param info 日志内容
    */
    public static void info(String type, String operate, String info) {
    Logger logger = LoggerFactory.getLogger(LOG_INFO);
    StringBuffer sb = new StringBuffer();
    sb.append("[");
    sb.append(type);
    sb.append("-");
    sb.append(operate);
    sb.append("]");
    sb.append(info);
    logger.info(sb.toString());
    }


    /**
    * 将信息打印到自定义日志(system_error.log)中
    *
    * @param e 异常信息
    */
    public static void error(Exception e, Class<?> clazz) {
    Logger logger = LoggerFactory.getLogger(LOG_ERROR);

    StringBuffer logOut = new StringBuffer();
    logOut.append(" ");
    logOut.append(clazz.getName());
    logOut.append(e.toString());
    logOut.append(" ");

    StackTraceElement[] errorList = e.getStackTrace();
    for (StackTraceElement stackTraceElement : errorList) {
    logOut.append(stackTraceElement.toString());
    logOut.append(" ");
    }

    logOut.append(" ");
    logger.error(logOut.toString());
    }

    }

    1.4调用

    LogUtil.info("测试type","测试operate","测试nfo");

    2.使用spring boot:log4j日志记录模块

    2.1pom文件

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.test.xmmc</groupId>
    <artifactId>test20191204</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--继承SpringBoot父类包-->
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-parent</artifactId>
    <version>2.0.1.RELEASE</version>
    </parent>
    <!--项目编码设定-->
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    </properties>
    <dependencies>
    <!--spring boot:web启动加载-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
    <!--排除:自带的日志记录框架-->
    <exclusion>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logging</artifactId>
    </exclusion>
    </exclusions>
    </dependency>
    <!--spring boot:aop模块-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <!--spring boot:log4j日志记录模块-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j</artifactId>
    <version>1.3.8.RELEASE</version>
    </dependency>
    <!--spring boot:热部署-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
    </dependency>
    <!--spring boot:资源文件的读取映射成实体-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    </dependency>


    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.2</version>
    <scope>compile</scope>
    </dependency>


    </dependencies>

    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
    <finalName>operate_oss</finalName>
    <mainClass>com.tzj.operate.oss.Application</mainClass>
    </configuration>
    <executions>
    <execution>
    <goals>
    <goal>repackage</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
    <source>1.8</source>
    <target>1.8</target>
    </configuration>
    </plugin>

    </plugins>
    </build>

    </project>

    2.2log4j.properties配置

    ### ����̨���  ###
    log4j.rootLogger=INFO, stdout
    log4j.logger.stdout=INFO, stdout
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
    log4j.logger.api=INFO,api
    log4j.adaptivity.api=true
    log4j.appender.api=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.api.File=C:/Users/77071/Desktop/fsdownload/logs/api_
    log4j.appender.api.DatePattern=yyyy-MM-dd'.log'
    log4j.appender.api.layout=org.apache.log4j.PatternLayout
    log4j.appender.api.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %m%n
    log4j.appender.api.encoding=UTF-8
    log4j.appender.api.MaxFileSize=50MB
    # 单个日期周期类,保留的日志数量
    log4j.appender.api.MaxBackupIndex=-1
    # 总保留文件数量,-1表示不限
    log4j.appender.api.maxTotalFile=15
    log4j.logger.error=ERROR, error
    log4j.adaptivity.error=true
    log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.error.File=C:/Users/77071/Desktop/fsdownload/logs/error_
    log4j.appender.error.DatePattern=yyyy-MM-dd'.log'
    log4j.appender.error.layout=org.apache.log4j.PatternLayout
    log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %m%n
    log4j.appender.error.encoding=UTF-8
    log4j.appender.error.MaxFileSize=50MB
    # 单个日期周期类,保留的日志数量
    log4j.appender.error.MaxBackupIndex=-1
    # 总保留文件数量,-1表示不限
    log4j.appender.error.maxTotalFile=15

    2.3 工具类

    package com.test.xmmc.common.util;

    import lombok.Getter;

    import javax.servlet.http.HttpServletRequest;


    public class LogFactory {


    /**
    * 操作上下文
    */
    public static LogFactory Context = new LogFactory();

    @Getter
    private org.apache.log4j.Logger info;
    @Getter
    private org.apache.log4j.Logger smsGzcu;
    @Getter
    private org.apache.log4j.Logger smsBroadbandcu;
    private org.apache.log4j.Logger error;
    @Getter
    private org.apache.log4j.Logger red;

    public LogFactory() {
    info = org.apache.log4j.Logger.getLogger("api");
    error = org.apache.log4j.Logger.getLogger("error");
    smsGzcu = org.apache.log4j.Logger.getLogger("sms_gzcu");
    smsBroadbandcu = org.apache.log4j.Logger.getLogger("sms_broadbandcu");
    red = org.apache.log4j.Logger.getLogger("red");
    }

    /**
    * 添加错误信息
    *
    * @param source 来源数据
    * @param ex 异常信息
    * @param data 源数据
    * @param request 请求
    */
    public void addError(String source, Exception ex, Object data, HttpServletRequest request) {
    error.error("===========================");
    error.error("source:" + source);
    error.error("ip:" );
    error.error("url:" + (request != null ? request.getRequestURI() : "无"));
    error.error("param:" + (request != null ? request.getQueryString() : "无"));
    error.error("ex:" );
    error.error("data:" );
    error.error("=========================== ");
    }

    }

    2.4调用

    LogFactory.Context.getInfo().info("==========测试==========");

    3.结束

  • 相关阅读:
    linux下安装elasticsearch5.6.3
    linux下安装git
    环境安装备忘录 Zookeeper
    环境安装备忘录 JDK
    环境安装备忘录 Tomcat
    MySql 通过show status 优化数据库性能
    MySQL执行计划解读 转他人文章
    2015年12月21日 my.cnf 配置
    mysql 如何查看my.cnf的 位置
    mysql状态查看 QPS/TPS/缓存命中率查看
  • 原文地址:https://www.cnblogs.com/CookiesBear/p/11988308.html
Copyright © 2011-2022 走看看