All Downloads are FREE. Search and download functionalities are using the official Maven repository.

panda.log.impl.ComboLogAdapter Maven / Gradle / Ivy

Go to download

Panda Core is the core module of Panda Framework, it contains commonly used utility classes similar to apache-commons.

There is a newer version: 1.8.0
Show newest version
package panda.log.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import panda.lang.Classes;
import panda.lang.Strings;
import panda.log.Log;
import panda.log.LogAdapter;
import panda.log.LogEvent;
import panda.log.LogLog;
import panda.log.Logs;


public class ComboLogAdapter extends AbstractLogAdapter {
	private Map adapters = new LinkedHashMap();

	@Override
	public void init(Logs logs, String name, Map props) {
		super.init(logs, name, props);
		
		if (Strings.isEmpty(name)) {
			setProperties("panda.", props);
		}

		// initialize adapters
		Map as = new LinkedHashMap(adapters.size());
		for (Entry en : adapters.entrySet()) {
			String an = en.getKey();
			LogAdapter adapter = en.getValue();
			try {
				adapter.init(logs, an, props);
				adapter.getLog("panda");
				as.put(an, adapter);
			}
			catch (Throwable e) {
				LogLog.error("Failed to initialize " + adapter.getClass() + ": " + e.getMessage());
			}
		}
		adapters = as;
	}

	@Override
	protected void setProperty(String name, String value) {
		if (name.startsWith("logger.")) {
			name = name.substring("logger.".length());
			addAdapter(name, value);
		}
		else {
			super.setProperty(name, value);
		}
	}
	
	private void addAdapter(String name, String impl) {
		try {
			LogAdapter adapter = (LogAdapter)Classes.newInstance(impl);
			adapters.put(name, adapter);
		}
		catch (Throwable e) {
			LogLog.error("Failed to create " + name + ":" + impl, e);
		}
	}

	@Override
	protected Log getLogger(String name) {
		if (adapters.isEmpty()) {
			return new ConsoleLog(this.logs, name, threshold);
		}
		
		if (adapters.size() == 1) {
			for (LogAdapter a : adapters.values()) {
				try {
					return a.getLog(name);
				}
				catch (Throwable e) {
					LogLog.error("Failed to getLogger(" + a.getClass() + ", " + name + ")");
				}
			}
			return new ConsoleLog(this.logs, name, threshold);
		}
		
		boolean error = false;
		List logs = new ArrayList(adapters.size());
		for (LogAdapter a : adapters.values()) {
			try {
				Log l = a.getLog(name);
				if (!(l instanceof NopLog)) {
					logs.add(l);
				}
			}
			catch (Throwable e) {
				error = true;
				LogLog.error("Failed to getLogger(" + a.getClass() + ", " + name + ")");
			}
		}

		if (logs.isEmpty()) {
			if (error) {
				// return a console log if some error occurs
				return new ConsoleLog(this.logs, name, threshold);
			}
			return NopLog.INSTANCE;
		}

		if (logs.size() == 1) {
			return logs.get(0);
		}

		return new ComboLog(this, name, logs.toArray(new Log[logs.size()]));
	}

	private static class ComboLog extends AbstractLog {
		private Log[] logs;
		
		ComboLog(ComboLogAdapter cla, String name, Log[] logs) {
			super(cla.logs, name, cla.threshold);
			this.logs = logs;
		}

		@Override
		protected void write(LogEvent event) {
			for (Log g : logs) {
				g.log(event);
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy