Slick Forums
http://slick.ninjacave.com/forum/

Log4j LogSystem implementation in slick
http://slick.ninjacave.com/forum/viewtopic.php?f=11&t=3254
Page 1 of 1

Author:  Kova [ Wed Apr 27, 2011 9:49 pm ]
Post subject:  Log4j LogSystem implementation in slick

Hello.

I setup log4j logging (most known Java logging system) in my game so I wanted to share with you the implementation of Slick's LogSystem with it. Slick uses Log class for logging where you can define what the system does with the log messages.

In order to route Slicks internal calls to Log to my log4j loggers, I wrote an implementation for the LogSystem.
Use it by placing in your main this line of code:
Code:
Log.setLogSystem(new Log4jLogSystem());


and the implementation is rather simple:

Code:
package com.threelines.afterdeath.utilities;

import org.apache.log4j.Logger;
import org.newdawn.slick.util.LogSystem;

/**
 * Impelentation to route internal slick log calls to our log4j system.
 * I don't recommend using org.newdawn.slick.util.Log for logging in your game
 * as wou will lose log4j information such as class name, context and such.
 * Use this just LogSystem implentation just for routing Slick's internal
 * Log calls that we can't control.
 *
 * @author Kova
 */
public class Log4jLogSystem implements LogSystem {
   
    private static final Logger _log = Logger.getLogger(Log4jLogSystem.class);

    @Override
    public void error(String message, Throwable e) {
        _log.error(message, e);
    }

    @Override
    public void error(Throwable e) {
        _log.error("", e);
    }

    @Override
    public void error(String message) {
        _log.error(message);
    }

    @Override
    public void warn(String message) {
        _log.warn(message);
    }

    @Override
    public  void info(String message) {
        _log.info(message);
    }

    @Override
    public void debug(String message) {
        _log.debug(message);
    }

    @Override
    public void warn(String message, Throwable e) {
        _log.warn(message, e);
    }
}


Hope someone will find this useful.

Author:  kibertoad [ Sat Apr 07, 2012 6:38 pm ]
Post subject:  Re: Log4j LogSystem implementation in slick

Thanks! Worked like a charm.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/