Jan Nowak
Jan Nowak

Reputation: 129

Liferay, how to get root logger?

I'm trying to develop portlet which sends email to administrators when a set of specified excpetions occured in a given time period. I'm trying to get root logger in liferay so i can add my appender to it and process all messages going trough the logging mechanism. I looked into source code and seems that liferay uses java.util.logging.LogManager. I made a hook which is fired when server starts up. Here is my run() method:

public void run(String[] ids) throws ActionException {
    System.out.println("initializing");     

    LogListener listener = new LogListener();
    listener.setLevel(Level.ALL);
    listener.setFilter(null);
    Logger.getGlobal().addHandler(listener);

    _log.debug("complete");
}

And LogListener class

package pl.com.mds.portlet.mailing.hook;

import java.util.logging.Handler;
import java.util.logging.LogRecord;

public class LogListener extends Handler {

    @Override
    public void publish(LogRecord record) {
        System.out.println("publishing******");

    }

    @Override
    public void flush() {
        // TODO Auto-generated method stub
        System.out.println("have to flush! *****");
    }

    @Override
    public void close() throws SecurityException {
        // TODO Auto-generated method stub
        System.out.println("close meee!");
    }

}

But when some exception is thrown i cant see in console publishing****** only exception stacktrace. How can i get all logs in application and expceptions? Thanks :)

Upvotes: 0

Views: 677

Answers (2)

Paras
Paras

Reputation: 824

Consider using LogFactoryUtil. Like following

private static final Log LOG = LogFactoryUtil.getLog(MyPortlet.class);

Upvotes: 0

Marat
Marat

Reputation: 237

Root logger can be obtained through standard procedure Logger.getLogger("logger name goes here"). Root logger has an empty string as its name. So you should edit your code as follows: Logger.getLogger("").addHandler(listener);

Upvotes: 1

Related Questions