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 018package org.apache.logging.log4j.jpl; 019 020import java.lang.System.Logger; 021import java.util.MissingResourceException; 022import java.util.Objects; 023import java.util.ResourceBundle; 024import java.util.function.Supplier; 025 026import org.apache.logging.log4j.spi.ExtendedLogger; 027 028/** 029 * JPL {@link Logger logger} implementation that uses Log4j. 030 * Implement all default {@link Logger} methods to ensure proper class resolution 031 * 032 * @since 2.14 033 */ 034public class Log4jSystemLogger implements Logger { 035 036 private final ExtendedLogger logger; 037 038 private static final String FQCN = Log4jSystemLogger.class.getName(); 039 040 public Log4jSystemLogger(final ExtendedLogger logger) { 041 this.logger = logger; 042 } 043 044 @Override 045 public String getName() { 046 return logger.getName(); 047 } 048 049 @Override 050 public boolean isLoggable(final Level level) { 051 return logger.isEnabled(getLevel(level)); 052 } 053 054 @Override 055 public void log(Level level, String msg) { 056 log(level, (ResourceBundle) null, msg, (Object[]) null); 057 } 058 059 @Override 060 public void log(Level level, Supplier<String> msgSupplier) { 061 Objects.requireNonNull(msgSupplier); 062 if (isLoggable(Objects.requireNonNull(level))) { 063 log(level, (ResourceBundle) null, msgSupplier.get(), (Object[]) null); 064 } 065 } 066 067 @Override 068 public void log(Level level, Object obj) { 069 Objects.requireNonNull(obj); 070 if (isLoggable(Objects.requireNonNull(level))) { 071 log(level, (ResourceBundle) null, obj.toString(), (Object[]) null); 072 } 073 } 074 075 @Override 076 public void log(Level level, String msg, Throwable thrown) { 077 log(level, null, msg, thrown); 078 } 079 080 @Override 081 public void log(Level level, Supplier<String> msgSupplier, Throwable thrown) { 082 Objects.requireNonNull(msgSupplier); 083 if (isLoggable(Objects.requireNonNull(level))) { 084 log(level, null, msgSupplier.get(), thrown); 085 } 086 } 087 088 @Override 089 public void log(Level level, String format, Object... params) { 090 log(level, null, format, params); 091 } 092 093 @Override 094 public void log(final Level level, final ResourceBundle bundle, final String msg, final Throwable thrown) { 095 logger.logIfEnabled(FQCN, getLevel(level), null, getResource(bundle, msg), thrown); 096 } 097 098 @Override 099 public void log(final Level level, final ResourceBundle bundle, final String format, final Object... params) { 100 logger.logIfEnabled(FQCN, getLevel(level), null, getResource(bundle, format), params); 101 } 102 103 private static org.apache.logging.log4j.Level getLevel(final Level level) { 104 switch (level) { 105 case OFF: 106 return org.apache.logging.log4j.Level.OFF; 107 case ERROR: 108 return org.apache.logging.log4j.Level.ERROR; 109 case WARNING: 110 return org.apache.logging.log4j.Level.WARN; 111 case INFO: 112 return org.apache.logging.log4j.Level.INFO; 113 case DEBUG: 114 return org.apache.logging.log4j.Level.DEBUG; 115 case TRACE: 116 return org.apache.logging.log4j.Level.TRACE; 117 case ALL: 118 return org.apache.logging.log4j.Level.ALL; 119 } 120 return org.apache.logging.log4j.Level.ERROR; 121 } 122 123 private static String getResource(ResourceBundle bundle, String msg) { 124 if (bundle == null || msg == null) { 125 return msg; 126 } 127 try { 128 return bundle.getString(msg); 129 } catch (MissingResourceException e) { 130 // ignore 131 return msg; 132 } catch (ClassCastException ex) { 133 return bundle.getObject(msg).toString(); 134 } 135 } 136}