You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by db...@apache.org on 2012/01/23 02:11:49 UTC

svn commit: r1234663 - in /openejb/trunk/openejb/container/openejb-core/src/main: java/org/apache/openejb/util/Log4jLogStreamFactory.java resources/log4j.embedded.logging.properties resources/log4j.standalone.logging.properties

Author: dblevins
Date: Mon Jan 23 01:11:48 2012
New Revision: 1234663

URL: http://svn.apache.org/viewvc?rev=1234663&view=rev
Log:
Restoring functionality lost with OPENEJB-1740

Added:
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/Log4jLogStreamFactory.java
    openejb/trunk/openejb/container/openejb-core/src/main/resources/log4j.embedded.logging.properties
    openejb/trunk/openejb/container/openejb-core/src/main/resources/log4j.standalone.logging.properties

Added: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/Log4jLogStreamFactory.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/Log4jLogStreamFactory.java?rev=1234663&view=auto
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/Log4jLogStreamFactory.java (added)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/Log4jLogStreamFactory.java Mon Jan 23 01:11:48 2012
@@ -0,0 +1,225 @@
+/*
+ * 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.loader.FileUtils;
+import org.apache.openejb.loader.SystemInstance;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+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 = "log4j.standalone.logging.properties";
+    private static final String EMBEDDED_PROPERTIES_FILE = "log4j.embedded.logging.properties";
+
+    public LogStream createLogStream(LogCategory logCategory) {
+        return new Log4jLogStream(logCategory);
+    }
+
+    public Log4jLogStreamFactory() {
+        try {
+            String prop = System.getProperty("openejb.logger.external", "false");
+            boolean externalLogging = Boolean.parseBoolean(prop);
+
+            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();
+        }
+    }
+
+    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 = System.getProperty("openejb.logging.embedded", "false").equalsIgnoreCase("true");
+
+        File confDir = SystemInstance.get().getBase().getDirectory("conf");
+        File loggingPropertiesFile = new File(confDir, LOGGING_PROPERTIES_FILE);
+        if (!embedded && confDir.exists()) {
+            if (loggingPropertiesFile.exists()) {
+                // load logging.properties file
+                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(loggingPropertiesFile));
+                Properties props = new Properties();
+                props.load(bis);
+                applyOverrides(props);
+                preprocessProperties(props);
+                PropertyConfigurator.configure(props);
+                try {
+                    bis.close();
+                } catch (IOException e) {
+
+                }
+            } else {
+                // install our logging.properties file into the conf dir
+                installLoggingPropertiesFile(loggingPropertiesFile);
+            }
+        } else {
+            // no conf directory, so we assume we are embedded
+            // configure log4j directly
+            configureEmbedded();
+        }
+    }
+
+    private static void applyOverrides(Properties properties) {
+        Properties system = SystemInstance.get().getProperties();
+        for (Map.Entry<Object, Object> entry : system.entrySet()) {
+            String key = entry.getKey().toString();
+            if (key.startsWith("log4j.") && !key.equals("log4j.configuration")) {
+                properties.put(key, entry.getValue());
+            }
+        }
+    }
+
+    private void preprocessProperties(Properties properties) {
+        FileUtils base = SystemInstance.get().getBase();
+        File confDir = new File(base.getDirectory(), "conf");
+        File baseDir = base.getDirectory();
+        File userDir = new File("foo").getParentFile();
+
+        File[] paths = {confDir, baseDir, userDir};
+
+        List<File> missing = new ArrayList<File>();
+
+        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
+            String key = (String) entry.getKey();
+            String value = (String) entry.getValue();
+
+            if (key.endsWith(".File")) {
+                boolean found = false;
+                for (int i = 0; i < paths.length && !found; i++) {
+                    File path = paths[i];
+                    File logfile = new File(path, value);
+                    if (logfile.getParentFile().exists()) {
+                        properties.setProperty(key, logfile.getAbsolutePath());
+                        found = true;
+                    }
+                }
+
+                if (!found) {
+                    File logfile = new File(paths[0], value);
+                    missing.add(logfile);
+                }
+            }
+        }
+
+        if (missing.size() > 0) {
+            org.apache.log4j.Logger logger = getFallabckLogger();
+
+            logger.error("Logging may not operate as expected.  The directories for the following files do not exist so no file can be created.  See the list below.");
+            for (int i = 0; i < missing.size(); i++) {
+                File file = missing.get(i);
+                logger.error("[" + i + "] " + file.getAbsolutePath());
+            }
+        }
+    }
+
+    private org.apache.log4j.Logger getFallabckLogger() {
+        org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger("OpenEJB.logging");
+
+        SimpleLayout simpleLayout = new SimpleLayout();
+        ConsoleAppender newAppender = new ConsoleAppender(simpleLayout);
+        logger.addAppender(newAppender);
+        return logger;
+    }
+
+    private void configureEmbedded() {
+        URL resource = ConfUtils.getResource(EMBEDDED_PROPERTIES_FILE);
+        if (resource == null) {
+            System.err.println("FATAL ERROR WHILE CONFIGURING LOGGING!!!. MISSING embedded.logging.properties FILE ");
+
+        } else {
+            Properties properties = asProperies(resource);
+            applyOverrides(properties);
+            PropertyConfigurator.configure(properties);
+
+            // TODO Has to be a better way to set the log level
+            final Logger logger = Logger.getLogger("org.apache");
+            final Logger parent = logger.getParent();
+            parent.setLevel(java.util.logging.Level.WARNING);
+
+        }
+    }
+
+    private static Properties asProperies(URL resource) {
+        Properties properties = new Properties();
+        InputStream in = null;
+        try {
+            in = resource.openStream();
+            in = new BufferedInputStream(in);
+            properties.load(in);
+        } catch (IOException e) {
+        } finally {
+            try {
+                if (in != null) in.close();
+            } catch (IOException e) {
+            }
+        }
+        return properties;
+    }
+
+    private void installLoggingPropertiesFile(File loggingPropertiesFile) throws IOException {
+        URL resource = Thread.currentThread().getContextClassLoader().getResource(LOGGING_PROPERTIES_FILE);
+        if (resource == null) {
+            System.err.println("FATAL ERROR WHILE CONFIGURING LOGGING!!!. MISSING logging.properties FILE ");
+            return;
+        }
+        InputStream in = resource.openStream();
+        in = new BufferedInputStream(in);
+        ByteArrayOutputStream bao = new ByteArrayOutputStream();
+        byte buf[] = new byte[4096];
+        for (int count = in.read(buf); count >= 0; count = in.read(buf)) {
+            bao.write(buf, 0, count);
+        }
+        byte[] byteArray = bao.toByteArray();
+        ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
+
+        Properties props = new Properties();
+        props.load(bis);
+        preprocessProperties(props);
+        BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(loggingPropertiesFile));
+        bout.write(byteArray);
+        PropertyConfigurator.configure(props);
+        try {
+            bout.close();
+        } catch (IOException e) {
+
+        }
+        try {
+            in.close();
+        } catch (IOException e) {
+
+        }
+    }
+}

Added: openejb/trunk/openejb/container/openejb-core/src/main/resources/log4j.embedded.logging.properties
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/resources/log4j.embedded.logging.properties?rev=1234663&view=auto
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/resources/log4j.embedded.logging.properties (added)
+++ openejb/trunk/openejb/container/openejb-core/src/main/resources/log4j.embedded.logging.properties Mon Jan 23 01:11:48 2012
@@ -0,0 +1,33 @@
+#
+# 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.
+#
+
+log4j.rootLogger                   = fatal,C
+log4j.category.OpenEJB             = warn
+log4j.category.OpenEJB.options     = info
+log4j.category.OpenEJB.server      = info
+log4j.category.OpenEJB.startup     = info
+log4j.category.OpenEJB.startup.service = warn
+log4j.category.OpenEJB.startup.config = info
+log4j.category.OpenEJB.hsql        = info
+log4j.category.CORBA-Adapter       = warn
+log4j.category.Transaction         = warn
+log4j.category.org.apache.activemq = error
+log4j.category.org.apache.geronimo = error
+log4j.category.openjpa             = warn
+
+log4j.appender.C                           = org.apache.log4j.ConsoleAppender
+log4j.appender.C.layout                    = org.apache.log4j.SimpleLayout

Added: openejb/trunk/openejb/container/openejb-core/src/main/resources/log4j.standalone.logging.properties
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/resources/log4j.standalone.logging.properties?rev=1234663&view=auto
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/resources/log4j.standalone.logging.properties (added)
+++ openejb/trunk/openejb/container/openejb-core/src/main/resources/log4j.standalone.logging.properties Mon Jan 23 01:11:48 2012
@@ -0,0 +1,87 @@
+#
+# 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.
+#
+
+# ---------------------------------------------------
+# The configuration below is good for standalone
+# scenarios.  There's a log file and everything is
+# time stamped and settings are slightly more verbose.
+#
+# For embedded scenarios the commented out configuration
+# below is a much better fit.
+#
+log4j.rootLogger = fatal,R
+
+log4j.category.OpenEJB             = warn,R
+log4j.category.OpenEJB.options     = info
+log4j.category.OpenEJB.server      = info
+log4j.category.OpenEJB.startup     = info
+log4j.category.OpenEJB.startup.service = info
+log4j.category.OpenEJB.startup.config = info
+log4j.category.OpenEJB.hsql        = info
+log4j.category.OpenEJB.ws          = info
+log4j.category.OpenEJB.rs          = info
+log4j.category.OpenEJB.tomcat      = info
+log4j.category.CORBA-Adapter       = error,R
+log4j.category.Transaction         = warn,TX
+log4j.category.org.apache.activemq = error,R
+log4j.category.org.apache.geronimo = error,R
+log4j.category.openjpa             = error,R
+log4j.category.axis                = info,R
+log4j.category.axis2               = info,R
+log4j.category.cxf                 = info,R
+log4j.category.org.apache.cxf      = info,R
+
+log4j.appender.R=org.apache.log4j.RollingFileAppender
+log4j.appender.R.layout=org.apache.log4j.PatternLayout
+log4j.appender.R.MaxFileSize=1000KB
+log4j.appender.R.MaxBackupIndex=7
+log4j.appender.R.File=logs/openejb.log
+log4j.appender.R.layout.ConversionPattern=%d - %-5p - %m%n
+
+log4j.appender.TX=org.apache.log4j.RollingFileAppender
+log4j.appender.TX.layout=org.apache.log4j.PatternLayout
+log4j.appender.TX.MaxFileSize=1000KB
+log4j.appender.TX.MaxBackupIndex=100
+log4j.appender.TX.File=logs/transaction.log
+log4j.appender.TX.layout.ConversionPattern=%d - %-5p - %m%n
+
+
+## ---------------------------------------------------
+## Nice alternate configuration for embedded testing
+##
+## Output is slightly more terse and sent to System.out
+##
+## Simply comment out the above declarations and
+## uncomment the configuration below.
+##
+#
+#log4j.rootLogger                   = fatal,C
+#log4j.category.OpenEJB             = warn
+#log4j.category.OpenEJB.options     = info
+#log4j.category.OpenEJB.server      = info
+#log4j.category.OpenEJB.startup     = info
+#log4j.category.OpenEJB.startup.service = warn
+#log4j.category.OpenEJB.startup.config = info
+#log4j.category.OpenEJB.hsql        = info
+#log4j.category.CORBA-Adapter       = info
+#log4j.category.Transaction         = warn
+#log4j.category.org.apache.activemq = error
+#log4j.category.org.apache.geronimo = error
+#log4j.category.openjpa             = error
+#
+#log4j.appender.C                           = org.apache.log4j.ConsoleAppender
+#log4j.appender.C.layout                    = org.apache.log4j.SimpleLayout