How can I setup my BlazeDS implementation with Log4J?
Asked Answered
J

3

8

I'm writing a Flex app on top of a Java web application using BlazeDS. BlazeDS has logging inside of it, but I want to set it up to Use the same logging framework I have in my application.

Is there a way to setup BlazeDS to use Log4J? Or am I stuck with the Flex logging stuff that's already baked into BlazeDS?

Jury answered 9/4, 2009 at 14:27 Comment(0)
E
13

No, out of box BlazeDS does not support log4j or other frameworks directly.

However, it's really simple to add support for your favourite logging framework; I used the following to get the output into SLF4J:

package example;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import flex.messaging.log.AbstractTarget;
import flex.messaging.log.LogEvent;

public class Slf4jTarget extends AbstractTarget {
    // log4j levels:   OFF - FATAL - ERROR - WARN - INFO - DEBUG - TRACE - ALL
    // blazeds levels:  NONE - FATAL - ERROR - WARN - INFO - DEBUG - ALL

    @Override
    public void logEvent(LogEvent event) {
        Logger log = LoggerFactory.getLogger(event.logger.getCategory());

        if (event.level >= LogEvent.ERROR)
            log.error(event.message, event.throwable);
        else if (event.level >= LogEvent.WARN)
            log.warn(event.message, event.throwable);
        else if (event.level >= LogEvent.INFO)
             log.info(event.message, event.throwable);
        else if (event.level >= LogEvent.DEBUG)
             log.debug(event.message, event.throwable);
        else
             log.trace(event.message, event.throwable);
    }
}

.. and to use it, enable it in services-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<services-config>
    <logging>
        <target class="example.Slf4jTarget" level="Info">
    </logging>
</services-config>
Evasive answered 9/4, 2009 at 22:38 Comment(2)
This answer is great. My comments: 1) I had to override equals() in my Slf4jTarget to return true for any other instance of the same type to prevent BlazeDS from registering that target twice 2) I prefixed the event.logger.getCategory() string with "BlazeDS." to have a better namespace handle when configuring slf4j 3) I truncate DEBUG messages to 1000 characters since BlazeDS dumps all in- and outgoing dataGrondin
It's also convenient to call super() from constructorHoggard
A
4

Use CommonsLoggingTarget.

See http://static.springsource.org/spring-flex/docs/1.0.x/javadoc-api/org/springframework/flex/core/CommonsLoggingTarget.html.

Just be careful with setting log level in service-config.xml. If you set it to "All", and define "blazeds" logger in log4j.xml, then there will be a lot of redundant log messages generated by BlazeDS/LCDS, which might have significant performance impact.

Araucania answered 19/1, 2010 at 19:10 Comment(1)
Also has useful documentation on all BlazeDS categoriesGrondin
O
1

I don't believe that there is anything built-in that allows you to redirect Blaze DS logging output to log4j, commons-logging, etc. However this JIRA issue may be of use to you:

http://jira.springframework.org/browse/FLEX-18

Includes a Java class to redirect output and sample configuration for services-config.xml

Osbourne answered 9/4, 2009 at 22:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.