Now myLogger will log to both the console and the file /var/log/tomcat/myapp.log. Handler consoleHandler = new ConsoleHandler() Handler fileHandler = new FileHandler("/var/log/tomcat/myapp.log") What if you wanted the output to appear in a file and on the console? For that you need to define 2 Handlers. For example you might want to turn this logger off, because it's not that useful. This is important to know when you want to alter the configuration of the Logger. Notice that both the name of the Logger and the method that logged the message are mentioned in the log statement. If you ran this code with a deployed web application you would see this statement on the console or in catalina.out: For example you could now try something like this: Now you have a logger to create logging messages from your class CriticalComponent. You may also want to think about why myLogger is a static field (Hint myLogger is shared among all instances of CriticalComponent). This is important to the material explaining Tomcat's logging configuration. Pay attention to how we defined the name of the Logger. Private static Logger myLogger = Logger.getLogger(nameOfLogger) If you wanted to log something from your web application's class CriticalComponent using you would first create a logger like this: What's the purpose of a Logger? A Logger is what a developer uses to write log statements to the console, to a file, to the network, etc. The official Tomcat logging documentation refers to the above concepts / classes extensively. The following concepts (Classes) are important: The section on Tomcat's configuration file discusses how to accomplish declaratively what is done programmatically in this section. The examples used in this section show programmatically how logging is configured. One may think it's wise to skip this section and proceed directly to the section on the configuration file, but all the concepts talked about here are necessary to understand the configuration file. The goal of this primer is to demonstrate how to log with, as implemented by JULI, as a backdrop to the rest of the tutorial, in particular the sections related to configuring Tomcat logging with the configuration file. In no event shall the author be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with this document or the use or other dealings in this document. This document is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and non infringement.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |