Friday, February 4, 2011

Configure your Red5 application logging in red5 fashion (with real live example)

I found a post on Paul Gregoire’s Blog. Although it's old, it surprisingly applies to newer versions of Red5.

This is what I managed to have for Red5 0.9.1

My application path is D:\work\red5_9_1\webapps\crilance

So, I edit D:\work\red5_9_1\webapps\crilance\WEB-INF\web.xml and add this at the bottom

    <listener>
        <listener-class>org.red5.logging.ContextLoggingListener</listener-class>
    </listener>


Then I need a configure file for the logging, so I create D:\work\red5_9_1\crilance\WEB-INF\classes\logback-live.xml and write on the file:


<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <appender name="crilance" class="ch.qos.logback.core.FileAppender">
        <File>c:/red5/log/crilance.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>
        <appender-ref ref="crilance"/>
    </root>

    <logger name="com.crilance.core">
        <level value="DEBUG"/>
    </logger>
</configuration>



com.crilance.core is my package where Red5 ApplicationAdapter is.

Log suppose to go under
c:/red5/log/crilance.log

Now I need to edit my application to create a logging instance
public class Application extends ApplicationAdapter {
    public static Logger log;
    static {
        ContextSelector selector = StaticLoggerBinder.getSingleton().getContextSelector();
        LoggerContext ctx = selector.getLoggerContext("crilance");
        log = ctx.getLogger(Application.class);
    }


Application is whatever class you create for your web application

Again, crilance is the context I create my app in


Now you just need to import the logging libraries:
import org.slf4j.Logger;
import org.slf4j.impl.StaticLoggerBinder;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.selector.ContextSelector;





I use
logback-classic-0.9.18.jar
logback-core-0.9.18.jar
slf4j-api-1.6.1.jar



That's pretty much it.


To test:
    /** {@inheritDoc} */
    @Override
    public boolean appStart(IScope app) {
        appScope = app;
        System.out.println("application: start; scope "+app.getName());//OLD WAY
        Application.log.debug("LOG WORKS");
    }


When opening  c:/red5/log/crilance.log I can see this


2011-02-04 11:20:34,221 [main[ DEBUG ROOT | Starting up context crilance
2011-02-04 11:20:34,674 [Launcher:/crilance[ DEBUG com.crilance.core.Application | LOG WORKS



You can always change D:\work\red5_9_1\crilance\WEB-INF\classes\logback-live.xml and put  ConsoleAppender instead of FileAppender to get your logs into the IDE(I use Eclipse) console


<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <appender name="crilance" class="ch.qos.logback.core.ConsoleAppender">

.....

hth,
Crirus

No comments:

Post a Comment