<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.4</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.0.6</version> </dependency>2. ให้นำ configuration ต่อไปนี้ (logback.xml) ไปวางใน Other Sources หรือ Class Path
<?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="ROOT_PATH" value="/ServerLog" /> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern> %d{HH:mm:ss.SSS} %level [%thread] %logger{10} [%file:%line] %msg%n </Pattern> </layout> </appender> <appender name="FILE" class="ch.qos.logback.core.FileAppender"> <file>${ROOT_PATH}/logbackLearning.log</file> <encoder> <pattern> %d{HH:mm:ss.SSS} %level [%thread] %logger{10} [%file:%line] %msg%n </pattern> </encoder> </appender> <logger name="com.blogspot.na5cent" level="TRACE"/> <root level="debug"> <appender-ref ref="STDOUT"/> <appender-ref ref="FILE"/> </root> </configuration>
3. เขียน java code
package com.blogspot.na5cent.logbacklearning; import java.io.Serializable; import java.util.Date; import javax.annotation.PostConstruct; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @author Redcrow */ @ManagedBean @RequestScoped public class LogbackTestingMB implements Serializable { private static final Logger LOG = LoggerFactory.getLogger(LogbackTestingMB.class); @PostConstruct public void postConstruct() { LOG.debug("start logger on {}", new Date()); } public String getHelloWorld() { return "hello world!"; } }
log ออก file
ไม่มีความคิดเห็น:
แสดงความคิดเห็น