View Javadoc
1   package org.apache.commons.jcs3.log;
2   
3   import org.apache.logging.log4j.Logger;
4   import org.apache.logging.log4j.message.MessageFactory;
5   import org.apache.logging.log4j.message.MessageFormatMessageFactory;
6   
7   /*
8    * Licensed to the Apache Software Foundation (ASF) under one or more
9    * contributor license agreements. See the NOTICE file distributed with
10   * this work for additional information regarding copyright ownership.
11   * The ASF licenses this file to You under the Apache license, Version 2.0
12   * (the "License"); you may not use this file except in compliance with
13   * the License. You may obtain a copy of the License at
14   *
15   *      http://www.apache.org/licenses/LICENSE-2.0
16   *
17   * Unless required by applicable law or agreed to in writing, software
18   * distributed under the License is distributed on an "AS IS" BASIS,
19   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   * See the license for the specific language governing permissions and
21   * limitations under the license.
22   */
23  
24  /**
25   * This is a SPI factory implementation for log4j2
26   */
27  public class Log4j2Factory implements LogFactory
28  {
29      /** Use java.text.MessageFormat for log messages */
30      private final MessageFactory messageFactory = new MessageFormatMessageFactory();
31  
32      /**
33       * Return the name of the Log subsystem managed by this factory
34       *
35       * @return the name of the log subsystem
36       */
37      @Override
38      public String getName()
39      {
40          return "log4j2";
41      }
42  
43      /**
44       * Shutdown the logging system if the logging system supports it.
45       */
46      @Override
47      public void shutdown()
48      {
49          org.apache.logging.log4j.LogManager.shutdown();
50      }
51  
52      /**
53       * Returns a Log using the fully qualified name of the Class as the Log
54       * name.
55       *
56       * @param clazz
57       *            The Class whose name should be used as the Log name. If null
58       *            it will default to the calling class.
59       * @return The Log.
60       * @throws UnsupportedOperationException
61       *             if {@code clazz} is {@code null} and the calling class cannot
62       *             be determined.
63       */
64      @Override
65      public Log getLog(final Class<?> clazz)
66      {
67          final Logger logger = org.apache.logging.log4j.LogManager.getLogger(clazz, messageFactory);
68          return new Log4j2LogAdapter(logger);
69      }
70  
71      /**
72       * Returns a Log with the specified name.
73       *
74       * @param name
75       *            The logger name. If null the name of the calling class will be
76       *            used.
77       * @return The Log.
78       * @throws UnsupportedOperationException
79       *             if {@code name} is {@code null} and the calling class cannot
80       *             be determined.
81       */
82      @Override
83      public Log getLog(final String name)
84      {
85          final Logger logger = org.apache.logging.log4j.LogManager.getLogger(name, messageFactory);
86          return new Log4j2LogAdapter(logger);
87      }
88  }