    001package com.hammurapi.common.concurrent;
    002
    003import java.util.ArrayList;
    004import java.util.Collection;
    005import java.util.Iterator;
    006import java.util.Map;
    007import java.util.concurrent.ConcurrentHashMap;
    008import java.util.logging.Level;
    009import java.util.logging.LogRecord;
    010
    011/**
    012 * Dispatches logging to several loggers.
    013 * @author Pavel Vlasov
    014 *
    015 */
    016public class CompositeLogger implements Logger {
    017
    018        private Logger[] loggers;
    019
    020        public CompositeLogger(Logger... loggers) {
    021                this.loggers = loggers;
    022        }
    023
    024        public CompositeLogger(Iterator<Logger> lit) {
    025                Collection<Logger> accumulator = new ArrayList<Logger>();
    026                while (lit.hasNext()) {
    027                        accumulator.add(lit.next());
    028                }
    029                loggers = accumulator.toArray(new Logger[accumulator.size()]);
    030        }
    031
    032        @Override
    033        public void fine(String msg) {
    034                for (Logger logger: loggers) {
    035                        logger.fine(msg);
    036                }
    037        }
    038
    039        @Override
    040        public void finer(String msg) {
    041                for (Logger logger: loggers) {
    042                        logger.finer(msg);
    043                }
    044        }
    045
    046        @Override
    047        public void finest(String msg) {
    048                for (Logger logger: loggers) {
    049                        logger.finest(msg);
    050                }
    051        }
    052
    053        @Override
    054        public void info(String msg) {
    055                for (Logger logger: loggers) {
    056                        logger.info(msg);
    057                }
    058        }
    059
    060        @Override
    061        public void severe(String msg) {
    062                for (Logger logger: loggers) {
    063                        logger.severe(msg);
    064                }
    065        }
    066
    067        @Override
    068        public void warning(String msg) {
    069                for (Logger logger: loggers) {
    070                        logger.warning(msg);
    071                }
    072        }
    073
    074        @Override
    075        public void log(Level level, String msg) {
    076                for (Logger logger: loggers) {
    077                        logger.log(level, msg);
    078                }
    079        }
    080
    081        @Override
    082        public void log(Level level, String msg, Object param1) {
    083                for (Logger logger: loggers) {
    084                        logger.log(level, msg, param1);
    085                }
    086        }
    087
    088        @Override
    089        public void log(Level level, String msg, Object[] params) {
    090                for (Logger logger: loggers) {
    091                        logger.log(level, msg, params);
    092                }
    093        }
    094
    095        @Override
    096        public void log(Level level, String msg, Throwable thrown) {
    097                for (Logger logger: loggers) {
    098                        logger.log(level, msg, thrown);
    099                }
    100        }
    101
    102        @Override
    103        public void log(LogRecord record) {
    104                for (Logger logger: loggers) {
    105                        logger.log(record);
    106                }
    107        }
    108
    109        private Map<String, Logger> subLoggers = new ConcurrentHashMap<String, Logger>();
    110
    111        @Override
    112        public Logger getLogger(String name) {
    113                Logger ret = subLoggers.get(name);
    114                if (ret==null) {
    115                        Logger[] sLoggers = new Logger[loggers.length];
    116                        for (int i=0; i<sLoggers.length; ++i) {
    117                                sLoggers[i]=loggers[i].getLogger(name);
    118                        }
    119                        ret = new CompositeLogger(sLoggers);
    120                        subLoggers.put(name, ret);
    121                }
    122                return  ret;
    123        }
    124
    125        @Override
    126        public boolean isLoggable(Level level) {
    127                for (Logger logger: loggers) {
    128                        if (logger.isLoggable(level)) {
    129                                return true;
    130                        }
    131                }
    132                return false;
    133        }
    134
    135}