按這幾篇文章配置,基本上就能搞定了。 一、第一篇中文的,開始。。。。 首先不能完全 按照log4j的配置去搞log4j.properties,因為他們是用 log4j+slf4j 搞在一起的 在 classes下面要有個logback-myapp.xml的檔案,裡面配置 <?xml version=”1.0″ encoding=”UTF-8″?> <configuration> <appender name=”RED5DEMO” class=”ch.qos.logback.core.FileAppender”> <File>log/red5demo.log</File> <Append>false</Append> <Encoding>UTF-8</Encoding> <BufferedIO>false</BufferedIO> <ImmediateFlush>true</ImmediateFlush> <layout class=”ch.qos.logback.classic.PatternLayout”> <Pattern> %date [%thread] %-5level %logger{35} - %msg%n </Pattern> </layout> </appender> <root> <level value=”DEBUG” /> <appender-ref ref=”RED5DEMO” /> </root> <logger name=”org.red5.demos.red5demo”> <level value=”DEBUG” /> </logger> </configuration> 重啟red5 在 red5/log 下看到一個red5demo.log檔案了。 二.再看另外一篇文章: 1、概述: Red5起初使用的log4j日誌系統,現在使用的是logback日誌系統。 Ceki在Java日誌領域世界知名。他創造了Log4J,這個最早的Java日誌架構即便在JRE內建日誌功能的競爭下 仍然非常流行。隨後他又著手實現SLF4J這個“簡單的日誌前端介面(simple logging facade for java)”來替代JCL(Jakarta Commons-Logging)。這幾年Ceki在從事他的新項目,LOGBack,一個“可靠、通用、快速而又靈活的Java日誌架構”。 主要函數有: package org.slf4j; public interface Logger { // Printing methods: public void trace(String message); public void debug(String message); public void info(String message); public void warn(String message); public void error(String message); } 主要記錄層級有:TRACE DEBUG INFO WARN ERROR OFF 2、Red5LoggerFactory類: red5-svn/java/server/trunk/src/org/red5/logging/Red5LoggerFactory檔案中定義了”Red5LoggerFactory”類。 它簡化了擷取日誌執行個體的請求操作,在Red5應用程式中推薦使用。 public class Red5LoggerFactory { public static Logger getLogger(Class clazz) { String contextName = null; return getLogger(clazz, contextName); } public static Logger getLogger(Class clazz, String contextName) { … } } getLogger()是個重載函數。 *)如果只傳遞一個參數,則返回預設context的日誌系統,即”red5.log”。 Red5源碼中一般都是調用第一個函數,如下所示: private static Logger log = Red5LoggerFactory.getLogger(TomcatLoader.class); *)如果傳遞兩個參數,則返回指定webapp context的日誌系統,即”xxx.log”。 Red5的應用程式一般都是調用第二個函數,如下所示: private static Logger log = Red5LoggerFactory.getLogger(Application.class, “oflaDemo”); (這裡有一點要注意,xuggle的例子貌似調用的不對: final private Logger log = Red5LoggerFactory.getLogger(this.getClass());) 3、注意事項: *)只要修改了web.xml檔案,添加了日誌偵聽,就可產生指定的記錄檔xxx.log。 *)logback-webapp.xml這個設定檔必要要放對位置,即jar打包檔案的根目錄或者class目錄下。 (否則即使產生了xxx.log檔案,後續日誌也無法寫進去。) *)java來源程式裡要正確返回指定的日誌系統,即使用Red5LoggerFactory類的兩個參數的getLogger函數。 (否則返回的是Red5預設的日誌系統,即寫到red5.log檔案裡) 三、具體怎麼用寫日誌,再看: Logging Setup The logging system uses Simple Logging Facade for Java ( SLF4J). This framework supports many of the logging systems available for Java and also provides simple implementations. The logging used by our dependencies are mainly Log4j and Apache commons logging and SLF4J allows us to combine them into one system. SLF4J gives you the ability to select a logging implementation and provides proxies for you dependencies if their maintainers did not select the same framework. We prefer the logback log implementation, but you may use whatever you like. There are some hoops you will have to jump through to get Log4j or Commons logging to work. Blog post about using other loggers here. After you chose an implementation framework, some of the SLF4J jars must NOT be in your applications classpath or they will cause conflicts. The default case it to use Logback, so the following jars must be included: slf4j-api - The core APIlogback-core - Current Logback core librarylogback-classic - Logback support librarylog4j-over-slf4j - Log4j proxy/bridgejcl-over-slf4j - Apache commons logging proxy/bridgejul-to-slf4j - java.util.logging proxy/bridge The items denoted as “proxy/bridge” listen for the logging calls to those implementations and pass them through to SLF4J. The following two strategies are to be consider untested. If you prefer to use Log4j instead, the following jars are required: slf4j-api - The core APIlog4j - Current Log4j library (1.2+)slf4j-log4j12 - Log4j adapterjcl-over-slf4j - Apache commons logging proxy/bridgejul-to-slf4j - java.util.logging proxy/bridge If you prefer to use Commons logging the following jars are required: slf4j-api - The core APIcommons-logging - Apache commons logging libraryslf4j-jcl - Commons logging adapterlog4j-over-slf4j - Log4j proxy/bridgejul-to-slf4j - java.util.logging proxy/bridge If you want to use another implementation not shown here, simply check out the faq SLF4J FAQ Logback is the successor of Log4j and was created by the creator of Log4j and SLF4J. A conversion tool has been created for your log4j properties files configuration converter There is also an eclipse console plugin eclipse console plugin. Web applications In your web applications remove the following entry from your web.xml <context-param><param-name>log4jConfigLocation</param-name><param-value>/WEB-INF/log4j.properties</param-value></context-param>Add the following to the web.xml <listener><listener-class>org.red5.logging.ContextLoggingListener</listener-class></listener><filter><filter-name>LoggerContextFilter</filter-name><filter-class>org.red5.logging.LoggerContextFilter</filter-class></filter><filter-mapping><filter-name>LoggerContextFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>You should also: Remove any “log4j” listeners from the web.xmlRemove any log4j.properties or log4j.xml filesCreate a logback-myApp.xml where myApp is the name for your webapp and place it on your webapp classpath (WEB-INF/classes or in your application jar within WEB-INF/lib)Set your display-name in the web application to match the context name you will be using (Use the example oflaDemo as a guide).Ensure that the contextName and jmxConfigurator have the correct context name, this is the name of your web application Sample webapp logback config file (logback-myApp.xml), not to be confused with the red5 log config file located in /conf <?xml version=”1.0″ encoding=”UTF-8″?><configuration><contextName>myApp</contextName><jmxConfiguratorcontextName=“myApp”/><appendername=“FILE”class=“ch.qos.logback.core.FileAppender”><File>example.log</File><Append>false</Append><BufferedIO>false</BufferedIO><ImmediateFlush>true</ImmediateFlush><layoutclass=“ch.qos.logback.classic.PatternLayout”><Pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</Pattern></layout></appender><root><levelvalue=“DEBUG“/><appender-refref=“FILE”/></root><loggername=“com.example”><levelvalue=“DEBUG“/></logger></configuration>Reminder replace everything that says “myApp” with your application name. Imports When using logback and slf4j, your imports should consist only of the following for a non webapp class: importorg.slf4j.LoggerFactory;importorg.slf4j.Logger;It is suggested that you use Red5LoggerFactory in-place of LoggerFactory to ensure that your application gets the correct logger. For loggers inside your webapp imports should be: importorg.slf4j.Logger;importorg.red5.logging.Red5LoggerFactory;Logger Instantiation For non webapp classes: To log to a “root” logger, change all your logger instantiation statements to: privatestaticLogger log=Red5LoggerFactory.getLogger(MyClassName.class);Reminder replace “MyClassName” with the name of the class itself. To log to a “context” logger, change all your logger instantiation statements to: privatestaticLogger log=Red5LoggerFactory.getLogger(MyClassName.class,“myApp”);Reminder replace “myApp” with the name of the context; “myApp” would become “oflaDemo” for the oflaDemo application. Your old instantiations probably resemble this: privatestaticLogger log=Logger.getLogger(MyClassName.class.getName());Your applications logging configuration file must contain the name of your application context in its file name; For instance the “oflaDemo” uses the configuration logback-oflaDemo.xml. Lastly, as an optimation change your log statements to: log.debug(“Here is a log message for an object {}”, myobject);You no longer need to concatenate strings when logging, if you need more than one variable do the following: log.debug(“Here is a log message with a couple vars {} or {} or {}”,new Object[]{object1, myobject, object3}); |