You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

advanced-logging.asciidoc 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. ---
  2. title: Logging
  3. order: 13
  4. layout: page
  5. ---
  6. [[advanced.logging]]
  7. = Logging
  8. (((, id="term.advanced.logging", range="startofrange")))
  9. You can do logging in Vaadin application using the standard
  10. [package]#java.util.logging# facilities. Configuring logging is as easy as
  11. putting a file named [filename]#logging.properties# in the default package of
  12. your Vaadin application ( [filename]#src# in an Eclipse project or
  13. [filename]#src/main/java# or [filename]#src/main/resources# in a Maven project).
  14. This file is read by the [classname]#Logger# class when a new instance of it is
  15. initialize.
  16. [[advanced.logging.tomcat]]
  17. == Logging in Apache Tomcat
  18. For logging Vaadin applications deployed in Apache Tomcat, you do not need to do
  19. anything special to log to the same place as Tomcat itself. If you need to write
  20. the Vaadin application related messages elsewhere, just add a custom
  21. [filename]#logging.properties# file to the default package of your Vaadin
  22. application.
  23. If you would like to pipe the log messages through another logging solution, see
  24. <<advanced.logging.slf4j>> below.
  25. [[advanced.logging.liferay]]
  26. == Logging in Liferay
  27. Liferay mutes logging through [package]#java.util.logging# by default. In order
  28. to enable logging, you need to add a [filename]#logging.properties# file of your
  29. own to the default package of your Vaadin application. This file should define
  30. at least one destination where to save the log messages.
  31. You can also log through SLF4J, which is used in and bundled with Liferay.
  32. Follow the instructions in <<advanced.logging.slf4j>>.
  33. [[advanced.logging.slf4j]]
  34. == Piping to Log4j using SLF4J
  35. ((("Log4j")))
  36. ((("SLF4J")))
  37. Piping output from [package]#java.util.logging# to Log4j is easy with SLF4J (
  38. http://slf4j.org/). The basic way to go about this is to add the SLF4J JAR file
  39. as well as the [filename]#jul-to-slf4j.jar# file, which implements the bridge
  40. from [package]#java.util.logging#, to SLF4J. You will also need to add a third
  41. logging implementation JAR file, that is, [filename]#slf4j-log4j12-x.x.x.jar#,
  42. to log the actual messages using Log4j. For more info on this, please visit the
  43. SLF4J site.
  44. In order to get the [package]#java.util.logging# to SLF4J bridge installed, you
  45. need to add the following snippet of code to your [classname]#VaadinServlet# class at the
  46. very top:
  47. [source, java]
  48. ----
  49. static {
  50. SLF4JBridgeHandler.install();
  51. }
  52. ----
  53. This will make sure that the bridge handler is installed and working before
  54. Vaadin starts to process any logging calls. If your app consists of multiple servlets,
  55. you should initialize the SLF4J logging in a [classname]#ServletContextListener# instead,
  56. so that the servlets' initialization methods uses the proper logger.
  57. [WARNING]
  58. .Please note!
  59. ====
  60. This can seriously impact on the cost of disabled logging statements (60-fold
  61. increase) and a measurable impact on enabled log statements (20% overall
  62. increase). However, Vaadin doesn't log very much, so the effect on performance
  63. will be negligible.
  64. ====
  65. [[advanced.logging.core]]
  66. == Using Logger
  67. You can do logging with a simple pattern where you register a static logger
  68. instance in each class that needs logging, and use this logger wherever logging
  69. is needed in the class. For example:
  70. [source, java]
  71. ----
  72. public class MyClass {
  73. private final static Logger logger =
  74. Logger.getLogger(MyClass.class.getName());
  75. public void myMethod() {
  76. try {
  77. // do something that might fail
  78. } catch (Exception e) {
  79. logger.log(Level.SEVERE, "FAILED CATASTROPHICALLY!", e);
  80. }
  81. }
  82. }
  83. ----
  84. ifdef::vaadin7[]
  85. ((("static")))
  86. ((("memory
  87. leak")))
  88. ((("PermGen")))
  89. Having a [literal]#++static++# logger instance for each class needing logging
  90. saves a bit of memory and time compared to having a logger for every logging
  91. class instance. However, it could cause the application to leak PermGen memory
  92. with some application servers when redeploying the application. The problem is
  93. that the [classname]#Logger# may maintain hard references to its instances. As
  94. the [classname]#Logger# class is loaded with a classloader shared between
  95. different web applications, references to classes loaded with a per-application
  96. classloader would prevent garbage-collecting the classes after redeploying,
  97. hence leaking memory. As the size of the PermGen memory where class object are
  98. stored is fixed, the leakage will lead to a server crash after many
  99. redeployments. The issue depends on the way how the server manages classloaders,
  100. on the hardness of the back-references.
  101. So, if you experience PermGen issues, or want to play it on the safe
  102. side, you should consider using non-static [classname]#Logger# instances.
  103. //As discussed in Forum thread 1175841 (24.2.2012).
  104. endif::vaadin7[]
  105. (((range="endofrange", startref="term.advanced.logging")))