转自:http://sungang-1120.iteye.com/blog/2104296
Logback是由log4j创始人设计的又一个开源日志组件。logback当前分成三个模块:logback-core,logback- classic和logback-access。logback-core是其它两个模块的基础模块。logback-classic是log4j的一个 改良版本。此外logback-classic完整实现SLF4J API使你可以很方便地更换成其它日志系统如log4j或JDK14 Logging。logback-access访问模块与Servlet容器集成提供通过Http来访问日志的功能。
logback.xml配置如下:
- xml version="1.0" encoding="UTF-8"?>
- <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>
- <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>
LogUtils.java
- 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);
- }
- }
TEST
- 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 ~");
- }
- }
根据日志级别输出不等的日志信息,