logback为单独的包或者类配置输出文件
最近接一个这样的需求,为某个类的日志输出到指定的文件。一般都是按日志级别输出到对应的文件中。查阅相关资料和logback教程,写出下面的demo供参考。
1.添加一个appender
<appender name="demo—all" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${rootpath}gateserver_demo-all.logfile> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>DEBUGlevel> filter> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy" > <FileNamePattern>${rootpath}demo-all.%i.logFileNamePattern> <minIndex>1minIndex> <maxIndex>10maxIndex> rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>800MBmaxFileSize> triggeringPolicy> <encoder> <pattern>%date [%thread] %-5level %logger{80} - %msg%npattern> encoder> appender>
2 添加一个logger
additivity必须设置为false,否则不光新的jetty日志文件会存储jetty的日志,原业务日志(root)文件也会存储.不继承root。
这样就OK了。
以下是logback的常用配置:
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true" scanPeriod="60 seconds" debug="false"> <property name="log_dir" value="/logs/ev_cmdb" /> <property name="maxHistory" value="30"/> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger -%msg%npattern> encoder> appender> <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERRORlevel> <onMatch>ACCEPTonMatch> <onMismatch>DENYonMismatch> filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/error-log.logfileNamePattern> <maxHistory>${maxHistory}maxHistory> rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%npattern> encoder> appender> <appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>WARNlevel> <onMatch>ACCEPTonMatch> <onMismatch>DENYonMismatch> filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/warn-log.log fileNamePattern> <maxHistory>${maxHistory}maxHistory> rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%npattern> encoder> appender> <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>INFOlevel> <onMatch>ACCEPTonMatch> <onMismatch>DENYonMismatch> filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/info-log.log fileNamePattern> <maxHistory>${maxHistory}maxHistory> rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%npattern> encoder> appender> <appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>DEBUGlevel> <onMatch>ACCEPTonMatch> <onMismatch>DENYonMismatch> filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/debug-log.log fileNamePattern> <maxHistory>${maxHistory}maxHistory> rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%npattern> encoder> appender> <appender name="TRACE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>TRACElevel> <onMatch>ACCEPTonMatch> <onMismatch>DENYonMismatch> filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/trace-log.log fileNamePattern> <maxHistory>${maxHistory}maxHistory> rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%npattern> encoder> appender> <logger name="java.sql.PreparedStatement" value="DEBUG" /> <logger name="java.sql.Connection" value="DEBUG" /> <logger name="java.sql.Statement" value="DEBUG" /> <logger name="com.ibatis" value="DEBUG" /> <logger name="com.ibatis.common.jdbc.SimpleDataSource" value="DEBUG" /> <logger name="com.ibatis.common.jdbc.ScriptRunner" level="DEBUG"/> <logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate" value="DEBUG" /> <root level="debug"> <appender-ref ref="STDOUT" /> <appender-ref ref="ERROR" /> <appender-ref ref="INFO" /> <appender-ref ref="WARN" /> <appender-ref ref="DEBUG" /> <appender-ref ref="TRACE" /> root> configuration>
java代码:
import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class LogUtils { /** * 错误输入日志 */ public static final Logger log = LoggerFactory.getLogger(LogUtils.class); /** * 记录一直 info信息 * * @param message */ public static void logInfo(String message) { StringBuilder s = new StringBuilder(); s.append((message)); log.info(s.toString()); } public static void logInfo(String message, Throwable e) { StringBuilder s = new StringBuilder(); s.append(("exception : -->>")); s.append((message)); log.info(s.toString(), e); } public static void logWarn(String message) { StringBuilder s = new StringBuilder(); s.append((message)); log.warn(s.toString()); } public static void logWarn(String message, Throwable e) { StringBuilder s = new StringBuilder(); s.append(("exception : -->>")); s.append((message)); log.warn(s.toString(), e); } public static void logDebug(String message) { StringBuilder s = new StringBuilder(); s.append((message)); log.debug(s.toString()); } public static void logDebug(String message, Throwable e) { StringBuilder s = new StringBuilder(); s.append(("exception : -->>")); s.append((message)); log.debug(s.toString(), e); } public static void logError(String message) { StringBuilder s = new StringBuilder(); s.append(message); log.error(s.toString()); } /** * 记录日志错误信息 * * @param message * @param e */ public static void logError(String message, Throwable e) { StringBuilder s = new StringBuilder(); s.append(("exception : -->>")); s.append((message)); log.error(s.toString(), e); } }
测试:
import java.io.File; import java.io.IOException; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ch.qos.logback.core.joran.spi.JoranException; import com.ghca.easyview.server.common.utils.LogBackConfigLoader; import com.ghca.easyview.server.common.utils.LogUtils; import com.ghca.easyview.server.config.ServerConfig; public class TestLog { @Before public void before() { File file = new File(System.getProperty("user.dir")); String path = file.getPath() + File.separator + "conf"; // 将主要配置文件路径赋值给 ServerConfig.configPath属性 ServerConfig.config_path = path; try { // 加载日志文件 LogBackConfigLoader.load(ServerConfig .loadLogBackConfPath("logback.xml")); } catch (IOException | JoranException e) { e.printStackTrace(); } } @Test public void test1() { LogUtils.logInfo("INFO ~"); LogUtils.logDebug("DEBUG ~"); LogUtils.logError("ERROR~"); LogUtils.logWarn("WARN ~"); } }