view src/luan/modules/logging/LuanLogger.java @ 1333:25746915a241

merge Luan and LuanState
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 12 Feb 2019 22:33:40 -0700
parents 11b7e11f9ed5
children c88b486a9511
line wrap: on
line source

package luan.modules.logging;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import luan.Luan;
import luan.LuanException;


public final class LuanLogger {
	private final Logger logger;

	public LuanLogger(Luan luan,String name) {
		this.logger = getLogger(luan,name);
	}

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

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

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

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


	private static ThreadLocal<Luan> tl = new ThreadLocal<Luan>();

	public static Logger getLogger(Luan luan,String name) {
		try {
			luan.require("luan:logging/Logging.luan");  // ensure initialization
		} catch(LuanException e) {
			throw new RuntimeException(e);
		}
		tl.set(luan);
		try {
			return LoggerFactory.getLogger(name);
		} finally {
			tl.remove();
		}
	}

	public static Luan luan() {
		return tl.get();
	}
}