Source Code Cross Referenced for LoggerServlet.java in  » Content-Management-System » TransferCM » com » methodhead » servlet » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » TransferCM » com.methodhead.servlet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* 
002:         * Copyright (C) 2006 Methodhead Software LLC.  All rights reserved.
003:         * 
004:         * This file is part of TransferCM.
005:         * 
006:         * TransferCM is free software; you can redistribute it and/or modify it under the
007:         * terms of the GNU General Public License as published by the Free Software
008:         * Foundation; either version 2 of the License, or (at your option) any later
009:         * version.
010:         * 
011:         * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012:         * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013:         * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
014:         * details.
015:         * 
016:         * You should have received a copy of the GNU General Public License along with
017:         * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018:         * Fifth Floor, Boston, MA  02110-1301  USA
019:         */
020:
021:        package com.methodhead.servlet;
022:
023:        import java.io.FileInputStream;
024:        import java.io.InputStream;
025:        import java.io.IOException;
026:
027:        import java.util.Properties;
028:
029:        import javax.servlet.ServletContext;
030:
031:        import javax.servlet.http.HttpServlet;
032:        import javax.servlet.http.HttpServletRequest;
033:        import javax.servlet.http.HttpServletResponse;
034:
035:        import org.apache.log4j.BasicConfigurator;
036:        import org.apache.log4j.Level;
037:        import org.apache.log4j.PropertyConfigurator;
038:
039:        /**
040:         * <p>
041:         *   A servlet to initialize the <a
042:         *   href="http://jakarta.apache.org/log4j/docs/index.html">Log4j</a> logger.
043:         *   This servlet sets up a very simple configuration and accepts a few
044:         *   optional parameters.  Three init-params may be specified:
045:         * </p>
046:         * <ul>
047:         *   <li>
048:         *     <strong>logFile</strong>: the path of the log file.  If the path begins
049:         *     with a forward slash it is assumed to be an absolute path, otherwise it
050:         *     is assumed to be a path relative to the context's root.  For example:
051:         *     <tt>WEB-INF/log.txt</tt> or <tt>/var/log/log.txt</tt>.  If this
052:         *     parameter is not specified, it defaults to <tt>WEB-INF/log.txt</tt>
053:         *   </li>
054:         *   <li>
055:         *     <strong>level</strong>: the log level.  Legal values are <tt>ALL</tt>,
056:         *     <tt>DEBUG</tt>, <tt>ERROR</tt>, <tt>FATAL</tt>, <tt>INFO</tt>,
057:         *     <tt>OFF</tt>, <tt>WARN</tt>.  Note that these level correspond to the
058:         *     log levels defined by Log4j.  If this parameter is not specified, it
059:         *     defaults to <tt>ERROR</tt>.
060:         *   </li>
061:         *   <li>
062:         *     <strong>configFile</strong>: the path of a properties file containing
063:         *     Log4j configuration information.  Like <tt>logFile</tt>, if the path
064:         *     begins with a forward slash it is assumed to be an absolute path,
065:         *     otherwise it is assumed to be a path relative to the context's root.
066:         *     This file should follow the conventions described in the Log4j
067:         *     documentation.
068:         *   </li>
069:         * </ul>
070:         * <p>
071:         *   If no init-params are specified, the following configuration is created:
072:         * </p>
073:         * <pre>
074:         *   log4j.appender.default=org.apache.log4j.FileAppender
075:         *   log4j.appender.default.file=WEB-INF/log.txt                 # logFile
076:         *   log4j.appender.default.layout=org.apache.log4j.PatternLayout
077:         *   log4j.appender.default.layout.ConversionPattern=org.apache.log4j.PatternLayout=%d %-5p %c - %m%n
078:         *   log4j.rootLogger=ERROR,default                              # level</pre>
079:         * <p>
080:         *   Specifying <tt>logFile</tt> and <tt>level</tt> overrides the properties
081:         *   indicated above.  If <tt>configFile</tt> is specified, its properties will
082:         *   be combined with those above, overriding any conflicting properties. 
083:         * </p>
084:         * <p>
085:         *   So, as an example, if you wanted to enable DEBUG logging for this class
086:         *   only and you wanted to override the output format, you could add the
087:         *   following lines to your <tt>configFile</tt>:
088:         * </p>
089:         * <pre>
090:         *   log4j.rootLogger=OFF
091:         *   log4j.logger.com.methodhead.servlet.LoggerServlet=DEBUG
092:         *   log4j.appender.default.layout=org.apache.log4j.PatternLayout
093:         *   log4j.appender.default.layout.ConversionPattern=%d [%t] %-5p %c - %m%n</pre>
094:         */
095:        public class LoggerServlet extends HttpServlet {
096:
097:            // constructors /////////////////////////////////////////////////////////////
098:
099:            // constants ////////////////////////////////////////////////////////////////
100:
101:            // classes //////////////////////////////////////////////////////////////////
102:
103:            // methods //////////////////////////////////////////////////////////////////
104:
105:            public void init() {
106:                //
107:                // get init-params
108:                //
109:                String configFileParam = getInitParameter("configFile");
110:                String logFileParam = getInitParameter("logFile");
111:                String levelParam = getInitParameter("level");
112:
113:                //
114:                // reset configuration
115:                //
116:                BasicConfigurator.resetConfiguration();
117:
118:                Properties props = new Properties();
119:                props.put("log4j.appender.default",
120:                        "org.apache.log4j.FileAppender");
121:                props.put("log4j.appender.default.layout",
122:                        "org.apache.log4j.PatternLayout");
123:                props.put("log4j.appender.default.layout.ConversionPattern",
124:                        "%d %-5p %c - %m%n");
125:
126:                //
127:                // determine log file
128:                //
129:                String file = logFileParam;
130:
131:                if (logFileParam == null)
132:                    file = getServletContext().getRealPath("WEB-INF/log.txt");
133:
134:                else if (!logFileParam.startsWith("/")) {
135:                    file = getServletContext().getRealPath(logFileParam);
136:
137:                    if (file == null) {
138:                        getServletContext().log(
139:                                "LoggerServlet: Couldn't get real path for "
140:                                        + logFileParam
141:                                        + "; defaulting to WEB-INF/log.txt.");
142:                        file = getServletContext().getRealPath(
143:                                "WEB-INF/log.txt");
144:                    }
145:                }
146:
147:                props.put("log4j.appender.default.file", file);
148:
149:                //
150:                // determine level
151:                //
152:                Level level = null;
153:                if ("ALL".equals(levelParam))
154:                    level = Level.ALL;
155:                else if ("DEBUG".equals(levelParam))
156:                    level = Level.DEBUG;
157:                else if ("ERROR".equals(levelParam))
158:                    level = Level.ERROR;
159:                else if ("FATAL".equals(levelParam))
160:                    level = Level.FATAL;
161:                else if ("INFO".equals(levelParam))
162:                    level = Level.INFO;
163:                else if ("OFF".equals(levelParam))
164:                    level = Level.OFF;
165:                else if ("WARN".equals(levelParam))
166:                    level = Level.WARN;
167:                else {
168:                    getServletContext().log(
169:                            "LoggerServlet: Unexpected level " + levelParam
170:                                    + "; defaulting to ERROR.");
171:                    level = Level.ERROR;
172:                }
173:
174:                props.put("log4j.rootLogger", level.toString() + ",default");
175:
176:                //
177:                // load config properties
178:                //
179:                if (configFileParam != null) {
180:                    String configFile = configFileParam;
181:                    if (!configFileParam.startsWith("/")) {
182:                        configFile = getServletContext().getRealPath(
183:                                configFileParam);
184:
185:                        if (configFile == null) {
186:                            getServletContext().log(
187:                                    "LoggerServlet: Couldn't get real path for "
188:                                            + configFileParam + ".");
189:                        }
190:                    }
191:
192:                    if (configFile != null) {
193:                        try {
194:                            InputStream in = new FileInputStream(configFile);
195:                            props.load(in);
196:                            in.close();
197:                        } catch (IOException e) {
198:                            getServletContext().log(
199:                                    "LoggerServlet: Unexpected IOException "
200:                                            + e);
201:                        }
202:                    }
203:                }
204:
205:                //
206:                // configure the logger
207:                //
208:                PropertyConfigurator.configure(props);
209:            }
210:
211:            public void doGet(HttpServletRequest req, HttpServletResponse res) {
212:            }
213:
214:            // properties ///////////////////////////////////////////////////////////////
215:
216:            // attributes ///////////////////////////////////////////////////////////////
217:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.