Better Alfresco Script Logging

I was working on some web scripts recently and using log statements to help my development. Does it ever bother you that the only log level available to web scripts is DEBUG? It bugs me.

So I made a simple JavaScript extension that exposes the various log levels of the ScriptLogger to JavaScript.

If you are not familiar with creating Alfresco JavaScript extensions read the Alfresco JavaScript API wiki entry.

The Java class extends BaseScopableProcessorExtension. It provides two log methods for each of these log levels:

  • DEBUG
  • INFO
  • WARN
  • ERROR
  • FATAL

Here are the debug methods an example, the full source code is below:

public void debug(Object message) {
    logger.debug(message);
}

public void debug(Object message, Throwable t) {
    logger.debug(message, t);
}

As you will see below there is not much to the code.

I’ve called it atcLogger (ATC = Abstractive Technology Consulting)

This is nice if, for example, you have a server with the log level set to WARN and you want to quickly toss in a log statement. If you used logger.log(“Some message”) you wouldn’t see the output in the log since that uses DEBUG level. You could use atcLogger.warn(“Some message”) and this would appear in the log without having to change the log level on the server. Even if you change the log level through JMX it’s slightly more effort than lazy developers like me want to expend.

Here is the spring bean for the extension:

<bean id="atc_atcLogger" parent="baseJavaScriptExtension">
    <property name="extensionName">
        <value>atcLogger</value>
    </property>
</bean>

ATCLogger.java:

package ca.abstractive.ecm.alfresco.jscript.io;

import org.alfresco.repo.jscript.BaseScopableProcessorExtension;
import org.alfresco.repo.jscript.ScriptLogger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Extends Alfresco's JavaScript API to expose a wider range of
 * logging levels.
 *
 * Refer to this class in JavaScript using the alias "atcLogger".
 *
 * @author Tim.Frith
 */
public class ATCLogger extends BaseScopableProcessorExtension {

    // Commons logger
    protected static final Log logger = LogFactory.getLog(ScriptLogger.class);

    /**
     * Standard constructor
     */
    public ATCLogger() {
    }

    /* ------------------------------------------------------------ */
    /* Public methods - available to JavaScript */
    /* ------------------------------------------------------------ */

    /**
     * @see org.apache.log4j.Logger#debug(Object)
     */
    public void debug(Object message) {
        logger.debug(message);
    }

    /**
     * @see org.apache.log4j.Logger#debug(Object, Throwable)
     */
    public void debug(Object message, Throwable t) {
        logger.debug(message, t);
    }

    /**
     * @see org.apache.log4j.Logger#info(Object)
     */
    public void info(Object message) {
        logger.info(message);
    }

    /**
     * @see org.apache.log4j.Logger#info(Object, Throwable)
     */
    public void info(Object message, Throwable t) {
        logger.info(message, t);
    }

    /**
     * @see org.apache.log4j.Logger#warn(Object)
     */
    public void warn(Object message) {
        logger.warn(message);
    }

    /**
     * @see org.apache.log4j.Logger#warn(Object, Throwable)
     */
    public void warn(Object message, Throwable t) {
        logger.warn(message, t);
    }

    /**
     * @see org.apache.log4j.Logger#error(Object)
     */
    public void error(Object message) {
        logger.error(message);
    }

    /**
     * @see org.apache.log4j.Logger#error(Object, Throwable)
     */
    public void error(Object message, Throwable t) {
        logger.error(message, t);
    }

    /**
     * @see org.apache.log4j.Logger#fatal(Object)
     */
    public void fatal(Object message) {
        logger.fatal(message);
    }

    /**
     * @see org.apache.log4j.Logger#fatal(Object, Throwable)
     */
    public void fatal(Object message, Throwable t) {
        logger.fatal(message, t);
    }
}

Possible improvements:

  • expose isDebugEnabled, isInfoEnabled(), etc. to JavaScript
This entry was posted in Alfresco, ECM and tagged by tim.frith. Bookmark the permalink.
tim.frith

About tim.frith

Alfresco Certified Engineer

Senior ECM Consultant
Tim holds a diploma in Computer Systems Technology from Camosun College as well as a Bachelor of Commerce Degree with a Finance specialization from the University of Alberta. He has over 10 years of experience developing web applications with Java as well as extensive database development experience with Oracle, IBM DB2, MS SQL Server, and MySQL.

In recent years he has focused on Enterprise Content Management, with specific training and experience in Alfresco's content management suite.

And finally, Tim is a strong believer that the Edmonton Oilers will soon reclaim their rightful place at the top of the hockey world. Yes, Stanley will once again come to town.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>