forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileLogger.java
More file actions
122 lines (117 loc) · 3.89 KB
/
Copy pathFileLogger.java
File metadata and controls
122 lines (117 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*-*- mode: Java; tab-width:8 -*-*/
/*
* Copyright (C) 2003-2007 Jost Boekemeier
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE 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 NONINFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) 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 THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
package php.java.bridge;
import java.io.FileNotFoundException;
import java.util.Date;
/**
* The classic PHP/Java Bridge logger, doesn't need any external libraries.
*/
class FileLogger implements ILogger {
static boolean haveDateFormat=true;
private static Object _form;
private boolean isInit = false;
private void init() {
if(Util.logStream==null) {
if((Util.DEFAULT_LOG_FILE==null) || (Util.DEFAULT_LOG_FILE.trim().length()==0)) Util.logStream = System.err;
else
try {
Util.logStream=new java.io.PrintStream(new java.io.FileOutputStream(Util.DEFAULT_LOG_FILE));
} catch (FileNotFoundException e1) {Util.logStream=System.err;}
}
isInit = true;
}
/**
* Create a String containing the current date/time.
* @return The date/time as a String
*/
public String now() {
if(!haveDateFormat) return String.valueOf(System.currentTimeMillis());
try {
if(_form==null)
_form = new java.text.SimpleDateFormat("MMM dd HH:mm:ss", java.util.Locale.ENGLISH);
return ((java.text.SimpleDateFormat)_form).format(new Date());
} catch (Throwable t) {
haveDateFormat=false;
return now();
}
}
/**
* Log a message
* @param s The message
*/
public void log(String s) {
if(!isInit) init();
byte[] bytes = null;
try {
bytes = s.getBytes(Util.UTF8);
} catch (java.io.UnsupportedEncodingException e) {
Util.printStackTrace(e);
bytes = s.getBytes();
}
Util.logStream.write(bytes, 0, bytes.length);
Util.logStream.println("");
Util.logStream.flush();
}
/* (non-Javadoc)
* @see php.java.bridge.ILogger#printStackTrace(java.lang.Throwable)
*/
public void printStackTrace(Throwable t) {
if(!isInit) init();
if(Util.logLevel > 0) {
if (t instanceof Error) {
Util.println(1, "An error occured: " + t);
} else if (Util.logLevel > 1) {
Util.println(2, "An exception occured: " + t);
}
t.printStackTrace(Util.logStream);
}
}
/* (non-Javadoc)
* @see php.java.bridge.ILogger#log(int, java.lang.String)
*/
public void log(int level, String msg) {
StringBuffer b = new StringBuffer(now());
b.append(" "); b.append(Util.EXTENSION_NAME); b.append(" ");
switch(level) {
case 1: b.append("FATAL"); break;
case 2: b.append("ERROR"); break;
case 3: b.append("INFO "); break;
case 4: b.append("DEBUG"); break;
default: b.append(level); break;
}
b.append(": ");
b.append(msg);
log(b.toString());
}
public void warn(String msg) {
StringBuffer b = new StringBuffer(now());
b.append(" "); b.append(Util.EXTENSION_NAME); b.append(" ");
b.append("WARNING");
b.append(": ");
b.append(msg);
log(b.toString());
}
public String toString() {
return "DefaultLogger";
}
}