<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
<dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>org.logback-extensions</groupId> <artifactId>logback-ext-spring</artifactId> <version>0.1.2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.12</version> </dependency>
<?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="SYS_LOG_DIR" value="d:/WEBLOAN" /> <property name="LOG_FILE" value="webloan.log" /> <property name="fileLayoutPattern" value="%-20(%d{yyyyMMdd_HH:mm:ss.SSS} [%logger{10}][%thread] [%-4relative]) [%X{hostName} %X{requestId} %X{processId} %X{service.timing} ][%level] %msg%n" /> <property name="consoleLayoutPattern" value="%-20(%d{yyyyMMdd_HH:mm:ss.SSS} [%logger][%thread] [%-4relative]) [%X{hostName} %X{requestId} %X{processId} %X{service.timing} ][%level] %msg%n" /> <appender name="LOG_ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${SYS_LOG_DIR}/${LOG_FILE}</file> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <!-- 日誌過濾器,只記錄warn級別日誌 ,若要配置其餘級別,複製appender,修改level,並在root中配置--> <level>WARN</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${SYS_LOG_DIR}/%d{yyyy-MM-dd}/${LOG_FILE}_%d{yyyy-MM-dd}_%i.zip</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>50MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <layout> <pattern>${fileLayoutPattern}</pattern> </layout> </appender> <!-- 控制檯輸出 --> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <layout name="StandardFormat" class="ch.qos.logback.classic.PatternLayout"> <pattern>${consoleLayoutPattern}</pattern> </layout> </appender> <logger name="java.sql.PreparedStatement" level="DEBUG" /> <logger name="java.sql.Connection" level="DEBUG" /> <logger name="java.sql.Statement" level="DEBUG" /> <logger name="com.ibatis" level="DEBUG" /> <logger name="com.ibatis.common.jdbc.SimpleDataSource" level="DEBUG" /> <logger name="com.ibatis.common.jdbc.ScriptRunner" level="DEBUG" /> <logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate" level="DEBUG" /> <!-- <logger name="org.springframework" level="ERROR" /> --> <logger name="net.sf.ehcache" level="ERROR"/> <!-- <logger name="org.mybatis.spring" level="ERROR"/> --> <root level="DEBUG"> <appender-ref ref="CONSOLE" /> <!-- 控制檯輸出 --> <appender-ref ref="LOG_ROLLING" /> <!-- 文件輸出 --> </root> </configuration>
參考:java
Logback是由log4j創始人設計的又一個開源日誌組件。logback當前分紅三個模塊:logback-core,logback- classic和logback-access。logback-core是其它兩個模塊的基礎模塊。web
logback-classic是log4j的一個 改良版本。此外logback-classic完整實現SLF4J API使你能夠很方便地更換成其它日誌系統如log4j或JDK14 Logging。logback-access訪問模塊與spring
Servlet容器集成提供經過Http來訪問日誌的功能。sql
logback.xml配置以下:mybatis
<?xml version="1.0" encoding="UTF-8"?> <!-- 從高到地低 OFF 、 FATAL 、 ERROR 、 WARN 、 INFO 、 DEBUG 、 TRACE 、 ALL --> <!-- 日誌輸出規則 根據當前ROOT 級別,日誌輸出時,級別高於root默認的級別時 會輸出 --> <!-- 如下 每一個配置的 filter 是過濾掉輸出文件裏面,會出現高級別文件,依然出現低級別的日誌信息,經過filter 過濾只記錄本級別的日誌--> <!-- 屬性描述 scan:性設置爲true時,配置文件若是發生改變,將會被從新加載,默認值爲true scanPeriod:設置監測配置文件是否有修改的時間間隔,若是沒有給出時間單位,
默認單位是毫秒。當scan爲true時,此屬性生效。默認的時間間隔爲1分鐘。 debug:當此屬性設置爲true時,將打印出logback內部日誌信息,實時查看logback運行狀態。默認值爲false。 --> <configuration scan="true" scanPeriod="60 seconds" debug="false"> <!-- 定義日誌文件 輸入位置 --> <property name="log_dir" value="/logs/ev_cmdb" /> <!-- 日誌最大的歷史 30天 --> <property name="maxHistory" value="30"/> <!-- ConsoleAppender 控制檯輸出日誌 --> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <!-- 對日誌進行格式化 --> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger -%msg%n</pattern> </encoder> </appender> <!-- ERROR級別日誌 --> <!-- 滾動記錄文件,先將日誌記錄到指定文件,當符合某個條件時,將日誌記錄到其餘文件 RollingFileAppender--> <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 過濾器,只記錄WARN級別的日誌 --> <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.TimeBasedRollingPolicy"> <!--日誌輸出位置 可相對、和絕對路徑 --> <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/error-log.log</fileNamePattern> <!-- 可選節點,控制保留的歸檔文件的最大數量,超出數量就刪除舊文件假設設置每一個月滾動,且<maxHistory>是6, 則只保存最近6個月的文件,刪除以前的舊文件。注意,刪除舊文件是,那些爲了歸檔而建立的目錄也會被刪除--> <maxHistory>${maxHistory}</maxHistory> </rollingPolicy> <!-- 按照固定窗口模式生成日誌文件,當文件大於20MB時,生成新的日誌文件。窗口大小是1到3,當保存了3個歸檔文件後,將覆蓋最先的日誌。 <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/.log.zip</fileNamePattern> <minIndex>1</minIndex> <maxIndex>3</maxIndex> </rollingPolicy> --> <!-- 查看當前活動文件的大小,若是超過指定大小會告知RollingFileAppender 觸發當前活動文件滾動 <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <maxFileSize>5MB</maxFileSize> </triggeringPolicy> --> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern> </encoder> </appender> <!-- WARN級別日誌 appender --> <appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 過濾器,只記錄WARN級別的日誌 --> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>WARN</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 按天回滾 daily --> <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/warn-log.log </fileNamePattern> <!-- 日誌最大的歷史 60天 --> <maxHistory>${maxHistory}</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern> </encoder> </appender> <!-- INFO級別日誌 appender --> <appender name="INFO" 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.TimeBasedRollingPolicy"> <!-- 按天回滾 daily --> <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/info-log.log </fileNamePattern> <!-- 日誌最大的歷史 60天 --> <maxHistory>${maxHistory}</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern> </encoder> </appender> <!-- DEBUG級別日誌 appender --> <appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 過濾器,只記錄DEBUG級別的日誌 --> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>DEBUG</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 按天回滾 daily --> <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/debug-log.log </fileNamePattern> <!-- 日誌最大的歷史 60天 --> <maxHistory>${maxHistory}</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern> </encoder> </appender> <!-- TRACE級別日誌 appender --> <appender name="TRACE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- 過濾器,只記錄ERROR級別的日誌 --> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>TRACE</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- 按天回滾 daily --> <fileNamePattern>${log_dir}/%d{yyyy-MM-dd}/trace-log.log </fileNamePattern> <!-- 日誌最大的歷史 60天 --> <maxHistory>${maxHistory}</maxHistory> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern> </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級別 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>
LogUtils.javaapp
package log.base; package com.ghca.easyview.server.common.utils; 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); } }
TESTpost
package com.ghca.easyview.server.log.test; 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 ~"); } }
根據日誌級別輸出不等的日誌信息,ui