Java EE Servlet/JSP tutorial : Adding an error page, logging, and other forms of debugging
From Resin 4.0 Wiki
Line 73: | Line 73: | ||
# app-0.log_level : finest | # app-0.log_level : finest | ||
</pre> | </pre> | ||
+ | |||
+ | |||
+ | |||
+ | == Using Java log API == | ||
+ | <pre> | ||
+ | package com.bookstore; | ||
+ | ... | ||
+ | import java.util.logging.Level; | ||
+ | import java.util.logging.Logger; | ||
+ | |||
+ | public class Book implements Cloneable { | ||
+ | |||
+ | private transient Logger log = Logger.getLogger(Book.class.getName()); | ||
+ | ... | ||
+ | |||
+ | public Book cloneMe() { | ||
+ | try { | ||
+ | return (Book) super.clone(); | ||
+ | } catch (CloneNotSupportedException e) { | ||
+ | log.logp(Level.WARNING, | ||
+ | this.getClass().getName(), | ||
+ | "cloneMe", "Unable to clone", e); | ||
+ | return null; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | ... | ||
+ | |||
+ | |||
+ | </pre> | ||
+ | |||
+ | Info about logp | ||
+ | http://docs.oracle.com/javase/6/docs/api/java/util/logging/Logger.html#logp(java.util.logging.Level, java.lang.String, java.lang.String, java.lang.String, java.lang.Throwable) | ||
+ | |||
+ | Why you should not use static | ||
+ | http://wiki.apache.org/commons/Logging/StaticLog | ||
+ | |||
+ | |||
+ | Another example: | ||
+ | |||
+ | <pre> | ||
+ | package com.bookstore; | ||
+ | |||
+ | ... | ||
+ | import java.util.logging.Logger; | ||
+ | |||
+ | import javax.enterprise.context.ApplicationScoped; | ||
+ | |||
+ | @ApplicationScoped @InMemory | ||
+ | public class BookRepositoryCollectionsImpl implements BookRepository { | ||
+ | ... | ||
+ | private Book book(String title, String description, String aPrice, | ||
+ | String aPubDate) { | ||
+ | |||
+ | Date pubDate = null; | ||
+ | BigDecimal price = null; | ||
+ | |||
+ | try { | ||
+ | price = new BigDecimal(aPrice); | ||
+ | }catch (Exception ex) { | ||
+ | log.throwing(this.getClass().getName(), "book", ex); | ||
+ | } | ||
+ | |||
+ | try { | ||
+ | pubDate = dateFormat.parse(aPubDate); | ||
+ | }catch (Exception ex) { | ||
+ | log.throwing(this.getClass().getName(), "book", ex); | ||
+ | } | ||
+ | |||
+ | return new Book("" + (count++), title, description, price, pubDate); | ||
+ | |||
+ | } | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | Info about throwing | ||
+ | http://docs.oracle.com/javase/6/docs/api/java/util/logging/Logger.html#throwing(java.lang.String, java.lang.String, java.lang.Throwable) |
Revision as of 00:00, 9 May 2012
In progress....
Contents |
Background on logging
Background information on logging
http://www.vogella.com/articles/Logging/article.html
http://onjava.com/onjava/2002/06/19/log.html
http://tutorials.jenkov.com/java-logging/index.html
http://docs.oracle.com/javase/7/docs/api/java/util/logging/SimpleFormatter.html
Common problem with logging
http://stackoverflow.com/questions/6315699/why-are-my-level-fine-logging-messages-not-showing
Understandng the outptut for logging
http://docs.oracle.com/javase/7/docs/technotes/guides/logging/index.html
http://docs.oracle.com/javase/7/docs/technotes/guides/logging/overview.html
http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax
Resin and logging
Logging in Resin
http://www.caucho.com/resin-4.0/admin/logging.xtp
http://www.caucho.com/resin-4.0/admin/resin-admin-command-line.xtp#loglevelsettingloglevel
Changing log levels in Resin:
~/workspace/javaee/Servers/Resin 4.0 at localhost-config/resin.xml
... <!-- Logging configuration for the JDK logging API --> <log-handler name="" level="all" path="stdout:" timestamp="[%y-%m-%d %H:%M:%S.%s]" format=" {${thread}} ${log.message}"/> ... <!-- - level='info' for production - 'fine' or 'finer' for development and troubleshooting --> <logger name="" level="${log_level?:'info'}"/> <logger name="com.caucho.java" level="config"/> <logger name="com.caucho.loader" level="config"/>
Changing log levels in Resin:
~/workspace/javaee/Servers/Resin 4.0 at localhost-config/resin.properties
# log_level : info # app-0.log_level : finest
Using Java log API
package com.bookstore; ... import java.util.logging.Level; import java.util.logging.Logger; public class Book implements Cloneable { private transient Logger log = Logger.getLogger(Book.class.getName()); ... public Book cloneMe() { try { return (Book) super.clone(); } catch (CloneNotSupportedException e) { log.logp(Level.WARNING, this.getClass().getName(), "cloneMe", "Unable to clone", e); return null; } } ...
Info about logp http://docs.oracle.com/javase/6/docs/api/java/util/logging/Logger.html#logp(java.util.logging.Level, java.lang.String, java.lang.String, java.lang.String, java.lang.Throwable)
Why you should not use static http://wiki.apache.org/commons/Logging/StaticLog
Another example:
package com.bookstore; ... import java.util.logging.Logger; import javax.enterprise.context.ApplicationScoped; @ApplicationScoped @InMemory public class BookRepositoryCollectionsImpl implements BookRepository { ... private Book book(String title, String description, String aPrice, String aPubDate) { Date pubDate = null; BigDecimal price = null; try { price = new BigDecimal(aPrice); }catch (Exception ex) { log.throwing(this.getClass().getName(), "book", ex); } try { pubDate = dateFormat.parse(aPubDate); }catch (Exception ex) { log.throwing(this.getClass().getName(), "book", ex); } return new Book("" + (count++), title, description, price, pubDate); }
Info about throwing http://docs.oracle.com/javase/6/docs/api/java/util/logging/Logger.html#throwing(java.lang.String, java.lang.String, java.lang.Throwable)