view src/luan/modules/logging/LuanLogger.java @ 1393:cc0dbca576dc

better logging
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 06 Sep 2019 05:09:56 -0600
parents 002152af497a
children 27efb1fcbcb5
line wrap: on
line source

package luan.modules.logging;

import luan.lib.logging.Logger;
import luan.lib.logging.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(Object obj) throws LuanException {
		logger.error( Luan.luanToString(obj) );
	}

	public void warn(Object obj) throws LuanException {
		logger.warn( Luan.luanToString(obj) );
	}

	public void info(Object obj) throws LuanException {
		logger.info( Luan.luanToString(obj) );
	}

	public void debug(Object obj) throws LuanException {
		logger.debug( Luan.luanToString(obj) );
	}


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

	private static void init(Luan luan) {
		try {
			luan.require("luan:logging/Logging.luan");  // ensure initialization
		} catch(LuanException e) {
			throw new RuntimeException(e);
		}
	}

	public static Logger getLogger(Luan luan,String name) {
		init(luan);
		tl.set(luan);
		try {
			return LoggerFactory.getLogger(name);
		} finally {
			tl.remove();
		}
	}

	public static Logger getLogger(Luan luan,Class cls) {
		init(luan);
		tl.set(luan);
		try {
			return LoggerFactory.getLogger(cls);
		} finally {
			tl.remove();
		}
	}

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