org.apache.openejb.util.Log4jLogStreamFactory 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 org.apache.openejb.util;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.SimpleLayout;
import org.apache.openejb.cdi.logging.Log4jLoggerFactory;
import org.apache.openejb.loader.FileUtils;
import org.apache.openejb.loader.IO;
import org.apache.openejb.loader.SystemInstance;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;
public class Log4jLogStreamFactory implements LogStreamFactory {
private static final String LOGGING_PROPERTIES_FILE = "logging.properties";
private static final String STANDALONE_PROPERTIES_FILE = "log4j.standalone.";
private static final String EMBEDDED_PROPERTIES_FILE = "log4j.embedded.logging.properties";
@Override
public LogStream createLogStream(final LogCategory logCategory) {
return new Log4jLogStream(logCategory);
}
public Log4jLogStreamFactory() {
try {
final boolean externalLogging = SystemInstance.get().getOptions().get("openejb.logger.external", false);
if (!externalLogging) configureInternal();
} catch (Exception e) {
// The fall back here is that if log4j.configuration system property is set, then that configuration file will be used.
e.printStackTrace();
}
System.setProperty("openwebbeans.logging.factory", "org.apache.openejb.cdi.logging.Log4jLoggerFactory");
}
private void configureInternal() throws IOException {
// OpenJPA should use Log4j also
System.setProperty("openjpa.Log", "log4j");
System.setProperty("org.apache.cxf.Logger", "org.apache.cxf.common.logging.Log4jLogger");
final boolean embedded = SystemInstance.get().getOptions().get("openejb.logging.embedded", false);
File confDir = SystemInstance.get().getConf(null);
if (confDir == null) {
confDir = SystemInstance.get().getBase().getDirectory("conf");
}
//Use the old file name first
File loggingPropertiesFile = new File(confDir, LOGGING_PROPERTIES_FILE);
if ((!embedded && confDir.exists()) || (embedded && loggingPropertiesFile.exists())) {
if (!loggingPropertiesFile.exists()) {
//Use the new file name
loggingPropertiesFile = new File(confDir, STANDALONE_PROPERTIES_FILE + LOGGING_PROPERTIES_FILE);
}
if (loggingPropertiesFile.exists()) {
// load logging.properties file
final Properties properties = IO.readProperties(loggingPropertiesFile);
applyOverrides(properties);
preprocessProperties(properties);
PropertyConfigurator.configure(properties);
} else {
// install our logging.properties file into the conf dir
installLoggingPropertiesFile(loggingPropertiesFile);
}
} else {
// Embedded and no logging.properties so configure log4j directly
configureEmbedded();
}
}
private static void applyOverrides(final Properties properties) {
final Properties system = SystemInstance.get().getProperties();
for (final Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy