Permalink
Browse files

Logging work

  • Loading branch information...
1 parent 3defe55 commit dbe4e0786e429b183c8118d601b82d4a0a11afb2 guillaume committed Apr 24, 2009
@@ -2,13 +2,16 @@
import java.io.PrintWriter;
import java.io.StringWriter;
+import java.net.URL;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
+import java.util.Properties;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import org.apache.log4j.Priority;
+import org.apache.log4j.PropertyConfigurator;
import play.exceptions.PlayException;
import play.mvc.Http.Request;
@@ -18,11 +21,27 @@
*/
public class Logger {
- static boolean useJuli = false;
+ public static boolean forceJuli = false;
- public static void setUp(String level) {
- useJuli = Play.configuration.getProperty("logger.juli", "false").equals("true");
- if(useJuli || log4j == null) {
+ /**
+ * The application logger (play).
+ */
+ public static org.apache.log4j.Logger log4j;
+ public static java.util.logging.Logger juli = java.util.logging.Logger.getLogger("play");
+ static {
+ URL log4jConf = Logger.class.getResource("/log4j.properties");
+ if(log4jConf == null) {
+ Properties shutUp = new Properties();
+ shutUp.setProperty("log4j.rootLogger", "OFF");
+ PropertyConfigurator.configure(shutUp);
+ } else {
+ PropertyConfigurator.configure(log4jConf);
+ Logger.log4j = org.apache.log4j.Logger.getLogger("play");
+ }
+ }
+
+ public static void setUp(String level) {
+ if(forceJuli || log4j == null) {
Logger.juli.setLevel(toJuliLevel(level));
} else {
Logger.log4j.setLevel(org.apache.log4j.Level.toLevel(level));
@@ -47,10 +66,10 @@ public static void setUp(String level) {
juliLevel = java.util.logging.Level.WARNING;
}
if(level.equals("DEBUG")) {
- juliLevel = java.util.logging.Level.CONFIG;
+ juliLevel = java.util.logging.Level.FINE;
}
if(level.equals("TRACE")) {
- juliLevel = java.util.logging.Level.FINE;
+ juliLevel = java.util.logging.Level.FINEST;
}
if(level.equals("ALL")) {
juliLevel = java.util.logging.Level.ALL;
@@ -60,27 +79,16 @@ public static void setUp(String level) {
}
return juliLevel;
}
-
- /**
- * The application logger (play).
- */
- public static org.apache.log4j.Logger log4j = org.apache.log4j.Logger.getLogger("play");
- static {
- if(!org.apache.log4j.Logger.getRootLogger().getAllAppenders().hasMoreElements()) {
- Logger.log4j = null;
- }
- }
- public static java.util.logging.Logger juli = java.util.logging.Logger.getLogger("play");
-
+
/**
* Log with TRACE level
* @param message The message pattern
* @param args Pattern arguments
*/
public static void trace(String message, Object... args) {
- if(useJuli || log4j == null) {
+ if(forceJuli || log4j == null) {
try {
- juli.fine(String.format(message, args));
+ juli.finest(String.format(message, args));
} catch (Throwable ex) {
juli.log(Level.SEVERE, "Oops. Error in Logger !", ex);
}
@@ -99,9 +107,9 @@ public static void trace(String message, Object... args) {
* @param args Pattern arguments
*/
public static void debug(String message, Object... args) {
- if(useJuli || log4j == null) {
+ if(forceJuli || log4j == null) {
try {
- juli.config(String.format(message, args));
+ juli.fine(String.format(message, args));
} catch (Throwable ex) {
juli.log(Level.SEVERE, "Oops. Error in Logger !", ex);
}
@@ -121,7 +129,7 @@ public static void debug(String message, Object... args) {
* @param args Pattern arguments
*/
public static void debug(Throwable e, String message, Object... args) {
- if(useJuli || log4j == null) {
+ if(forceJuli || log4j == null) {
try {
if (!niceThrowable(Priority.DEBUG, e, message, args)) {
juli.log(Level.CONFIG, String.format(message, args), e);
@@ -146,7 +154,7 @@ public static void debug(Throwable e, String message, Object... args) {
* @param args Pattern arguments
*/
public static void info(String message, Object... args) {
- if(useJuli || log4j == null) {
+ if(forceJuli || log4j == null) {
try {
juli.info(String.format(message, args));
} catch (Throwable ex) {
@@ -168,7 +176,7 @@ public static void info(String message, Object... args) {
* @param args Pattern arguments
*/
public static void info(Throwable e, String message, Object... args) {
- if(useJuli || log4j == null) {
+ if(forceJuli || log4j == null) {
try {
if (!niceThrowable(Priority.INFO, e, message, args)) {
juli.log(Level.INFO, String.format(message, args), e);
@@ -193,7 +201,7 @@ public static void info(Throwable e, String message, Object... args) {
* @param args Pattern arguments
*/
public static void warn(String message, Object... args) {
- if(useJuli || log4j == null) {
+ if(forceJuli || log4j == null) {
try {
juli.warning(String.format(message, args));
} catch (Throwable ex) {
@@ -215,7 +223,7 @@ public static void warn(String message, Object... args) {
* @param args Pattern arguments
*/
public static void warn(Throwable e, String message, Object... args) {
- if(useJuli || log4j == null) {
+ if(forceJuli || log4j == null) {
try {
if (!niceThrowable(Priority.WARN, e, message, args)) {
juli.log(Level.WARNING, String.format(message, args), e);
@@ -240,7 +248,7 @@ public static void warn(Throwable e, String message, Object... args) {
* @param args Pattern arguments
*/
public static void error(String message, Object... args) {
- if(useJuli || log4j == null) {
+ if(forceJuli || log4j == null) {
try {
juli.severe(String.format(message, args));
} catch (Throwable ex) {
@@ -262,7 +270,7 @@ public static void error(String message, Object... args) {
* @param args Pattern arguments
*/
public static void error(Throwable e, String message, Object... args) {
- if(useJuli || log4j == null) {
+ if(forceJuli || log4j == null) {
try {
if (!niceThrowable(Priority.ERROR, e, message, args)) {
juli.log(Level.SEVERE, String.format(message, args), e);
@@ -287,7 +295,7 @@ public static void error(Throwable e, String message, Object... args) {
* @param args Pattern arguments
*/
public static void fatal(String message, Object... args) {
- if(useJuli || log4j == null) {
+ if(forceJuli || log4j == null) {
try {
juli.severe(String.format(message, args));
} catch (Throwable ex) {
@@ -309,7 +317,7 @@ public static void fatal(String message, Object... args) {
* @param args Pattern arguments
*/
public static void fatal(Throwable e, String message, Object... args) {
- if(useJuli || log4j == null) {
+ if(forceJuli || log4j == null) {
try {
if (!niceThrowable(Priority.FATAL, e, message, args)) {
juli.log(Level.SEVERE, String.format(message, args), e);
@@ -367,7 +375,7 @@ static boolean niceThrowable(Priority priority, Throwable e, String message, Obj
errorOut.println(playException.getErrorTitle());
}
errorOut.println(playException.getErrorDescription().replace("<strong>", "").replace("</strong>", "").replace("\n", " "));
- if(useJuli || log4j == null) {
+ if(forceJuli || log4j == null) {
juli.log(toJuliLevel(priority.toString()), sw.toString(), e);
} else {
log4j.log(priority, sw.toString(), e);
@@ -130,6 +130,8 @@ public static void init(File root, String id) {
Play.id = id;
Play.started = false;
Play.applicationPath = root;
+
+ initStaticStuff();
// Guess the framework path
try {
@@ -284,7 +286,7 @@ public static synchronized void start() {
readConfiguration();
if (configuration.getProperty("play.tmp", "tmp").equals("none")) {
tmpDir = null;
- Logger.warn("No tmp folder will be used (play.tmp is set to none)");
+ Logger.debug("No tmp folder will be used (play.tmp is set to none)");
} else {
tmpDir = new File(configuration.getProperty("play.tmp", "tmp"));
if (!tmpDir.isAbsolute()) {
@@ -418,7 +420,7 @@ public static void loadPlugins() {
urls = Play.classloader.getResources("play.plugins");
} catch (Exception e) {
}
- while (urls.hasMoreElements()) {
+ while (urls != null && urls.hasMoreElements()) {
URL url = urls.nextElement();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
@@ -438,11 +440,36 @@ public static void loadPlugins() {
plugin.onLoad();
}
}
-
- public static void addPlugins(URL playPluginManifest) {
-
+
+ /**
+ * Allow some code to run very eraly in Play! - Use with caution !
+ */
+ public static void initStaticStuff() {
+ // Play! plugings
+ Enumeration<URL> urls = null;
+ try {
+ urls = Play.class.getClassLoader().getResources("play.static");
+ } catch (Exception e) {
+ }
+ while (urls != null && urls.hasMoreElements()) {
+ URL url = urls.nextElement();
+ try {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
+ String line = null;
+ while ((line = reader.readLine()) != null) {
+ try {
+ Class.forName(line);
+ } catch(Exception e) {
+ System.out.println("! Cannot init static : " + line);
+ }
+ }
+ } catch (Exception ex) {
+ Logger.error(ex, "Cannot load %s", url);
+ }
+ }
}
+
public static void loadModules() {
if (System.getenv("MODULES") != null) {
// Modules path is prepended with a env property
@@ -129,12 +129,6 @@ public void afterActionInvocation() {
public void onConfigurationRead() {
}
- /**
- * Called when the server is ready.
- */
- public void onServerReady() {
- }
-
/**
* Called after routes loading.
*/
@@ -21,8 +21,8 @@
* Generate valid JavaBeans.
*/
public class PropertiesEnhancer extends Enhancer {
-
- @Override
+
+ @Override
public void enhanceThisClass(ApplicationClass applicationClass) throws Exception {
final CtClass ctClass = makeClass(applicationClass);
if (ctClass.isInterface()) {
@@ -45,7 +45,7 @@ public void onApplicationStart() {
for (Class clazz : classes) {
if (clazz.isAnnotationPresent(Entity.class)) {
cfg.addAnnotatedClass(clazz);
- Logger.debug("JPA Model : %s", clazz);
+ Logger.trace("JPA Model : %s", clazz);
}
}
Logger.debug("Initializing JPA ...");
@@ -173,7 +173,7 @@ public static void findAllFields(Class clazz, Set<Field> found) {
public static FieldWrapper getFieldWrapper(Field field) {
if (wrappers.get(field) == null) {
FieldWrapper fw = new FieldWrapper(field);
- play.Logger.debug("caching %s", fw);
+ play.Logger.trace("caching %s", fw);
wrappers.put(field, fw);
}
return wrappers.get(field);
@@ -226,13 +226,13 @@ public void setValue(Object instance, Object value) {
}
try {
if (setter != null) {
- play.Logger.debug("invoke setter %s on %s with value %s", setter, instance, value);
+ play.Logger.trace("invoke setter %s on %s with value %s", setter, instance, value);
setter.invoke(instance, value);
} else {
if (!accessible) {
field.setAccessible(true);
}
- play.Logger.debug("field.set(%s, %s)", instance, value);
+ play.Logger.trace("field.set(%s, %s)", instance, value);
field.set(instance, value);
if (!accessible) {
field.setAccessible(accessible);
@@ -145,7 +145,7 @@ public static void route(Http.Request request) {
if (request.querystring != null && methodOverride.matches(request.querystring)) {
Matcher matcher = methodOverride.matcher(request.querystring);
if (matcher.matches()) {
- Logger.debug("request method %s overriden to %s ", request.method, matcher.group("method"));
+ Logger.trace("request method %s overriden to %s ", request.method, matcher.group("method"));
request.method = matcher.group("method");
}
}
@@ -50,11 +50,8 @@ public Server() {
Logger.error("Could not bind on port " + httpPort, e);
acceptor.dispose();
}
-
- // Plugins
- for (PlayPlugin plugin : Play.plugins) {
- plugin.onServerReady();
- }
+
+ Logger.info("");
}
public static void main(String[] args) {
View
@@ -25,6 +25,7 @@
<include name="**/*.properties"/>
<include name="**/*.xml"/>
<include name="**/play.plugins"/>
+ <include name="**/play.static"/>
</fileset>
</copy>
<jar destfile="lib/play-gae.jar" basedir="tmp/classes">
@@ -0,0 +1 @@
+play.modules.gae.GAEInit
@@ -110,7 +110,7 @@ Object unwrap(Object bytes) {
@Override
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
- return Play.classloader.loadClass(desc.getName());
+ return Class.forName(desc.getName(), false, Play.classloader);
}
}.readObject();
} catch (Exception e) {
@@ -0,0 +1,18 @@
+package play.modules.gae;
+
+import com.google.apphosting.api.ApiProxy;
+import org.apache.log4j.helpers.LogLog;
+import play.Logger;
+
+public class GAEInit {
+
+ static {
+ if(ApiProxy.getCurrentEnvironment() != null) {
+ LogLog.setQuietMode(true);
+ Logger.forceJuli = true;
+ Logger.setUp("DEBUG");
+ Logger.info("Play! is running in Google App Engine");
+ }
+ }
+
+}
Oops, something went wrong.

0 comments on commit dbe4e07

Please sign in to comment.