Usage example


First of all, in order to demonstrate the use of multiple loggers, modify the log4j.xml configuration content as follows:

  <?xml version="1.0" encoding="UTF-8"?>
  <Configuration>
      <Appenders>
          <RollingFile name="default" fileName="${sys:LOG_OUT_DIR}/default.log"
                       filePattern="${sys:LOG_OUT_DIR}/$${date:yyyy-MM}/default-%d{MM-dd-yyyy}-%i.log.gz">
              <PatternLayout pattern="%m %n" charset="UTF-8"/>
              <SizeBasedTriggeringPolicy size="500 MB"/>
          </RollingFile>

          <RollingFile name="wechat" fileName="${sys:LOG_OUT_DIR}/wechat.log"
                       filePattern="${sys:LOG_OUT_DIR}/$${date:yyyy-MM}/wechat-%d{MM-dd-yyyy}-%i.log.gz">
              <PatternLayout pattern="%m %n" charset="UTF-8"/>
              <SizeBasedTriggeringPolicy size="500 MB"/>
          </RollingFile>

      </Appenders>
      <Loggers>

          <Logger name="wechat" level="debug">
              <AppenderRef ref="wechat"/>
          </Logger>

          <!-- 配置记录器级别 -->
          <Root level="debug">
              <!-- 输出设置 -->
              <AppenderRef ref="default"/>
          </Root>
      </Loggers>
  </Configuration>

There are two loggers configured in the above configuration file:

  • default: the default root logger, which will record all log contents;
  • wechat: a custom logger ;

Sample code:

  • Use default logger output:

    Logs.get().getLogger().debug("日志将被输出到default.log文件...");
    Logs.get().getLogger().debug("日志内容", e);

    Note: The default logger is specified by the logger_name parameter, and the default value is default;

  • Output the log to the wechat.log file:

    ILogger _wechat = Logs.get().getLogger("wechat");
    _wechat.debug("日志将被分别输出到wechat.log和default.log文件中");
    //
    if (_wechat.isDebugEnabled()) {
        _wechat.debug("日志内容", e);
    }
    
    // 或者
    Logs.get().getLogger("wechat").info("日志内容");