001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements. See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache license, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License. You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the license for the specific language governing permissions and
015     * limitations under the license.
016     */
017    
018    package org.apache.logging.log4j.jul;
019    
020    import java.util.logging.Level;
021    import java.util.logging.Logger;
022    
023    /**
024     * Log4j Core implementation of the JUL {@link Logger} class. <strong>Note that this implementation does
025     * <em>not</em> use the {@link java.util.logging.Handler} class.</strong> Instead, logging is delegated to the
026     * underlying Log4j {@link org.apache.logging.log4j.core.Logger} which uses
027     * {@link org.apache.logging.log4j.core.Appender Appenders} instead.
028     *
029     * @since 2.1
030     */
031    public class CoreLogger extends ApiLogger {
032    
033        private final org.apache.logging.log4j.core.Logger logger;
034    
035        /**
036         * Constructs a Logger using a Log4j {@link org.apache.logging.log4j.core.Logger}.
037         *
038         * @param logger the underlying Logger to base this Logger on
039         */
040        CoreLogger(final org.apache.logging.log4j.core.Logger logger) {
041            super(logger);
042            this.logger = logger;
043        }
044    
045        @Override
046        public void setLevel(final Level level) throws SecurityException {
047            logger.setLevel(LevelTranslator.toLevel(level));
048            super.doSetLevel(level);
049        }
050    
051        /**
052         * Marks the underlying {@link org.apache.logging.log4j.core.Logger} as additive.
053         *
054         * @param additive {@code true} if this Logger should be additive
055         * @see org.apache.logging.log4j.core.Logger#setAdditive(boolean)
056         */
057        @Override
058        public synchronized void setUseParentHandlers(final boolean additive) {
059            logger.setAdditive(additive);
060        }
061    
062        /**
063         * Indicates if the underlying {@link org.apache.logging.log4j.core.Logger} is additive. <strong>Note that the
064         * Log4j version of JDK Loggers do <em>not</em> use Handlers.</strong>
065         *
066         * @return {@code true} if this Logger is additive, or {@code false} otherwise
067         * @see org.apache.logging.log4j.core.Logger#isAdditive()
068         */
069        @Override
070        public synchronized boolean getUseParentHandlers() {
071            return logger.isAdditive();
072        }
073    
074        @Override
075        public Logger getParent() {
076            final org.apache.logging.log4j.core.Logger parent = logger.getParent();
077            return parent == null ? null : Logger.getLogger(parent.getName());
078        }
079    }