Source Code Cross Referenced for Logger.java in  » Database-JDBC-Connection-Pool » Primrose » uk » org » primrose » 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 » Primrose » uk.org.primrose 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         *	Library nam : Primrose - A Java Database Connection Pool.
003:         *	Published by Ben Keeping, http://primrose.org.uk .
004:         *	Copyright (C) 2004 Ben Keeping, primrose.org.uk
005:         *	Email: Use "Contact Us Form" on website
006:         *
007:         *	This library is free software; you can redistribute it and/or
008:         *	modify it under the terms of the GNU Lesser General Public
009:         *	License as published by the Free Software Foundation; either
010:         *	version 2.1 of the License, or (at your option) any later version.
011:         *
012:         *	This library is distributed in the hope that it will be useful,
013:         *	but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015:         *	Lesser General Public License for more details.
016:         *
017:         *	You should have received a copy of the GNU Lesser General Public
018:         *	License along with this library; if not, write to the Free Software
019:         *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
020:         */package uk.org.primrose;
021:
022:        import java.io.*;
023:        import java.util.*;
024:
025:        /**
026:         *	Logging class for the pools.
027:         */
028:        public class Logger {
029:            // The log file to log to
030:            private PrintWriter logWriter = null;
031:            private boolean logVerbose = false;
032:            private boolean logInfo = false;
033:            private boolean logWarn = false;
034:            private boolean logError = false;
035:            private boolean logCrisis = false;
036:            //private String logLevel = "";
037:            private int logDayOfMonth = -1;
038:            private String origLogName = null;
039:            private String emailEvents = null;
040:            private String mxServer = null;
041:            private String toAddress = null;
042:            private String poolName = null;
043:
044:            public void setLogWriter(String log) throws IOException {
045:                if (origLogName == null)
046:                    origLogName = log;
047:
048:                Calendar cal = Calendar.getInstance();
049:                int localLogDayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
050:                boolean makeNewLog = false;
051:
052:                if (logDayOfMonth == -1 && logWriter == null)
053:                    makeNewLog = true; // indicates first start up
054:
055:                if (origLogName.indexOf("${") > -1
056:                        && localLogDayOfMonth != logDayOfMonth) {
057:                    String dateFormat = origLogName.substring(origLogName
058:                            .indexOf("${") + 2, origLogName.indexOf("}"));
059:                    String logPrefix = origLogName.substring(0, origLogName
060:                            .indexOf("${"));
061:                    String logSuffix = origLogName.substring(origLogName
062:                            .indexOf("}") + 1, origLogName.length());
063:                    java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat(
064:                            dateFormat);
065:                    java.util.Date tmp = new java.util.Date();
066:                    String formattedDate = sdf2.format(tmp);
067:                    log = (logPrefix + formattedDate + logSuffix);
068:                    logDayOfMonth = localLogDayOfMonth;
069:                    makeNewLog = true;
070:                }
071:
072:                if (makeNewLog) {
073:                    if (DebugLogger.getEnabled())
074:                        DebugLogger.log("[Logger@" + poolName
075:                                + "] Making new log (" + log + ")");
076:                    if (logWriter != null)
077:                        logWriter.close();
078:                    logWriter = new PrintWriter(
079:                            new FileOutputStream(log, true), true);
080:                }
081:            }
082:
083:            public void setEmailDetails(String emailEvents, String toAddress,
084:                    String mxServer, String poolName) {
085:                if (emailEvents != null && emailEvents.length() > 0
086:                        && !emailEvents.equals("null")) {
087:                    this .emailEvents = emailEvents.toUpperCase();
088:                    this .emailEvents += ",CRISIS"; // Always add CRISIS event if want have email events
089:                    this .toAddress = toAddress;
090:                    this .mxServer = mxServer;
091:                    this .poolName = poolName;
092:                }
093:            }
094:
095:            public void setLogLevel(String level) {
096:                if (level != null && level.length() > 0) {
097:                    String[] levels = level.split(",");
098:                    for (int i = 0; i < levels.length; i++) {
099:                        if (levels[i].equalsIgnoreCase("verbose")) {
100:                            logVerbose = true;
101:                        } else if (levels[i].equalsIgnoreCase("info")) {
102:                            logInfo = true;
103:                        } else if (levels[i].equalsIgnoreCase("warn")) {
104:                            logWarn = true;
105:                        } else if (levels[i].equalsIgnoreCase("error")) {
106:                            logError = true;
107:                        } else if (levels[i].equalsIgnoreCase("crisis")) {
108:                            logCrisis = true;
109:                        } else if (levels[i].equalsIgnoreCase("debug")) {
110:                            DebugLogger.setEnabled(true);
111:                        }
112:                    }
113:                }
114:            }
115:
116:            /**
117:             *	Print a stack trace from an exception to the logs
118:             */
119:            public void printStackTrace(Throwable t) {
120:                if (logWriter != null) {
121:                    t.printStackTrace(logWriter);
122:                } else {
123:                    t.printStackTrace(System.err);
124:                }
125:                if (DebugLogger.getEnabled())
126:                    DebugLogger.printStackTrace(t);
127:            }
128:
129:            /**
130:             *	Close the logger's file handle.
131:             */
132:            public void close() {
133:                if (logWriter != null) {
134:                    logWriter.close();
135:                }
136:
137:            }
138:
139:            /**
140:             *	Log verbose messages
141:             */
142:            public void verbose(String data) {
143:                if (logVerbose)
144:                    log("VERBOSE", data);
145:                if (DebugLogger.getEnabled())
146:                    DebugLogger.log(data);
147:            }
148:
149:            /**
150:             *	Log an info message
151:             */
152:            public void info(byte[] data) {
153:                if (logInfo)
154:                    log("INFO", new String(data));
155:            }
156:
157:            /**
158:             *	Log an info message
159:             */
160:            public void info(String data) {
161:                if (logInfo)
162:                    log("INFO", data);
163:                if (DebugLogger.getEnabled())
164:                    DebugLogger.log(data);
165:
166:            }
167:
168:            /**
169:             *	Log an warn message
170:             */
171:            public void warn(String data) {
172:                if (logWarn)
173:                    log("WARN", data);
174:                if (DebugLogger.getEnabled())
175:                    DebugLogger.log(data);
176:
177:            }
178:
179:            /**
180:             *	Log an error message
181:             */
182:            public void error(String data) {
183:                if (logError)
184:                    log("ERROR", data);
185:                if (DebugLogger.getEnabled())
186:                    DebugLogger.log(data);
187:            }
188:
189:            public void email(String eventType, String message) {
190:                if (emailEvents == null
191:                        || emailEvents.indexOf(eventType.toUpperCase()) == -1) {
192:                    return;
193:                }
194:
195:                if (DebugLogger.getEnabled())
196:                    DebugLogger.log("About to email event " + eventType
197:                            + " :: " + message);
198:
199:                String host = "unknown_host";
200:                try {
201:                    host = java.net.InetAddress.getLocalHost().getHostName();
202:                } catch (Exception e) {
203:                    printStackTrace(e);
204:                }
205:
206:                //String fromAddress = poolName +"@" +host;
207:                //String fromAddress = poolName +"_at_" +host +"@primrose.org.uk";
208:                String fromAddress = "pools@primrose.org.uk";
209:                String subject = eventType + " : " + poolName + "@" + host;
210:                info("Sending email for eventType(" + eventType
211:                        + "), toAddress(" + toAddress + "), fromAddress("
212:                        + fromAddress + ") message(" + message + ")");
213:
214:                try {
215:                    new SendMail(this , mxServer, toAddress, fromAddress,
216:                            subject, message).send();
217:                } catch (Exception e) {
218:                    printStackTrace(e);
219:                }
220:            }
221:
222:            /**
223:             *	Log an fatal message and email the world !
224:             */
225:            public void crisis(String message) {
226:                if (logCrisis)
227:                    log("CRISIS", message);
228:                if (DebugLogger.getEnabled())
229:                    DebugLogger.log(message);
230:                if (emailEvents == null)
231:                    return;
232:                email("CRISIS", message);
233:            }
234:
235:            /**
236:             *	Log an info message
237:             */
238:            private void log(String level, String data) {
239:                try {
240:                    this .setLogWriter(this .origLogName); // alter log name if we need to (if date changes && they want date logging)
241:                } catch (IOException ioe) {
242:                    ioe.printStackTrace();
243:                }
244:
245:                Calendar now = Calendar.getInstance();
246:                String nowString = now.get(Calendar.DAY_OF_MONTH) + "/"
247:                        + (now.get(Calendar.MONTH) + 1) + "/"
248:                        + now.get(Calendar.YEAR) + " "
249:                        + now.get(Calendar.HOUR_OF_DAY) + ":"
250:                        + now.get(Calendar.MINUTE) + ":"
251:                        + now.get(Calendar.SECOND);
252:
253:                String message = nowString + " : " + level + ": " + data;
254:
255:                if (logWriter == null) {
256:                    System.out.println(message);
257:                } else {
258:                    logWriter.println(message);
259:                }
260:            }
261:
262:            /**
263:             *	Print a blank new line / line break to the log
264:             */
265:            public void linebreak() {
266:                if (logWriter == null) {
267:                    System.out.println("\n");
268:                } else {
269:                    logWriter.println("\n");
270:                }
271:            }
272:
273:            public String getLogLevel() {
274:                // TODO Auto-generated method stub
275:                return null;
276:            }
277:
278:            public int getLogDayOfMonth() {
279:                return logDayOfMonth;
280:            }
281:
282:            public void setLogDayOfMonth(int logDayOfMonth) {
283:                this.logDayOfMonth = logDayOfMonth;
284:            }
285:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.