com.alipay.sofa.common.log.factory.Log4j2LoggerSpaceFactory Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.common.log.factory;
import com.alipay.sofa.common.log.Constants;
import com.alipay.sofa.common.log.SpaceId;
import com.alipay.sofa.common.log.adapter.level.AdapterLevel;
import com.alipay.sofa.common.log.spi.ReInitializeChecker;
import com.alipay.sofa.common.log.spi.Log4j2FilterGenerator;
import com.alipay.sofa.common.log.spi.Log4j2ReInitializer;
import com.alipay.sofa.common.utils.StringUtil;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.slf4j.Log4jLogger;
import org.slf4j.Logger;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import static com.alipay.sofa.common.log.Constants.IS_DEFAULT_LOG_PATH;
import static com.alipay.sofa.common.log.Constants.LOG_LEVEL_PREFIX;
import static com.alipay.sofa.common.log.Constants.LOG_PATH;
/**
* @author qilong.zql
* @since 1.0.15
*/
public class Log4j2LoggerSpaceFactory extends AbstractLoggerSpaceFactory {
private ConcurrentMap loggerMap = new ConcurrentHashMap();
private SpaceId spaceId;
private Properties properties;
private LoggerContext loggerContext;
private URL confFile;
/**
* @param source logback,log4j2,log4j,temp,nop
*/
public Log4j2LoggerSpaceFactory(SpaceId spaceId, Properties properties, URL confFile,
String source) throws Throwable {
super(source);
this.spaceId = spaceId;
this.properties = properties;
this.confFile = confFile;
boolean willReinitialize = false;
Iterator checkers = ServiceLoader.load(ReInitializeChecker.class,
this.getClass().getClassLoader()).iterator();
while (checkers.hasNext()) {
willReinitialize = !checkers.next().isReInitialize();
}
this.loggerContext = initialize(willReinitialize);
Iterator matchers = ServiceLoader.load(Log4j2FilterGenerator.class,
this.getClass().getClassLoader()).iterator();
while (matchers.hasNext() & willReinitialize) {
Log4j2FilterGenerator matcher = matchers.next();
for (Filter filter : matcher.generatorFilters()) {
this.loggerContext.addFilter(filter);
}
}
}
private LoggerContext initialize(boolean willReInitialize) throws Throwable {
for (Map.Entry entry : properties.entrySet()) {
ThreadContext.put((String) entry.getKey(),
properties.getProperty((String) entry.getKey()));
}
LoggerContext context;
if (willReInitialize) {
context = new LoggerContext(spaceId.getSpaceName(),
Constants.SOFA_LOG_FIRST_INITIALIZE, confFile.toURI());
} else {
context = new LoggerContext(spaceId.getSpaceName(), null, confFile.toURI());
}
Configuration config = null;
ConfigurationFactory configurationFactory = ConfigurationFactory.getInstance();
try {
//log4j-core 2.3 version
Class[] parameterTypes = new Class[3];
parameterTypes[0] = String.class;
parameterTypes[1] = URI.class;
parameterTypes[2] = ClassLoader.class;
Method getConfigurationMethod = configurationFactory.getClass().getMethod(
"getConfiguration", parameterTypes);
config = (Configuration) getConfigurationMethod.invoke(configurationFactory,
spaceId.getSpaceName(), confFile.toURI(), this.getClass().getClassLoader());
} catch (NoSuchMethodException noSuchMethodException) {
//log4j-core 2.7+ version
Class[] parameterTypes = new Class[4];
parameterTypes[0] = LoggerContext.class;
parameterTypes[1] = String.class;
parameterTypes[2] = URI.class;
parameterTypes[3] = ClassLoader.class;
Method getConfigurationMethod = configurationFactory.getClass().getMethod(
"getConfiguration", parameterTypes);
config = (Configuration) getConfigurationMethod.invoke(configurationFactory, context,
spaceId.getSpaceName(), confFile.toURI(), this.getClass().getClassLoader());
}
if (config == null) {
throw new RuntimeException("No log4j2 configuration are found.");
}
for (Map.Entry entry : properties.entrySet()) {
config.getProperties().put((String) entry.getKey(), (String) entry.getValue());
}
for (Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy