• 打印日志


    日志的打印可以采用两种工具:一是log4j工具,另一个是使用logback工具。

    一:log4j工具与logback工具的比较

        logback的优势:

    (1) 可以配置文件大小,当超过该大小值,会自动备份成zip;通过设置TimeBasedRollingPolicy或者SizeAndTimeBasedFNATP的maxHistory属性,你可以控制已经产生日志文件的最大数量。如果设置maxHistory为12,那那些log文件超过12个月的都会被自动移除。

    (2) 修改配置文件不需要不需要重启,会自动生效;

    (3) Logback-classic非常自然实现了SLF4j,因此代码中书写的时候仅需要使用slf4j的api即可;当从logback切换到log4j的时候,代码部分不需要更改,仅需要更改依赖的jar包;

    二:log4j工具使用

    log4j.rootLogger= DEBUG,A1, A2,A4
    #log4j.category.com.hikvision=ERROR
    
    log4j.appender.A1=org.apache.log4j.RollingFileAppender
    log4j.appender.A1.Threshold=DEBUG 
    log4j.appender.A1.File=installlogs/debug.log
    log4j.appender.A1.MaxFileSize=5120KB
    log4j.appender.A1.MaxBackupIndex=10
    log4j.appender.A1.layout=org.apache.log4j.PatternLayout
    log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss.SSS} %p [%t] test-install <%F [%L] - %M> <%m>%n
    
    
    log4j.appender.A2=org.apache.log4j.ConsoleAppender
    log4j.appender.A2.Threshold=DEBUG
    log4j.appender.A2.layout=org.apache.log4j.PatternLayout
    log4j.appender.A2.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss.SSS} %p [%t] test-install <%F [%L] - %M> <%m>%n
    
    
    log4j.appender.A4=org.apache.log4j.RollingFileAppender
    log4j.appender.A4.Threshold=ERROR
    log4j.appender.A4.File=installlogs/error.log
    log4j.appender.A4.MaxFileSize=5120KB
    log4j.appender.A4.MaxBackupIndex=5
    log4j.appender.A4.layout=org.apache.log4j.PatternLayout
    log4j.appender.A4.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss.SSS} %p [%t] test-install <%F [%L] - %M> <%m>%n

    依赖包

    <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-log4j12</artifactId>
    </dependency>

    三:logback工具使用

    <?xml version="1.0" encoding="UTF-8"?>
    <!--
        Copyright 2010-2011 The myBatis Team
        Licensed under the Apache License, Version 2.0 (the "License");
        you may not use this file except in compliance with the License.
        You may obtain a copy of the License at
            http://www.apache.org/licenses/LICENSE-2.0
        Unless required by applicable law or agreed to in writing, software
        distributed under the License is distributed on an "AS IS" BASIS,
        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        See the License for the specific language governing permissions and
        limitations under the License.
    -->
    <configuration scanPeriod="60 seconds" debug="false">
        <property name="LOG_HOME" value="d:/disinstall-log/" />      //这里是可以使用相对路径的,如:value="disinstall-log/",这个是相对于类加载路径来说的
        
       <!-- 控制台输出日志 -->
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <layout class="ch.qos.logback.classic.PatternLayout">
                <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}[%line] - %msg%n</pattern>
            </layout>
        </appender>
      
        <!-- 文件输出日志 (文件大小策略进行文件输出,超过指定大小对文件备份)-->
        <appender name="FILE-debug" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>DEBUG</level>
            </filter>        
            <File>${LOG_HOME}/debug.log</File>        
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>${LOG_HOME}/debug.%d{yyyy-MM-dd}.%i.zip</FileNamePattern>
                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <maxFileSize>10MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
                <MaxHistory>3</MaxHistory>
            </rollingPolicy>    
            <layout class="ch.qos.logback.classic.PatternLayout">
                <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}[%line] - %msg%n</Pattern>
            </layout>
        </appender>    
        
        <appender name="FILE-error" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>ERROR</level>
            </filter>
            <File>${LOG_HOME}/error.log</File>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>${LOG_HOME}/error.%d{yyyy-MM-dd}.%i.zip</FileNamePattern>
                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                   <maxFileSize>10MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
                <MaxHistory>3</MaxHistory>
            </rollingPolicy>
            <layout class="ch.qos.logback.classic.PatternLayout">
                <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}[%line] - %msg%n</Pattern>
            </layout>
        </appender>
      
        <appender name="FILE-info" class="ch.qos.logback.core.rolling.RollingFileAppender">
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                <level>INFO</level>
            </filter>
            <File>${LOG_HOME}/info.log</File>
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                <FileNamePattern>${LOG_HOME}/info.%d{yyyy-MM-dd}.%i.zip</FileNamePattern>
                <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                    <maxFileSize>10MB</maxFileSize>
                </timeBasedFileNamingAndTriggeringPolicy>
                <MaxHistory>3</MaxHistory>
            </rollingPolicy>
            <layout class="ch.qos.logback.classic.PatternLayout">
                <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}[%line] - %msg%n</Pattern>
            </layout>
        </appender>
          
        <logger name="com.hikvision" level="DEBUG" />
    
        <!-- 日志输出级别 -->
        <root level="DEBUG">
            <appender-ref ref="STDOUT" />
            <appender-ref ref="FILE-info"/>
            <appender-ref ref="FILE-debug" />
            <appender-ref ref="FILE-error" />
        </root> 
        
    </configuration>

    依赖包

     <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
    </dependency>
    <dependency>
        <groupId>org.logback-extensions</groupId>
        <artifactId>logback-ext-spring</artifactId>
    </dependency>
  • 相关阅读:
    【Redis】集群NetCore实战
    【Redis】集群教程(Windows)
    【Redis】入门
    【SQL SERVER】索引
    【SQL SERVER】锁机制
    【SQL SERVER】数据内部存储结构简单探索
    Windows软件包管理工具
    Git常用命令记录
    【ASP.NET Core学习】远程过程调用
    CouchDB学习-API
  • 原文地址:https://www.cnblogs.com/sandyflower/p/6992591.html
Copyright © 2020-2023  润新知