Source Code Cross Referenced for LogUtil.java in  » Database-JDBC-Connection-Pool » DBPool » snaq » 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 » Database JDBC Connection Pool » DBPool » snaq.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package snaq.util;
002:
003:        import java.io.*;
004:        import java.text.*;
005:        import java.util.*;
006:
007:        /**
008:         * Base class providing simple logging and debug functionality, which can
009:         * either be instantiated and used as a logging object, or can be sub-classed
010:         * to be used as an integral logging facility.
011:         * <p>When specifying an OutputStream for use by the Logger, make sure that none
012:         * of the &quot;standard&quot; streams are used (System.out, System.err) as
013:         * they will be closed either when the close() method is called or the Logger
014:         * terminates. To send log to the standard output stream simply call
015:         * setLogging(true) and leave the actual log unspecified.
016:         * @author Giles Winstanley
017:         */
018:        public class LogUtil {
019:            protected DateFormat dateFormat, ddf;
020:            protected PrintStream log;
021:            protected boolean logging = false;
022:            protected boolean debug = false;
023:
024:            /**
025:             * Creates a new Logger with logging disabled.
026:             */
027:            public LogUtil() {
028:            }
029:
030:            /**
031:             * Creates a new Logger which writes to the specified file.
032:             * @throws FileNotFoundException if specified file is a directory, or cannot
033:             * be opened for some reason.
034:             */
035:            public LogUtil(File f) throws FileNotFoundException {
036:                setLog(new FileOutputStream(f, true));
037:            }
038:
039:            /**
040:             * Sets the date formatter for the logging.
041:             */
042:            public synchronized void setDateFormat(DateFormat df) {
043:                dateFormat = df;
044:            }
045:
046:            /**
047:             * Sets the log stream and enables logging.
048:             */
049:            public synchronized void setLog(OutputStream out) {
050:                if (log != null)
051:                    close();
052:                if (out != null)
053:                    log = new PrintStream(out);
054:                logging = true;
055:            }
056:
057:            /**
058:             * Sets the log writer and enables logging.
059:             */
060:            public synchronized void setLog(PrintStream ps) {
061:                if (log != null)
062:                    close();
063:                log = ps;
064:                logging = true;
065:            }
066:
067:            /**
068:             * Returns the current PrintStream used to write to the log.
069:             */
070:            public PrintStream getLogStream() {
071:                return log;
072:            }
073:
074:            /**
075:             * Writes a message to the log, with an optional prefix.
076:             */
077:            public synchronized void log(String prefix, String logEntry) {
078:                if (!logging)
079:                    return;
080:                StringBuffer sb = new StringBuffer();
081:                if (dateFormat != null)
082:                    sb.append(dateFormat.format(new Date()));
083:                else {
084:                    if (ddf == null)
085:                        ddf = DateFormat.getDateTimeInstance(DateFormat.LONG,
086:                                DateFormat.LONG);
087:                    sb.append(ddf.format(new Date()));
088:                }
089:
090:                sb.append(": ");
091:                if (prefix != null)
092:                    sb.append(prefix);
093:                sb.append(logEntry);
094:                if (log != null) {
095:                    synchronized (log) {
096:                        log.println(sb.toString());
097:                        //				log.flush();
098:                    }
099:                }
100:            }
101:
102:            /**
103:             * Writes a message to the log.
104:             */
105:            public void log(String logEntry) {
106:                log("", logEntry);
107:            }
108:
109:            /**
110:             * Writes a message with an Exception to the log file.
111:             */
112:            public void log(Throwable e, String prefix, String logEntry) {
113:                if (!logging)
114:                    return;
115:                log(prefix, logEntry);
116:                e.printStackTrace(log);
117:                log.flush();
118:            }
119:
120:            /**
121:             * Writes a message with an Exception to the log file.
122:             */
123:            public void log(Throwable e, String logEntry) {
124:                log(e, "", logEntry);
125:            }
126:
127:            /**
128:             * Writes an Exception to the log file.
129:             */
130:            public void log(Throwable e) {
131:                log(e, e.getMessage());
132:            }
133:
134:            /**
135:             * Closes the log.
136:             */
137:            public synchronized void close() {
138:                logging = false;
139:                if (log != null) {
140:                    log.flush();
141:                    if (!isSystemLog())
142:                        log.close();
143:                }
144:                log = null;
145:            }
146:
147:            // Returns whether the log is writing to a system log stream.
148:            private boolean isSystemLog() {
149:                return (!log.equals(System.out) && !log.equals(System.err));
150:            }
151:
152:            /**
153:             * Determines whether to write to the log.
154:             */
155:            public void setLogging(boolean b) {
156:                logging = b;
157:            }
158:
159:            /**
160:             * Returns whether logging is enabled.
161:             */
162:            public boolean isLogging() {
163:                return logging;
164:            }
165:
166:            /**
167:             * Determines whether to perform debug logging.
168:             */
169:            public void setDebug(boolean b) {
170:                debug = b;
171:            }
172:
173:            /**
174:             * Returns whether debug logging is enabled.
175:             */
176:            public boolean isDebug() {
177:                return debug;
178:            }
179:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.