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.util; 020 021import org.jpos.core.Configurable; 022import org.jpos.core.Configuration; 023import org.jpos.core.ConfigurationException; 024import org.jpos.q2.Q2; 025 026import java.util.ArrayList; 027import java.util.Collections; 028import java.util.ConcurrentModificationException; 029import java.util.Iterator; 030import java.util.List; 031 032/** 033 * Peer class Logger forwards LogEvents generated by LogSources 034 * to LogListeners. 035 * <br> 036 * This little <a href="/doc/LoggerGuide.html">tutorial</a> 037 * give you additional information on how to extend the jPOS's 038 * Logger subsystem. 039 * 040 * @author apr@cs.com.uy 041 * @version $Id$ 042 * @see LogEvent 043 * @see LogSource 044 * @see LogListener 045 * @see Loggeable 046 * @see SimpleLogListener 047 * @see RotateLogListener 048 */ 049@SuppressWarnings("unchecked") 050public class Logger implements LogProducer,Configurable 051{ 052 Configuration cfg; 053 String name; 054 List<LogListener> listeners; 055 public static final String NRPREFIX = "logger."; 056 057 public Logger () { 058 super(); 059 listeners = Collections.synchronizedList(new ArrayList<>()); 060 name = ""; 061 } 062 063 public Configuration getConfiguration() 064 { 065 return cfg; 066 } 067 068 @Override 069 public void setConfiguration(Configuration cfg) throws ConfigurationException 070 { 071 this.cfg = cfg; 072 } 073 074 public void addListener (LogListener l) { 075 listeners.add(l); 076 } 077 public void removeListener (LogListener l) { 078 listeners.remove(l); 079 } 080 public void removeAllListeners () { 081 for (Object l : listeners) { 082 if (l instanceof Destroyable) { 083 ((Destroyable) l).destroy(); 084 } 085 } 086 listeners.clear (); 087 } 088 public static void log (LogEvent evt) { 089 Logger l = null; 090 LogSource source = evt.getSource(); 091 if (source != null) 092 l = source.getLogger(); 093 if (l == null && !evt.isHonorSourceLogger()) { 094 l = getLogger(Q2.LOGGER_NAME); 095 } 096 if (l != null && l.hasListeners ()) { 097 Iterator<LogListener> i = l.listeners.iterator(); 098 while (i.hasNext() && evt != null) { 099 try { 100 evt = i.next().log(evt); 101 } catch (ConcurrentModificationException e) { 102 break; 103 } catch (Throwable t) { 104 evt.addMessage (t); 105 } 106 } 107 } 108 } 109 /** 110 * associates this Logger with a name using NameRegistrar 111 * @param name name to register 112 * @see NameRegistrar 113 */ 114 public void setName (String name) { 115 this.name = name; 116 NameRegistrar.register (NRPREFIX+name, this); 117 } 118 /** 119 * destroy logger 120 */ 121 public void destroy () { 122 NameRegistrar.unregister (NRPREFIX+name); 123 removeAllListeners (); 124 } 125 /** 126 * @return logger instance with given name. Creates one if necessary 127 * @see NameRegistrar 128 */ 129 public synchronized static Logger getLogger (String name) { 130 Logger l; 131 try { 132 l = NameRegistrar.get (NRPREFIX+name); 133 } catch (NameRegistrar.NotFoundException e) { 134 l = new Logger(); 135 l.setName (name); 136 } 137 return l; 138 } 139 /** 140 * @return this logger's name ("" if no name was set) 141 */ 142 public String getName() { 143 return this.name; 144 } 145 /** 146 * Used by heavy used methods to avoid LogEvent creation 147 * @return true if Logger has associated LogListsners 148 */ 149 public boolean hasListeners() { 150 return !listeners.isEmpty(); 151 } 152}