view src/luan/modules/logging/LuanLogger.java @ 1321:307e76ccd0d6

generalize separate logging
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 05 Feb 2019 22:36:55 -0700
parents 1a68fc55a80c
children 11b7e11f9ed5
line wrap: on
line source

package luan.modules.logging;

import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
import org.apache.log4j.Hierarchy;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggerRepository;
import org.apache.log4j.spi.RootLogger;
import luan.Luan;
import luan.LuanState;
import luan.LuanException;
import luan.LuanTable;


public final class LuanLogger {
	private final Logger logger;

	public LuanLogger(Logger logger) {
		this.logger = logger;
	}

	public void error(LuanState luan,Object obj) throws LuanException {
		logger.error( luan.toString(obj) );
	}

	public void warn(LuanState luan,Object obj) throws LuanException {
		logger.warn( luan.toString(obj) );
	}

	public void info(LuanState luan,Object obj) throws LuanException {
		logger.info( luan.toString(obj) );
	}

	public void debug(LuanState luan,Object obj) throws LuanException {
		logger.debug( luan.toString(obj) );
	}


	private static String KEY = "Logger.Repository";

	public static LoggerRepository getLoggerRepository(LuanState luan) {
		LoggerRepository lr = (LoggerRepository)luan.registry().get(KEY);
		return lr != null ? lr : LogManager.getLoggerRepository();
	}

	public static void setLoggerRepository(LuanState luan,LoggerRepository lr) throws LuanException {
		luan.registry().put(KEY,lr);
		LuanTable module = (LuanTable)luan.require("luan:logging/Logging.luan");
		module.call( "init_root" );
	}

	public static void newLoggerRepository(LuanState luan) throws LuanException {
		LoggerRepository lr =  new Hierarchy(new RootLogger(Level.DEBUG));
		setLoggerRepository(luan,lr);
	}

	public static Logger getRootLogger(LuanState luan) {
		return getLoggerRepository(luan).getRootLogger();
	}

	public static Logger getLogger(LuanState luan,String name) {
		return getLoggerRepository(luan).getLogger(name);
	}

}