001package org.apache.commons.jcs3.log;
002
003import org.apache.logging.log4j.Logger;
004import org.apache.logging.log4j.message.MessageFactory;
005import org.apache.logging.log4j.message.MessageFormatMessageFactory;
006
007/*
008 * Licensed to the Apache Software Foundation (ASF) under one or more
009 * contributor license agreements. See the NOTICE file distributed with
010 * this work for additional information regarding copyright ownership.
011 * The ASF licenses this file to You under the Apache license, Version 2.0
012 * (the "License"); you may not use this file except in compliance with
013 * the License. You may obtain a copy of the License at
014 *
015 *      http://www.apache.org/licenses/LICENSE-2.0
016 *
017 * Unless required by applicable law or agreed to in writing, software
018 * distributed under the License is distributed on an "AS IS" BASIS,
019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020 * See the license for the specific language governing permissions and
021 * limitations under the license.
022 */
023
024/**
025 * This is a SPI factory implementation for log4j2
026 */
027public class Log4j2Factory implements LogFactory
028{
029    /** Use java.text.MessageFormat for log messages */
030    private final MessageFactory messageFactory = new MessageFormatMessageFactory();
031
032    /**
033     * Return the name of the Log subsystem managed by this factory
034     *
035     * @return the name of the log subsystem
036     */
037    @Override
038    public String getName()
039    {
040        return "log4j2";
041    }
042
043    /**
044     * Shutdown the logging system if the logging system supports it.
045     */
046    @Override
047    public void shutdown()
048    {
049        org.apache.logging.log4j.LogManager.shutdown();
050    }
051
052    /**
053     * Returns a Log using the fully qualified name of the Class as the Log
054     * name.
055     *
056     * @param clazz
057     *            The Class whose name should be used as the Log name. If null
058     *            it will default to the calling class.
059     * @return The Log.
060     * @throws UnsupportedOperationException
061     *             if {@code clazz} is {@code null} and the calling class cannot
062     *             be determined.
063     */
064    @Override
065    public Log getLog(final Class<?> clazz)
066    {
067        final Logger logger = org.apache.logging.log4j.LogManager.getLogger(clazz, messageFactory);
068        return new Log4j2LogAdapter(logger);
069    }
070
071    /**
072     * Returns a Log with the specified name.
073     *
074     * @param name
075     *            The logger name. If null the name of the calling class will be
076     *            used.
077     * @return The Log.
078     * @throws UnsupportedOperationException
079     *             if {@code name} is {@code null} and the calling class cannot
080     *             be determined.
081     */
082    @Override
083    public Log getLog(final String name)
084    {
085        final Logger logger = org.apache.logging.log4j.LogManager.getLogger(name, messageFactory);
086        return new Log4j2LogAdapter(logger);
087    }
088}