Use logback to easily manage logs

Source: Internet
Author: User

Recently, logback was used in the project, and it was very easy to meet the following requirements:

1. Easy to use

2. One log file can contain only one level of logs.

3. Multiple different logs can be specified in a class, and each generated log file only contains its own content.

4. You can disable or open the logs of several packages, and set different packages to use different log levels.

I. ease of use

Logback is easy to use. You can output logs in the console using the following two lines:

Logger debugLogger = LoggerFactory.getLogger(MyClass.class);

Logger.info ("this is a log ");
When the program is running, logback looks for the default configuration file logback. xml or logback-test.xml file. If it is not found, it prints the log to the console with the default configuration. Below is a simple configuration file (http://logback.qos.ch/manual/configuration.html ):

<configuration>  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">    <!-- encoders are assigned the type         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->    <encoder>      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>    </encoder>  </appender>  <root level="debug">    <appender-ref ref="STDOUT" />  </root></configuration>

Ii. Log Level restrictions

In most log tools, the level can be passed. For example, if the log output level is set to debug, logs of info and error levels will also appear in log files. By default, this causes a lot of trouble for program debugging. In logback, you can useAppenderInFilterTo strictly limit the log output level:

<filter class="ch.qos.logback.classic.filter.LevelFilter"><level>INFO</level><onMatch>ACCEPT</onMatch><onMismatch>DENY</onMismatch></filter>

In the above settings, only the log content with the info level will appear in the file.

3. The same class contains different logs

Sometimes a class may require printing different log information, for example, some for debugging, some for recording some parameter changes in the program running, and so on. In this case, you can declare different logs using the following statement:

Logger debugLogger = LoggerFactory.getLogger(MyClass.class);Logger monitorLogger = LoggerFactory.getLogger("monitor");

Specify different output files in the configuration file (debuglogger uses the default configuration ):

<appender name="monitor" class="ch.qos.logback.core.rolling.RollingFileAppender"><File>${log.dir}/monitor.log</File><encoder><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} : %m%n</pattern></encoder><filter class="ch.qos.logback.classic.filter.ThresholdFilter"><level>INFO</level></filter><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><fileNamePattern>${log.dir}/sensitive.log.%d{yyyy-MM-dd}</fileNamePattern></rollingPolicy></appender>        <logger name="monitor" additivity="false" level="INFO"><appender-ref ref="monitor" /></logger>

SetAdditivity = "false"Disable the upload of the content in monitor. Otherwise, the content is displayed in the default log.

Iv. Precise Control of log Application Scope

During program debugging, errors are often found only in one or more classes or packages. Therefore, you only need to open the debug-level logs of these classes or packages. In previous projects, when using spring and hibernate, once the debug-level log is enabled, the debug information of the program itself will be overwhelmed by a large number of logs from spring and hibernate, this greatly reduces the debugging efficiency. Logback makes it easy:

<logger name="org" level="ERROR" />

This line sets all log levels under the org package to error and will not disturb our debug.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.