001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util.log.log4j.model;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.List;
021
022import javax.xml.bind.annotation.XmlAttribute;
023import javax.xml.bind.annotation.XmlElement;
024import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
025
026import org.kuali.common.util.Assert;
027import org.kuali.common.util.CollectionUtils;
028import org.kuali.common.util.nullify.NullUtils;
029import org.kuali.common.util.xml.jaxb.adapter.OmitNoneStringAdapter;
030import org.kuali.common.util.xml.jaxb.adapter.OmitTrueAdapter;
031
032public class Logger {
033
034        public static final List<Logger> EMPTY = Collections.<Logger> emptyList();
035        public static final boolean DEFAULT_ADDITIVITY = true;
036        public static final Logger DEFAULT = new Logger();
037
038        @XmlAttribute
039        @XmlJavaTypeAdapter(OmitNoneStringAdapter.class)
040        private final String name;
041
042        @XmlElement(name = "appender-ref")
043        private final List<AppenderRef> references;
044
045        @XmlElement
046        private final Level level;
047
048        @XmlAttribute
049        @XmlJavaTypeAdapter(OmitTrueAdapter.class)
050        private final Boolean additivity;
051
052        private Logger() {
053                this(NullUtils.NONE, AppenderRef.EMPTY, Level.DEFAULT);
054        }
055
056        public Logger(String name, Level level) {
057                this(name, AppenderRef.EMPTY, level, DEFAULT_ADDITIVITY);
058        }
059
060        public Logger(String name, List<AppenderRef> references) {
061                this(name, references, Level.DEFAULT, DEFAULT_ADDITIVITY);
062        }
063
064        public Logger(String name, List<AppenderRef> references, Level level) {
065                this(name, references, level, DEFAULT_ADDITIVITY);
066        }
067
068        public Logger(String name, List<AppenderRef> references, Level level, boolean additivity) {
069                Assert.noBlanks(name);
070                Assert.noNulls(references, level);
071                this.name = name;
072                this.references = new ArrayList<AppenderRef>(references);
073                this.level = level;
074                this.additivity = additivity;
075        }
076
077        public List<AppenderRef> getReferences() {
078                return Collections.unmodifiableList(references);
079        }
080
081        public boolean getAdditivity() {
082                return additivity;
083        }
084
085        public String getName() {
086                return name;
087        }
088
089        public Level getLevel() {
090                return level;
091        }
092
093        public static boolean isThresholdNull(Logger logger) {
094                return Threshold.NULL.equals(logger.getLevel().getValue());
095        }
096
097        public static Logger getRootLogger(AppenderRef reference, Level level) {
098                return getRootLogger(CollectionUtils.singletonList(reference), level);
099        }
100
101        public static Logger getRootLogger(List<AppenderRef> references, Level level) {
102                return new Logger(NullUtils.NONE, references, level);
103        }
104}