Skip to content

Commit

Permalink
Logging work
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume committed Apr 24, 2009
1 parent 3defe55 commit dbe4e07
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 63 deletions.
72 changes: 40 additions & 32 deletions framework/src/play/Logger.java
Expand Up @@ -2,13 +2,16 @@


import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.net.URL;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties;
import java.util.logging.Handler; import java.util.logging.Handler;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
import org.apache.log4j.Priority; import org.apache.log4j.Priority;
import org.apache.log4j.PropertyConfigurator;
import play.exceptions.PlayException; import play.exceptions.PlayException;
import play.mvc.Http.Request; import play.mvc.Http.Request;


Expand All @@ -18,11 +21,27 @@
*/ */
public class Logger { 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"); * The application logger (play).
if(useJuli || log4j == null) { */
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)); Logger.juli.setLevel(toJuliLevel(level));
} else { } else {
Logger.log4j.setLevel(org.apache.log4j.Level.toLevel(level)); Logger.log4j.setLevel(org.apache.log4j.Level.toLevel(level));
Expand All @@ -47,10 +66,10 @@ static java.util.logging.Level toJuliLevel(String level) {
juliLevel = java.util.logging.Level.WARNING; juliLevel = java.util.logging.Level.WARNING;
} }
if(level.equals("DEBUG")) { if(level.equals("DEBUG")) {
juliLevel = java.util.logging.Level.CONFIG; juliLevel = java.util.logging.Level.FINE;
} }
if(level.equals("TRACE")) { if(level.equals("TRACE")) {
juliLevel = java.util.logging.Level.FINE; juliLevel = java.util.logging.Level.FINEST;
} }
if(level.equals("ALL")) { if(level.equals("ALL")) {
juliLevel = java.util.logging.Level.ALL; juliLevel = java.util.logging.Level.ALL;
Expand All @@ -60,27 +79,16 @@ static java.util.logging.Level toJuliLevel(String level) {
} }
return juliLevel; 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 * Log with TRACE level
* @param message The message pattern * @param message The message pattern
* @param args Pattern arguments * @param args Pattern arguments
*/ */
public static void trace(String message, Object... args) { public static void trace(String message, Object... args) {
if(useJuli || log4j == null) { if(forceJuli || log4j == null) {
try { try {
juli.fine(String.format(message, args)); juli.finest(String.format(message, args));
} catch (Throwable ex) { } catch (Throwable ex) {
juli.log(Level.SEVERE, "Oops. Error in Logger !", ex); juli.log(Level.SEVERE, "Oops. Error in Logger !", ex);
} }
Expand All @@ -99,9 +107,9 @@ public static void trace(String message, Object... args) {
* @param args Pattern arguments * @param args Pattern arguments
*/ */
public static void debug(String message, Object... args) { public static void debug(String message, Object... args) {
if(useJuli || log4j == null) { if(forceJuli || log4j == null) {
try { try {
juli.config(String.format(message, args)); juli.fine(String.format(message, args));
} catch (Throwable ex) { } catch (Throwable ex) {
juli.log(Level.SEVERE, "Oops. Error in Logger !", ex); juli.log(Level.SEVERE, "Oops. Error in Logger !", ex);
} }
Expand All @@ -121,7 +129,7 @@ public static void debug(String message, Object... args) {
* @param args Pattern arguments * @param args Pattern arguments
*/ */
public static void debug(Throwable e, String message, Object... args) { public static void debug(Throwable e, String message, Object... args) {
if(useJuli || log4j == null) { if(forceJuli || log4j == null) {
try { try {
if (!niceThrowable(Priority.DEBUG, e, message, args)) { if (!niceThrowable(Priority.DEBUG, e, message, args)) {
juli.log(Level.CONFIG, String.format(message, args), e); juli.log(Level.CONFIG, String.format(message, args), e);
Expand All @@ -146,7 +154,7 @@ public static void debug(Throwable e, String message, Object... args) {
* @param args Pattern arguments * @param args Pattern arguments
*/ */
public static void info(String message, Object... args) { public static void info(String message, Object... args) {
if(useJuli || log4j == null) { if(forceJuli || log4j == null) {
try { try {
juli.info(String.format(message, args)); juli.info(String.format(message, args));
} catch (Throwable ex) { } catch (Throwable ex) {
Expand All @@ -168,7 +176,7 @@ public static void info(String message, Object... args) {
* @param args Pattern arguments * @param args Pattern arguments
*/ */
public static void info(Throwable e, String message, Object... args) { public static void info(Throwable e, String message, Object... args) {
if(useJuli || log4j == null) { if(forceJuli || log4j == null) {
try { try {
if (!niceThrowable(Priority.INFO, e, message, args)) { if (!niceThrowable(Priority.INFO, e, message, args)) {
juli.log(Level.INFO, String.format(message, args), e); juli.log(Level.INFO, String.format(message, args), e);
Expand All @@ -193,7 +201,7 @@ public static void info(Throwable e, String message, Object... args) {
* @param args Pattern arguments * @param args Pattern arguments
*/ */
public static void warn(String message, Object... args) { public static void warn(String message, Object... args) {
if(useJuli || log4j == null) { if(forceJuli || log4j == null) {
try { try {
juli.warning(String.format(message, args)); juli.warning(String.format(message, args));
} catch (Throwable ex) { } catch (Throwable ex) {
Expand All @@ -215,7 +223,7 @@ public static void warn(String message, Object... args) {
* @param args Pattern arguments * @param args Pattern arguments
*/ */
public static void warn(Throwable e, String message, Object... args) { public static void warn(Throwable e, String message, Object... args) {
if(useJuli || log4j == null) { if(forceJuli || log4j == null) {
try { try {
if (!niceThrowable(Priority.WARN, e, message, args)) { if (!niceThrowable(Priority.WARN, e, message, args)) {
juli.log(Level.WARNING, String.format(message, args), e); juli.log(Level.WARNING, String.format(message, args), e);
Expand All @@ -240,7 +248,7 @@ public static void warn(Throwable e, String message, Object... args) {
* @param args Pattern arguments * @param args Pattern arguments
*/ */
public static void error(String message, Object... args) { public static void error(String message, Object... args) {
if(useJuli || log4j == null) { if(forceJuli || log4j == null) {
try { try {
juli.severe(String.format(message, args)); juli.severe(String.format(message, args));
} catch (Throwable ex) { } catch (Throwable ex) {
Expand All @@ -262,7 +270,7 @@ public static void error(String message, Object... args) {
* @param args Pattern arguments * @param args Pattern arguments
*/ */
public static void error(Throwable e, String message, Object... args) { public static void error(Throwable e, String message, Object... args) {
if(useJuli || log4j == null) { if(forceJuli || log4j == null) {
try { try {
if (!niceThrowable(Priority.ERROR, e, message, args)) { if (!niceThrowable(Priority.ERROR, e, message, args)) {
juli.log(Level.SEVERE, String.format(message, args), e); juli.log(Level.SEVERE, String.format(message, args), e);
Expand All @@ -287,7 +295,7 @@ public static void error(Throwable e, String message, Object... args) {
* @param args Pattern arguments * @param args Pattern arguments
*/ */
public static void fatal(String message, Object... args) { public static void fatal(String message, Object... args) {
if(useJuli || log4j == null) { if(forceJuli || log4j == null) {
try { try {
juli.severe(String.format(message, args)); juli.severe(String.format(message, args));
} catch (Throwable ex) { } catch (Throwable ex) {
Expand All @@ -309,7 +317,7 @@ public static void fatal(String message, Object... args) {
* @param args Pattern arguments * @param args Pattern arguments
*/ */
public static void fatal(Throwable e, String message, Object... args) { public static void fatal(Throwable e, String message, Object... args) {
if(useJuli || log4j == null) { if(forceJuli || log4j == null) {
try { try {
if (!niceThrowable(Priority.FATAL, e, message, args)) { if (!niceThrowable(Priority.FATAL, e, message, args)) {
juli.log(Level.SEVERE, String.format(message, args), e); juli.log(Level.SEVERE, String.format(message, args), e);
Expand Down Expand Up @@ -367,7 +375,7 @@ static boolean niceThrowable(Priority priority, Throwable e, String message, Obj
errorOut.println(playException.getErrorTitle()); errorOut.println(playException.getErrorTitle());
} }
errorOut.println(playException.getErrorDescription().replace("<strong>", "").replace("</strong>", "").replace("\n", " ")); 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); juli.log(toJuliLevel(priority.toString()), sw.toString(), e);
} else { } else {
log4j.log(priority, sw.toString(), e); log4j.log(priority, sw.toString(), e);
Expand Down
37 changes: 32 additions & 5 deletions framework/src/play/Play.java
Expand Up @@ -130,6 +130,8 @@ public static void init(File root, String id) {
Play.id = id; Play.id = id;
Play.started = false; Play.started = false;
Play.applicationPath = root; Play.applicationPath = root;

initStaticStuff();


// Guess the framework path // Guess the framework path
try { try {
Expand Down Expand Up @@ -284,7 +286,7 @@ public static synchronized void start() {
readConfiguration(); readConfiguration();
if (configuration.getProperty("play.tmp", "tmp").equals("none")) { if (configuration.getProperty("play.tmp", "tmp").equals("none")) {
tmpDir = null; 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 { } else {
tmpDir = new File(configuration.getProperty("play.tmp", "tmp")); tmpDir = new File(configuration.getProperty("play.tmp", "tmp"));
if (!tmpDir.isAbsolute()) { if (!tmpDir.isAbsolute()) {
Expand Down Expand Up @@ -418,7 +420,7 @@ public static void loadPlugins() {
urls = Play.classloader.getResources("play.plugins"); urls = Play.classloader.getResources("play.plugins");
} catch (Exception e) { } catch (Exception e) {
} }
while (urls.hasMoreElements()) { while (urls != null && urls.hasMoreElements()) {
URL url = urls.nextElement(); URL url = urls.nextElement();
try { try {
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8")); BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));
Expand All @@ -438,11 +440,36 @@ public static void loadPlugins() {
plugin.onLoad(); 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() { public static void loadModules() {
if (System.getenv("MODULES") != null) { if (System.getenv("MODULES") != null) {
// Modules path is prepended with a env property // Modules path is prepended with a env property
Expand Down
6 changes: 0 additions & 6 deletions framework/src/play/PlayPlugin.java
Expand Up @@ -129,12 +129,6 @@ public void afterActionInvocation() {
public void onConfigurationRead() { public void onConfigurationRead() {
} }


/**
* Called when the server is ready.
*/
public void onServerReady() {
}

/** /**
* Called after routes loading. * Called after routes loading.
*/ */
Expand Down
Expand Up @@ -21,8 +21,8 @@
* Generate valid JavaBeans. * Generate valid JavaBeans.
*/ */
public class PropertiesEnhancer extends Enhancer { public class PropertiesEnhancer extends Enhancer {

@Override @Override
public void enhanceThisClass(ApplicationClass applicationClass) throws Exception { public void enhanceThisClass(ApplicationClass applicationClass) throws Exception {
final CtClass ctClass = makeClass(applicationClass); final CtClass ctClass = makeClass(applicationClass);
if (ctClass.isInterface()) { if (ctClass.isInterface()) {
Expand Down
2 changes: 1 addition & 1 deletion framework/src/play/db/jpa/JPAPlugin.java
Expand Up @@ -45,7 +45,7 @@ public void onApplicationStart() {
for (Class clazz : classes) { for (Class clazz : classes) {
if (clazz.isAnnotationPresent(Entity.class)) { if (clazz.isAnnotationPresent(Entity.class)) {
cfg.addAnnotatedClass(clazz); cfg.addAnnotatedClass(clazz);
Logger.debug("JPA Model : %s", clazz); Logger.trace("JPA Model : %s", clazz);
} }
} }
Logger.debug("Initializing JPA ..."); Logger.debug("Initializing JPA ...");
Expand Down
6 changes: 3 additions & 3 deletions framework/src/play/libs/Java.java
Expand Up @@ -173,7 +173,7 @@ public static void findAllFields(Class clazz, Set<Field> found) {
public static FieldWrapper getFieldWrapper(Field field) { public static FieldWrapper getFieldWrapper(Field field) {
if (wrappers.get(field) == null) { if (wrappers.get(field) == null) {
FieldWrapper fw = new FieldWrapper(field); FieldWrapper fw = new FieldWrapper(field);
play.Logger.debug("caching %s", fw); play.Logger.trace("caching %s", fw);
wrappers.put(field, fw); wrappers.put(field, fw);
} }
return wrappers.get(field); return wrappers.get(field);
Expand Down Expand Up @@ -226,13 +226,13 @@ public void setValue(Object instance, Object value) {
} }
try { try {
if (setter != null) { 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); setter.invoke(instance, value);
} else { } else {
if (!accessible) { if (!accessible) {
field.setAccessible(true); 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); field.set(instance, value);
if (!accessible) { if (!accessible) {
field.setAccessible(accessible); field.setAccessible(accessible);
Expand Down
2 changes: 1 addition & 1 deletion framework/src/play/mvc/Router.java
Expand Up @@ -145,7 +145,7 @@ public static void route(Http.Request request) {
if (request.querystring != null && methodOverride.matches(request.querystring)) { if (request.querystring != null && methodOverride.matches(request.querystring)) {
Matcher matcher = methodOverride.matcher(request.querystring); Matcher matcher = methodOverride.matcher(request.querystring);
if (matcher.matches()) { 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"); request.method = matcher.group("method");
} }
} }
Expand Down
7 changes: 2 additions & 5 deletions framework/src/play/server/Server.java
Expand Up @@ -50,11 +50,8 @@ public Server() {
Logger.error("Could not bind on port " + httpPort, e); Logger.error("Could not bind on port " + httpPort, e);
acceptor.dispose(); acceptor.dispose();
} }


// Plugins Logger.info("");
for (PlayPlugin plugin : Play.plugins) {
plugin.onServerReady();
}
} }


public static void main(String[] args) { public static void main(String[] args) {
Expand Down
1 change: 1 addition & 0 deletions modules/gae/build.xml
Expand Up @@ -25,6 +25,7 @@
<include name="**/*.properties"/> <include name="**/*.properties"/>
<include name="**/*.xml"/> <include name="**/*.xml"/>
<include name="**/play.plugins"/> <include name="**/play.plugins"/>
<include name="**/play.static"/>
</fileset> </fileset>
</copy> </copy>
<jar destfile="lib/play-gae.jar" basedir="tmp/classes"> <jar destfile="lib/play-gae.jar" basedir="tmp/classes">
Expand Down
1 change: 1 addition & 0 deletions modules/gae/src/play.static
@@ -0,0 +1 @@
play.modules.gae.GAEInit
2 changes: 1 addition & 1 deletion modules/gae/src/play/modules/gae/GAECache.java
Expand Up @@ -110,7 +110,7 @@ Object unwrap(Object bytes) {


@Override @Override
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException { protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
return Play.classloader.loadClass(desc.getName()); return Class.forName(desc.getName(), false, Play.classloader);
} }
}.readObject(); }.readObject();
} catch (Exception e) { } catch (Exception e) {
Expand Down
18 changes: 18 additions & 0 deletions modules/gae/src/play/modules/gae/GAEInit.java
@@ -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");
}
}

}

0 comments on commit dbe4e07

Please sign in to comment.