001/* 002 * jPOS Project [http://jpos.org] 003 * Copyright (C) 2000-2026 jPOS Software SRL 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018 019package org.jpos.q2.qbean; 020 021import org.jdom2.Element; 022import org.jpos.core.ConfigurationException; 023import org.jpos.q2.QBeanSupport; 024import org.jpos.q2.QFactory; 025import org.jpos.util.*; 026 027import java.io.IOException; 028import java.io.PrintStream; 029 030public class LoggerAdaptor extends QBeanSupport { 031 private Logger logger; 032 private PrintStream originalOut = null; 033 private PrintStream originalErr = null; 034 035 protected void initService () { 036 logger = Logger.getLogger (getName()); 037 } 038 protected void startService () throws ConfigurationException, IOException { 039 logger.removeAllListeners (); 040 for (Element lle : getPersist().getChildren("log-listener")) 041 addListener(lle); 042 043 String redirect = cfg.get("redirect"); 044 long delay = cfg.getLong("delay", 500); 045 046 if (redirect.contains("stdout")) { 047 originalOut = System.out; 048 System.setOut(new PrintStream(new LogEventOutputStream(logger, "stdout", delay))); 049 } 050 if (redirect.contains("stderr")) { 051 originalErr = System.err; 052 System.setErr(new PrintStream(new LogEventOutputStream(logger, "stderr", delay))); 053 } 054 } 055 protected void stopService() { 056 if (originalOut != null) 057 System.setOut(originalOut); 058 if (originalErr != null) 059 System.setErr(originalErr); 060 logger.removeAllListeners (); 061 } 062 protected void destroyService() { 063 // we don't destroy (that would unregister the logger from the 064 // NameRegistrar) because other components might have references 065 // to this logger. 066 // 067 // logger.destroy (); 068 } 069 070 private void addListener (Element e) throws ConfigurationException { 071 QFactory factory = getServer().getFactory(); 072 LogListener listener = factory.newInstance(e); 073 if (listener != null) { 074 attemptToAddWriter(e.getChild("writer"), listener); 075 logger.addListener(listener); 076 } 077 } 078 079 private void attemptToAddWriter (Element e, LogListener listener) throws ConfigurationException { 080 if (e != null) { 081 QFactory factory = getServer().getFactory(); 082 if (QFactory.isEnabled(e)) { 083 String clazz = e.getAttributeValue("class"); 084 LogEventWriter writer = factory.newInstance(clazz); 085 factory.setConfiguration(writer, e); 086 listener.setLogEventWriter (writer); 087 } 088 } 089 } 090}