欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

转几篇Red5的日志配置

shiping1 的头像
按这几篇文章配置,基本上就能搞定了。

一、第一篇中文的,开始。。。。

首先不能完全 按照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});

来自 http://blog.csdn.net/whycold/article/details/6142480
普通分类: