1 /*
2 * PCUServeur for the PCU project.
3 * Copyright (C) 2010 PLU Julien
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * For questions: julien.plu@redaction-developpez.com
19 */
20 package pcuserveur.lib;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27 import java.util.logging.FileHandler;
28 import java.util.logging.Handler;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 import java.util.logging.SimpleFormatter;
32
33 /**
34 * @author PLU Julien
35 * @version 1.0
36 *
37 * <b>
38 * Cette classe permet de faire des log contenant les erreurs
39 * et les messages généré par le serveur.
40 * </b>
41 * <p>
42 * Un MonLog est caractérisé par les informations suivantes:
43 * <ul>
44 * <li>Une DateFormat pour la gestion des fichiers.</li>
45 * <li>Un Handler pour la manipulation de fichiers.</li>
46 * <li>Un Logger pour la gestion de l'écriture.</li>
47 * </ul>
48 * </p>
49 *
50 * @see GestionnaireConnexion
51 * @see Serveur
52 * @see Canal
53 */
54 public class MonLog {
55 /**
56 * <p>
57 * Un handler pour la gestion des fichiers.
58 * </p>
59 */
60 private Handler descripteur;
61 /**
62 * <p>
63 * Une date pour connaître le jour des messages placés dans les logs.
64 * </p>
65 */
66 private final DateFormat formatDate = new SimpleDateFormat ("dd-MM-yyyy"); //$NON-NLS-1$
67 /**
68 * <p>
69 * Un logger pour l'écriture des logs dans les fichiers correspondants.
70 * </p>
71 */
72 private Logger log;
73
74 /**
75 * <p>
76 * Constructeur par défaut.
77 * </p>
78 */
79 public MonLog () {
80 // Constructeur par défaut.
81 }
82
83 /**
84 * <p>
85 * Construcuteur paramétré.
86 * </p>
87 *
88 * @param nom
89 * Nom du fichier créé.
90 */
91 public MonLog (final String nom) {
92 this.log = Logger.getLogger ("pcuserveur.lib.MonLog" + nom); //$NON-NLS-1$
93
94 this.log.setLevel (Level.ALL);
95
96 final String date = this.formatDate.format (new Date ());
97
98 try {
99 final File logFile = new File ("." + File.separator + "Logs" + File.separator + date + "." + nom + ".log"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
100
101 if (!logFile.createNewFile ()) {
102 this.descripteur = new FileHandler (logFile.getAbsolutePath (), true);
103 }
104 else {
105 this.descripteur = new FileHandler (logFile.getAbsolutePath ());
106 }
107
108 }
109 catch (final SecurityException e) {
110 Serveur.logServeur.logMessage ("Erreur de sécurité"); //$NON-NLS-1$
111 Serveur.logServeur.logException ("MonLog", "MonLog", e); //$NON-NLS-1$ //$NON-NLS-2$
112 }
113 catch (final IOException e) {
114 Serveur.logServeur.logMessage ("Erreur d'entrée sortie"); //$NON-NLS-1$
115 Serveur.logServeur.logException ("MonLog", "MonLog", e); //$NON-NLS-1$ //$NON-NLS-2$
116 }
117 this.descripteur.setFormatter (new SimpleFormatter ());
118 this.log.addHandler (this.descripteur);
119 }
120
121 /**
122 * <p>
123 * Cette méthode permet de fermer proprement le fichier de log.
124 * </p>
125 */
126 public void fermer () {
127 this.descripteur.close ();
128 }
129
130 /**
131 * <p>
132 * Cette méthode log une nouvelle exception.
133 * </p>
134 *
135 * @param classe
136 * La classe qui a générrer l'exception.
137 * @param methode
138 * La méthode dans la classe qui a générer l'exception.
139 * @param exception
140 * L'exception qui a été levée.
141 */
142 public void logException (final String classe, final String methode, final Throwable exception) {
143 this.log.throwing (classe, methode, exception);
144 }
145
146 /**
147 * <p>
148 * Cette méthode log un nouveau message.
149 * </p>
150 *
151 * @param message
152 * Le message à écrire dans le log.
153 */
154 public void logMessage (final String message) {
155 this.log.info (message);
156 }
157 }