1 | package jp.co.orkney.restlet.util; |
---|
2 | |
---|
3 | /** |
---|
4 | * Copyright (c) 2007 Orkney, Inc. <http://www.orkney.co.jp/> |
---|
5 | * |
---|
6 | * This program is free software: you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation, either version 3 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | */ |
---|
19 | |
---|
20 | import java.io.BufferedWriter; |
---|
21 | import java.io.FileWriter; |
---|
22 | import java.io.IOException; |
---|
23 | import java.text.DateFormat; |
---|
24 | import java.util.Calendar; |
---|
25 | import java.util.Date; |
---|
26 | import java.util.Locale; |
---|
27 | |
---|
28 | /** |
---|
29 | * <b>Log is a class which represents a log file and provides a method to write |
---|
30 | * in this file</b> |
---|
31 | * <p> |
---|
32 | * Log object is characterized by the following information: |
---|
33 | * <ul> |
---|
34 | * <li>The logs file's URL</li> |
---|
35 | * </ul> |
---|
36 | * </p> |
---|
37 | * |
---|
38 | * @author Matthieu Bilbille - Orkney Inc. |
---|
39 | * @version 1.0 |
---|
40 | */ |
---|
41 | public class Log |
---|
42 | { |
---|
43 | private String file; |
---|
44 | private int mode;// 0 default mode, 1: debug mode, 2: full mode |
---|
45 | |
---|
46 | /** |
---|
47 | * Constructor Log. |
---|
48 | * <p> |
---|
49 | * Creates a new log with the specified string URL |
---|
50 | * </p> |
---|
51 | * |
---|
52 | * @param file |
---|
53 | * logs file's URL |
---|
54 | */ |
---|
55 | public Log(String file, int mode) |
---|
56 | { |
---|
57 | this.file = file; |
---|
58 | this.mode = mode; |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * Writes the specified string in the log file |
---|
63 | * |
---|
64 | * @param text |
---|
65 | * the string that is to be write in log file |
---|
66 | */ |
---|
67 | public void write(String text, int mode) |
---|
68 | { |
---|
69 | if (this.mode >= mode) |
---|
70 | { |
---|
71 | try |
---|
72 | { |
---|
73 | FileWriter fileWriter = new FileWriter(file, true); |
---|
74 | BufferedWriter buffer = new BufferedWriter(fileWriter); |
---|
75 | Calendar calendar = Calendar.getInstance(); |
---|
76 | Date now = calendar.getTime(); |
---|
77 | String datelog = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, Locale.FRANCE).format(now); |
---|
78 | buffer.write("[" + datelog + "]: " + text); |
---|
79 | buffer.newLine(); |
---|
80 | buffer.close(); |
---|
81 | } |
---|
82 | catch (IOException e) |
---|
83 | { |
---|
84 | System.out.println(e.getMessage()); |
---|
85 | } |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * Writes the stack trace information of the exception |
---|
91 | * |
---|
92 | * @param e |
---|
93 | * exception |
---|
94 | */ |
---|
95 | public void getStackTrace(Exception e) |
---|
96 | { |
---|
97 | for (int i = 0; i < e.getStackTrace().length; i++) |
---|
98 | { |
---|
99 | this.write(e.getStackTrace()[i].toString(), 2); |
---|
100 | } |
---|
101 | } |
---|
102 | } |
---|