Source Code Cross Referenced for Log4jConfigurer.java in  » J2EE » spring-framework-2.0.6 » org » springframework » util » 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 » J2EE » spring framework 2.0.6 » org.springframework.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2002-2005 the original author or authors.
003:         * 
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         * 
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         * 
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.springframework.util;
018:
019:        import java.io.File;
020:        import java.io.FileNotFoundException;
021:        import java.net.URL;
022:
023:        import org.apache.log4j.LogManager;
024:        import org.apache.log4j.PropertyConfigurator;
025:        import org.apache.log4j.xml.DOMConfigurator;
026:
027:        /**
028:         * Convenience class that features simple methods for custom Log4J configuration.
029:         *
030:         * <p>Only needed for non-default Log4J initialization, for example with a custom
031:         * config location or a refresh interval. By default, Log4J will simply read its
032:         * configuration from a "log4j.properties" file in the root of the class path.
033:         *
034:         * <p>For web environments, the analogous Log4jWebConfigurer class can be found
035:         * in the web package, reading in its configuration from context-params in web.xml.
036:         * In a J2EE web application, Log4J is usually set up via Log4jConfigListener or
037:         * Log4jConfigServlet, delegating to Log4jWebConfigurer underneath.
038:         *
039:         * @author Juergen Hoeller
040:         * @since 13.03.2003
041:         * @see org.springframework.web.util.Log4jWebConfigurer
042:         * @see org.springframework.web.util.Log4jConfigListener
043:         * @see org.springframework.web.util.Log4jConfigServlet
044:         */
045:        public abstract class Log4jConfigurer {
046:
047:            /** Pseudo URL prefix for loading from the class path: "classpath:" */
048:            public static final String CLASSPATH_URL_PREFIX = "classpath:";
049:
050:            /** Extension that indicates a Log4J XML config file: ".xml" */
051:            public static final String XML_FILE_EXTENSION = ".xml";
052:
053:            /**
054:             * Initialize Log4J from the given file location, with no config file refreshing.
055:             * Assumes an XML file in case of a ".xml" file extension, and a properties file else.
056:             * @param location the location of the config file: either a "classpath:" location
057:             * (e.g. "classpath:myLog4j.properties"), an absolute file URL
058:             * (e.g. "file:C:/log4j.properties), or a plain absolute path in the file system
059:             * (e.g. "C:/log4j.properties")
060:             * @throws FileNotFoundException if the location specifies an invalid file path
061:             */
062:            public static void initLogging(String location)
063:                    throws FileNotFoundException {
064:                String resolvedLocation = SystemPropertyUtils
065:                        .resolvePlaceholders(location);
066:                URL url = ResourceUtils.getURL(resolvedLocation);
067:                if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) {
068:                    DOMConfigurator.configure(url);
069:                } else {
070:                    PropertyConfigurator.configure(url);
071:                }
072:            }
073:
074:            /**
075:             * Initialize Log4J from the given location, with the given refresh interval
076:             * for the config file. Assumes an XML file in case of a ".xml" file extension,
077:             * and a properties file else.
078:             * <p>Log4J's watchdog thread will asynchronously check whether the timestamp
079:             * of the config file has changed, using the given interval between checks.
080:             * A refresh interval of 1000 milliseconds (one second), which allows to
081:             * do on-demand log level changes with immediate effect, is not unfeasible.
082:             * <p><b>WARNING:</b> Log4J's watchdog thread does not terminate until VM shutdown;
083:             * in particular, it does not terminate on LogManager shutdown. Therefore, it is
084:             * recommended to <i>not</i> use config file refreshing in a production J2EE
085:             * environment; the watchdog thread would not stop on application shutdown there.
086:             * @param location the location of the config file: either a "classpath:" location
087:             * (e.g. "classpath:myLog4j.properties"), an absolute file URL
088:             * (e.g. "file:C:/log4j.properties), or a plain absolute path in the file system
089:             * (e.g. "C:/log4j.properties")
090:             * @param refreshInterval interval between config file refresh checks, in milliseconds
091:             * @throws FileNotFoundException if the location specifies an invalid file path
092:             */
093:            public static void initLogging(String location, long refreshInterval)
094:                    throws FileNotFoundException {
095:                String resolvedLocation = SystemPropertyUtils
096:                        .resolvePlaceholders(location);
097:                File file = ResourceUtils.getFile(resolvedLocation);
098:                if (!file.exists()) {
099:                    throw new FileNotFoundException("Log4J config file ["
100:                            + resolvedLocation + "] not found");
101:                }
102:                if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) {
103:                    DOMConfigurator.configureAndWatch(file.getAbsolutePath(),
104:                            refreshInterval);
105:                } else {
106:                    PropertyConfigurator.configureAndWatch(file
107:                            .getAbsolutePath(), refreshInterval);
108:                }
109:            }
110:
111:            /**
112:             * Shut down Log4J, properly releasing all file locks.
113:             * <p>This isn't strictly necessary, but recommended for shutting down
114:             * Log4J in a scenario where the host VM stays alive (for example, when
115:             * shutting down an application in a J2EE environment).
116:             */
117:            public static void shutdownLogging() {
118:                LogManager.shutdown();
119:            }
120:
121:            /**
122:             * Set the specified system property to the current working directory.
123:             * <p>This can be used e.g. for test environments, for applications that leverage
124:             * Log4jWebConfigurer's "webAppRootKey" support in a web environment.
125:             * @param key system property key to use, as expected in Log4j configuration
126:             * (for example: "demo.root", used as "${demo.root}/WEB-INF/demo.log")
127:             * @see org.springframework.web.util.Log4jWebConfigurer
128:             */
129:            public static void setWorkingDirSystemProperty(String key) {
130:                System.setProperty(key, new File("").getAbsolutePath());
131:            }
132:
133:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.