source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/listener/Log4jListener.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 5.4 KB
Line 
1/*
2 * Copyright 2001-2002,2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.listener;
19
20import org.apache.log4j.Category;
21import org.apache.log4j.helpers.NullEnumeration;
22import org.apache.tools.ant.BuildEvent;
23import org.apache.tools.ant.BuildListener;
24import org.apache.tools.ant.Project;
25import org.apache.tools.ant.Target;
26import org.apache.tools.ant.Task;
27
28
29/**
30 * Listener which sends events to Log4j logging system
31 *
32 */
33public class Log4jListener implements BuildListener {
34
35 /** Indicates if the listener was initialized. */
36 private boolean initialized = false;
37
38 /**
39 * Construct the listener and make sure there is a valid appender.
40 */
41 public Log4jListener() {
42 initialized = false;
43 Category cat = Category.getInstance("org.apache.tools.ant");
44 Category rootCat = Category.getRoot();
45 if (!(rootCat.getAllAppenders() instanceof NullEnumeration)) {
46 initialized = true;
47 } else {
48 cat.error("No log4j.properties in build area");
49 }
50 }
51
52 /**
53 * @see BuildListener#buildStarted
54 */
55 public void buildStarted(BuildEvent event) {
56 if (initialized) {
57 Category cat = Category.getInstance(Project.class.getName());
58 cat.info("Build started.");
59 }
60 }
61
62 /**
63 * @see BuildListener#buildFinished
64 */
65 public void buildFinished(BuildEvent event) {
66 if (initialized) {
67 Category cat = Category.getInstance(Project.class.getName());
68 if (event.getException() == null) {
69 cat.info("Build finished.");
70 } else {
71 cat.error("Build finished with error.", event.getException());
72 }
73 }
74 }
75
76 /**
77 * @see BuildListener#targetStarted
78 */
79 public void targetStarted(BuildEvent event) {
80 if (initialized) {
81 Category cat = Category.getInstance(Target.class.getName());
82 cat.info("Target \"" + event.getTarget().getName() + "\" started.");
83 }
84 }
85
86 /**
87 * @see BuildListener#targetFinished
88 */
89 public void targetFinished(BuildEvent event) {
90 if (initialized) {
91 String targetName = event.getTarget().getName();
92 Category cat = Category.getInstance(Target.class.getName());
93 if (event.getException() == null) {
94 cat.info("Target \"" + targetName + "\" finished.");
95 } else {
96 cat.error("Target \"" + targetName
97 + "\" finished with error.", event.getException());
98 }
99 }
100 }
101
102 /**
103 * @see BuildListener#taskStarted
104 */
105 public void taskStarted(BuildEvent event) {
106 if (initialized) {
107 Task task = event.getTask();
108 Category cat = Category.getInstance(task.getClass().getName());
109 cat.info("Task \"" + task.getTaskName() + "\" started.");
110 }
111 }
112
113 /**
114 * @see BuildListener#taskFinished
115 */
116 public void taskFinished(BuildEvent event) {
117 if (initialized) {
118 Task task = event.getTask();
119 Category cat = Category.getInstance(task.getClass().getName());
120 if (event.getException() == null) {
121 cat.info("Task \"" + task.getTaskName() + "\" finished.");
122 } else {
123 cat.error("Task \"" + task.getTaskName()
124 + "\" finished with error.", event.getException());
125 }
126 }
127 }
128
129 /**
130 * @see BuildListener#messageLogged
131 */
132 public void messageLogged(BuildEvent event) {
133 if (initialized) {
134 Object categoryObject = event.getTask();
135 if (categoryObject == null) {
136 categoryObject = event.getTarget();
137 if (categoryObject == null) {
138 categoryObject = event.getProject();
139 }
140 }
141
142 Category cat
143 = Category.getInstance(categoryObject.getClass().getName());
144 switch (event.getPriority()) {
145 case Project.MSG_ERR:
146 cat.error(event.getMessage());
147 break;
148 case Project.MSG_WARN:
149 cat.warn(event.getMessage());
150 break;
151 case Project.MSG_INFO:
152 cat.info(event.getMessage());
153 break;
154 case Project.MSG_VERBOSE:
155 cat.debug(event.getMessage());
156 break;
157 case Project.MSG_DEBUG:
158 cat.debug(event.getMessage());
159 break;
160 default:
161 cat.error(event.getMessage());
162 break;
163 }
164 }
165 }
166}
Note: See TracBrowser for help on using the repository browser.