[Jython-checkins] jython: Add sign-on log message to PyServlet

jeff.allen jython-checkins at python.org
Tue Dec 24 06:10:09 EST 2019


https://hg.python.org/jython/rev/84ad2ab25bca
changeset:   8312:84ad2ab25bca
user:        Jeff Allen <ja.py at farowl.co.uk>
date:        Wed Dec 18 18:02:29 2019 +0000
summary:
  Add sign-on log message to PyServlet

A latent bug (maybe) where PrePy.getEffectiveLoggingLevel causes an NPE,
and revealed in experimentation, is also treated.

files:
  src/org/python/Version.java            |   4 +-
  src/org/python/core/PrePy.java         |  27 +++++++------
  src/org/python/core/PySystemState.java |   8 ++--
  src/org/python/util/PyServlet.java     |  28 ++++++++++---
  src/org/python/util/jython.java        |   4 +-
  5 files changed, 44 insertions(+), 27 deletions(-)


diff --git a/src/org/python/Version.java b/src/org/python/Version.java
--- a/src/org/python/Version.java
+++ b/src/org/python/Version.java
@@ -121,7 +121,7 @@
      * Describe the current Java VM.
      */
     public static String getVM() {
-        return String.format("\n[%s (%s)]", System.getProperty("java.vm.name"),
+        return String.format("[%s (%s)]", System.getProperty("java.vm.name"),
                              System.getProperty("java.vm.vendor"));
     }
 
@@ -130,7 +130,7 @@
      * the Java VM).
      */
     public static String getVersion() {
-        return String.format("%.80s (%.80s) %.80s", PY_VERSION, getBuildInfo(), getVM());
+        return String.format("%.80s (%.80s)\n%.80s", PY_VERSION, getBuildInfo(), getVM());
     }
 
     public static Set<CodeFlag> getDefaultCodeFlags() {
diff --git a/src/org/python/core/PrePy.java b/src/org/python/core/PrePy.java
--- a/src/org/python/core/PrePy.java
+++ b/src/org/python/core/PrePy.java
@@ -106,14 +106,15 @@
 
     /**
      * Convenience function to get the effective level of a given Logger, looking up the parent
-     * chain.
+     * chain. If the root logger is reached without an explicit level set, assume
+     * {@code Level.INFO}.
      */
     private static Level getEffectiveLoggingLevel(Logger logger) {
-        Level level;
-        while ((level = logger.getLevel()) == null) {
+        Level level = null;
+        while (logger != null && (level = logger.getLevel()) == null) {
             logger = logger.getParent();
         }
-        return level;
+        return level != null ? level : Level.INFO;
     }
 
     /** Convenience function to get the effective level of Logger "org.python". */
@@ -125,7 +126,7 @@
      * Used by {@link #maybeWrite(Level, String)}, the terminus of all verbosity-based logging
      * calls, to detect changes made directly to {@link Options#verbose}.
      */
-    private static int savedVerbosity = Py.MESSAGE;
+    private static int savedVerbosity = MESSAGE;
 
     /**
      * Set the level of the Jython logger "org.python" using the standard {@code java.util.logging}
@@ -368,12 +369,12 @@
      * then be opened using {@code jarFile = new JarFile(jarFileName)}. The path to the JAR is
      * returned. If the JAR is accessed by another mechanism ({@code http:} say) this will fail.
      * <p>
-     * The JBoss URL must be a reference to exactly
-     * {@code vfs:<JAR>/org/python/core/PySystemState.class}, or the same thing using the
-     * {@code vfszip:} protocol, where <JAR> stands for the absolute path to the Jython JAR in
-     * VFS. There is no "!/" marker: in JBoss VFS a JAR is treated just like a directory and can no
-     * longer be opened as a JAR. The method essentially just swaps a VFS protocol for the Java
-     * {@code file:} protocol. The path returned will be correct only if this naive swap is valid.
+     * The JBoss URL must be a reference to exactly {@code vfs:<JAR>/org/python/core/PrePy.class},
+     * or the same thing using the {@code vfszip:} protocol, where <JAR> stands for the
+     * absolute path to the Jython JAR in VFS. There is no "!/" marker: in JBoss VFS a JAR is
+     * treated just like a directory and can no longer be opened as a JAR. The method essentially
+     * just swaps a VFS protocol for the Java {@code file:} protocol. The path returned will be
+     * correct only if this naive swap is valid.
      *
      * @param url into the JAR
      * @return the file path or {@code null} in the event of a detectable error
@@ -396,9 +397,9 @@
 
                 case "vfs":
                 case "vfszip":
-                    // path is /some/path/some-jython.jar/org/python/core/PySystemState.class
+                    // path is /some/path/some-jython.jar/org/python/core/PrePy.class
                     String path = url.getPath();
-                    final String target = ".jar/" + Py.class.getName().replace('.', '/');
+                    final String target = ".jar/org/python/core/PrePy.class";
                     int jarIndex = path.indexOf(target);
                     if (jarIndex > 0) {
                         // path contains the target class in a JAR, so make a file URL for it
diff --git a/src/org/python/core/PySystemState.java b/src/org/python/core/PySystemState.java
--- a/src/org/python/core/PySystemState.java
+++ b/src/org/python/core/PySystemState.java
@@ -30,6 +30,7 @@
 import java.util.concurrent.locks.ReentrantLock;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
+import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
@@ -59,7 +60,7 @@
 public class PySystemState extends PyObject
         implements AutoCloseable, ClassDictInit, Closeable, Traverseproc {
 
-    protected static final Logger logger = Logger.getLogger("org.python");
+    private static final Logger logger = Logger.getLogger("org.python.core");
 
     protected static final String CACHEDIR_DEFAULT_NAME = "cachedir";
 
@@ -94,10 +95,8 @@
 
     public final static PyTuple _mercurial = new PyTuple(Py.newString("Jython"),
             Py.newString(Version.getHGIdentifier()), Py.newString(Version.getHGVersion()));
-    /**
-     * The copyright notice for this release.
-     */
 
+    /** The copyright notice for this release. */
     public static final PyObject copyright =
             Py.newString("Copyright (c) 2000-2017 Jython Developers.\n" + "All rights reserved.\n\n"
                     + "Copyright (c) 2000 BeOpen.com.\n" + "All Rights Reserved.\n\n"
@@ -251,6 +250,7 @@
         __dict__.__setitem__("displayhook", __displayhook__);
         __dict__.__setitem__("excepthook", __excepthook__);
 
+        logger.config("sys module instance created");
     }
 
     public static void classDictInit(PyObject dict) {
diff --git a/src/org/python/util/PyServlet.java b/src/org/python/util/PyServlet.java
--- a/src/org/python/util/PyServlet.java
+++ b/src/org/python/util/PyServlet.java
@@ -5,6 +5,8 @@
 import java.util.Enumeration;
 import java.util.Map;
 import java.util.Properties;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -15,12 +17,12 @@
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 
+import org.python.Version;
 import org.python.core.PrePy;
 import org.python.core.Py;
 import org.python.core.PyException;
 import org.python.core.PyObject;
 import org.python.core.PyString;
-import org.python.core.PyStringMap;
 import org.python.core.PySystemState;
 
 /**
@@ -63,26 +65,35 @@
  */
 public class PyServlet extends HttpServlet {
 
+    protected static final Logger logger = Logger.getLogger("org.python.servlet");
+
     public static final String SKIP_INIT_NAME = "skip_jython_initialization";
 
     protected static final String INIT_ATTR = "__jython_initialized__";
 
     @Override
     public void init() {
+        logger.log(Level.INFO, "Jython {0} servlet {1}",
+                new Object[] {Version.PY_VERSION, getServletName()});
+
+        // Config parameters
         Properties props = new Properties();
-        // Config parameters
         Enumeration<?> e = getInitParameterNames();
         while (e.hasMoreElements()) {
             String name = (String)e.nextElement();
             props.put(name, getInitParameter(name));
         }
+
         boolean initialize = getServletConfig().getInitParameter(SKIP_INIT_NAME) != null;
+
         if (getServletContext().getAttribute(INIT_ATTR) != null) {
             if (initialize) {
-                System.err.println("Jython has already been initialized in this context, not "
-                        + "initializing for " + getServletName() + ".  Add " + SKIP_INIT_NAME
-                        + " to as an init param to this servlet's configuration to indicate this "
-                        + "is expected.");
+                logger.log(Level.WARNING, //
+                        "Jython has already been initialized in this context."
+                                + " Not initializing for ''{0}''."
+                                + " Add {1} as an init param to this servlet''s configuration"
+                                + " to indicate this is expected.",
+                        new Object[] {getServletName(), SKIP_INIT_NAME});
             }
         } else if (initialize) {
             init(props, getServletContext());
@@ -96,9 +107,11 @@
      * context, the system state initialization code only runs once.
      */
     protected static void init(Properties props, ServletContext context) {
+
         String rootPath = getRootPath(context);
         context.setAttribute(INIT_ATTR, true);
         Properties baseProps = PrePy.getSystemProperties();
+
         // Context parameters
         Enumeration<?> e = context.getInitParameterNames();
         while (e.hasMoreElements()) {
@@ -109,6 +122,7 @@
                 && baseProps.getProperty("python.home") == null) {
             props.put("python.home", rootPath + "WEB-INF" + File.separator + "lib");
         }
+
         PySystemState.initialize(baseProps, props, new String[0]);
         PySystemState.add_package("javax.servlet");
         PySystemState.add_package("javax.servlet.http");
@@ -237,7 +251,7 @@
         public HttpServlet servlet;
 
         CacheEntry(HttpServlet servlet, long date) {
-            this.servlet=  servlet;
+            this.servlet = servlet;
             this.date = date;
         }
     }
diff --git a/src/org/python/util/jython.java b/src/org/python/util/jython.java
--- a/src/org/python/util/jython.java
+++ b/src/org/python/util/jython.java
@@ -166,7 +166,9 @@
 
     /**
      * Try to set the format for SimpleFormatter if no other mechanism has been provided, and
-     * security allows it.
+     * security allows it. Note that the absolute fall-back format is:
+     * {@code "%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%6$s%n"},
+     * defined ultimately in {@code sun.util.logging.LoggingSupport}.
      *
      * @param format to set for {@code java.util.logging.SimpleFormatter}
      * @throws SecurityException if not allowed to read or set necessary properties.

-- 
Repository URL: https://hg.python.org/jython


More information about the Jython-checkins mailing list