KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Simple


1 /**
2  * Copyright (C) 2001-2003 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19
20 import org.objectweb.util.monolog.Monolog;
21 import org.objectweb.util.monolog.api.BasicLevel;
22 import org.objectweb.util.monolog.api.Logger;
23 import org.objectweb.util.monolog.api.LoggerFactory;
24
25
26 /**
27  * This example showing how to use monolog (initialisation and instrumentation).
28  *
29  * @author Sebastien Chassande-Barrioz
30  */

31 public class Simple {
32     public static void main(String[] args) {
33         LoggerFactory lf;
34         switch(args.length) {
35         case 0:
36             //Let monolog find the monolog.properties in the classpath or use
37
// the default configuration
38
lf = Monolog.initialize();
39             break;
40         case 1:
41             // A monolog configuration file has been specified, then use it
42
lf = Monolog.getMonologFactory(args[0]);
43             break;
44         default:
45             System.out.println("Syntax error!\nUsage: java Simple [<monolog file name>]");
46             return;
47         }
48         Simple s = new Simple(lf);
49         s.foo();
50         s.bar();
51     }
52
53     private static final boolean DEBUG = false;
54
55     protected Logger logger = null;
56
57     public Simple(LoggerFactory lf) {
58         logger = lf.getLogger("monolog.examples.Simple");
59     }
60
61     public void foo() {
62         if (logger.isLoggable(BasicLevel.DEBUG)) {
63             logger.log(BasicLevel.DEBUG,
64                 "my logger has been configured in order to log debug message");
65         }
66         logger.log(BasicLevel.INFO, "foo : hello my favourite logger in info");
67
68         if (DEBUG && logger.isLoggable(BasicLevel.DEBUG)) {
69             //This code is removed at compilation time because the DEBUG final
70
// static field is equal to false.
71
logger.log(BasicLevel.DEBUG, "This message should not appears");
72         }
73     }
74
75     public void bar() {
76         logger.log(BasicLevel.WARN, "bar : warning !");
77         logger.log(BasicLevel.ERROR, "This is a throwed exception",
78             new Throwable().fillInStackTrace());
79     }
80 }
81
Popular Tags