KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > listener > Log4jListener


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.listener;
20
21 import org.apache.log4j.Logger;
22 import org.apache.log4j.helpers.NullEnumeration;
23 import org.apache.tools.ant.BuildEvent;
24 import org.apache.tools.ant.BuildListener;
25 import org.apache.tools.ant.Project;
26 import org.apache.tools.ant.Target;
27 import org.apache.tools.ant.Task;
28
29
30 /**
31  * Listener which sends events to Log4j logging system
32  *
33  */

34 public class Log4jListener implements BuildListener {
35
36     /** Indicates if the listener was initialized. */
37     private boolean initialized = false;
38
39     /**
40      * log category we log into
41      */

42     public static final String JavaDoc LOG_ANT = "org.apache.tools.ant";
43
44     /**
45      * Construct the listener and make sure there is a valid appender.
46      */

47     public Log4jListener() {
48         initialized = false;
49         Logger log = Logger.getLogger(LOG_ANT);
50         Logger rootLog = Logger.getRootLogger();
51         if (!(rootLog.getAllAppenders() instanceof NullEnumeration)) {
52             initialized = true;
53         } else {
54             log.error("No log4j.properties in build area");
55         }
56     }
57
58     /**
59      * @see BuildListener#buildStarted
60      */

61     /** {@inheritDoc}. */
62     public void buildStarted(BuildEvent event) {
63         if (initialized) {
64             Logger log = Logger.getLogger(Project.class.getName());
65             log.info("Build started.");
66         }
67     }
68
69     /**
70      * @see BuildListener#buildFinished
71      */

72     /** {@inheritDoc}. */
73     public void buildFinished(BuildEvent event) {
74         if (initialized) {
75             Logger log = Logger.getLogger(Project.class.getName());
76             if (event.getException() == null) {
77                 log.info("Build finished.");
78             } else {
79                 log.error("Build finished with error.", event.getException());
80             }
81         }
82     }
83
84     /**
85      * @see BuildListener#targetStarted
86      */

87     /** {@inheritDoc}. */
88     public void targetStarted(BuildEvent event) {
89         if (initialized) {
90             Logger log = Logger.getLogger(Target.class.getName());
91             log.info("Target \"" + event.getTarget().getName() + "\" started.");
92         }
93     }
94
95     /**
96      * @see BuildListener#targetFinished
97      */

98     /** {@inheritDoc}. */
99     public void targetFinished(BuildEvent event) {
100         if (initialized) {
101             String JavaDoc targetName = event.getTarget().getName();
102             Logger cat = Logger.getLogger(Target.class.getName());
103             if (event.getException() == null) {
104                 cat.info("Target \"" + targetName + "\" finished.");
105             } else {
106                 cat.error("Target \"" + targetName
107                     + "\" finished with error.", event.getException());
108             }
109         }
110     }
111
112     /**
113      * @see BuildListener#taskStarted
114      */

115     /** {@inheritDoc}. */
116     public void taskStarted(BuildEvent event) {
117         if (initialized) {
118             Task task = event.getTask();
119             Logger log = Logger.getLogger(task.getClass().getName());
120             log.info("Task \"" + task.getTaskName() + "\" started.");
121         }
122     }
123
124     /**
125      * @see BuildListener#taskFinished
126      */

127     /** {@inheritDoc}. */
128     public void taskFinished(BuildEvent event) {
129         if (initialized) {
130             Task task = event.getTask();
131             Logger log = Logger.getLogger(task.getClass().getName());
132             if (event.getException() == null) {
133                 log.info("Task \"" + task.getTaskName() + "\" finished.");
134             } else {
135                 log.error("Task \"" + task.getTaskName()
136                     + "\" finished with error.", event.getException());
137             }
138         }
139     }
140
141     /**
142      * @see BuildListener#messageLogged
143      */

144     /** {@inheritDoc}. */
145     public void messageLogged(BuildEvent event) {
146         if (initialized) {
147             Object JavaDoc categoryObject = event.getTask();
148             if (categoryObject == null) {
149                 categoryObject = event.getTarget();
150                 if (categoryObject == null) {
151                     categoryObject = event.getProject();
152                 }
153             }
154
155             Logger log
156                 = Logger.getLogger(categoryObject.getClass().getName());
157             switch (event.getPriority()) {
158                 case Project.MSG_ERR:
159                     log.error(event.getMessage());
160                     break;
161                 case Project.MSG_WARN:
162                     log.warn(event.getMessage());
163                     break;
164                 case Project.MSG_INFO:
165                     log.info(event.getMessage());
166                     break;
167                 case Project.MSG_VERBOSE:
168                     log.debug(event.getMessage());
169                     break;
170                 case Project.MSG_DEBUG:
171                     log.debug(event.getMessage());
172                     break;
173                 default:
174                     log.error(event.getMessage());
175                     break;
176             }
177         }
178     }
179 }
180
Popular Tags