view src/luan/modules/logging/LuanLogger.java @ 1332:11b7e11f9ed5

cleaner logging
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 12 Feb 2019 21:50:26 -0700
parents 307e76ccd0d6
children 25746915a241
line wrap: on
line source

package luan.modules.logging;

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


public final class LuanLogger {
	private final Logger logger;

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

	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 ThreadLocal<LuanState> tl = new ThreadLocal<LuanState>();

	public static Logger getLogger(LuanState 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 LuanState luan() {
		return tl.get();
	}
}