KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > monolog > wrapper > ant > MonologBuildListener


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 package org.objectweb.util.monolog.wrapper.ant;
20
21 import org.apache.tools.ant.BuildEvent;
22 import org.apache.tools.ant.BuildListener;
23 import org.apache.tools.ant.Project;
24 import org.objectweb.util.monolog.Monolog;
25 import org.objectweb.util.monolog.api.BasicLevel;
26 import org.objectweb.util.monolog.api.Logger;
27 import org.objectweb.util.monolog.api.LoggerFactory;
28
29 /**
30  * This class is an ant BuildListener which logs events in monolog. This
31  * listener uses the LoggerFactory initialized by the Monolog.class.
32  * The topic of the message is concatenation (with dot separator) of the project
33  * name, the target name and the task name.
34  *
35  * @author S.Chassande-Barrioz
36  */

37 public class MonologBuildListener implements BuildListener {
38
39     /**
40      * The logger factory used for logger allocation if no logger are specified
41      */

42     private LoggerFactory loggerFactory;
43
44     /**
45      * The logger use for logging event. A null value means the logger is
46      * allocated each time. In this case the topic name depends on the project,
47      * target and task names.
48      */

49     private Logger log;
50     
51     public MonologBuildListener() {
52         this(Monolog.initialize());
53     }
54
55     public MonologBuildListener(Logger log) {
56         this.log = log;
57         if (log == null) {
58             throw new IllegalArgumentException JavaDoc("Non null Logger is required");
59         }
60     }
61
62     public MonologBuildListener(LoggerFactory loggerFactory) {
63         this.loggerFactory = loggerFactory;
64         if (loggerFactory == null) {
65             throw new IllegalArgumentException JavaDoc("Non null LoggerFactory is required");
66         }
67     }
68
69     /**
70      * Does the logging of the event
71      */

72     private void log(BuildEvent be) {
73         Logger logger;
74         if (log == null) {
75             logger = loggerFactory.getLogger(
76                 be.getProject().getName()
77                 + "." + be.getTarget().getName()
78                 + "." + be.getTask().getTaskName());
79         } else {
80             logger = log;
81         }
82         int level;
83         switch(be.getPriority()) {
84         case Project.MSG_ERR:
85             level = BasicLevel.ERROR;
86             break;
87         case Project.MSG_WARN:
88             level = BasicLevel.WARN;
89             break;
90         case Project.MSG_INFO:
91             level = BasicLevel.INFO;
92             break;
93         case Project.MSG_DEBUG:
94         case Project.MSG_VERBOSE:
95         default:
96             level = BasicLevel.DEBUG;
97             break;
98         }
99         if (be.getSource() != null) {
100             if (be.getException() != null) {
101                 logger.log(level, be.getMessage(), be.getException(), be.getSource(), be.getSource());
102             } else {
103                 logger.log(level, be.getMessage(), be.getSource(), be.getSource());
104             }
105         } else if (be.getException() != null) {
106             logger.log(level, be.getMessage(), be.getException());
107         } else {
108             logger.log(level, be.getMessage());
109         }
110     }
111     
112     // IMPLEMENTATION OF THE BuildListener INTERFACE //
113
//-----------------------------------------------//
114

115     public void buildStarted(BuildEvent be) {
116         log(be);
117     }
118     public void buildFinished(BuildEvent be) {
119         log(be);
120     }
121     public void targetStarted(BuildEvent be) {
122         log(be);
123     }
124     public void targetFinished(BuildEvent be) {
125         log(be);
126     }
127     public void taskStarted(BuildEvent be) {
128         log(be);
129     }
130     public void taskFinished(BuildEvent be) {
131         log(be);
132     }
133     public void messageLogged(BuildEvent be) {
134         log(be);
135     }
136 }
137
Popular Tags