introduction of EventInfo.
[xmlrouter] / listener / src / main / java / org / wamblee / xmlrouter / listener / LoggingEventListener.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
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 package org.wamblee.xmlrouter.listener;
17
18 import java.util.List;
19 import java.util.logging.Level;
20 import java.util.logging.Logger;
21
22 import javax.print.attribute.standard.Destination;
23
24 import org.wamblee.xmlrouter.common.Id;
25 import org.wamblee.xmlrouter.config.Transformation;
26
27 /**
28  * Event logger that simply logs to java.util logging.
29  * 
30  * @author Erik Brakkee
31  * 
32  */
33 public class LoggingEventListener implements EventListener {
34     private static final Logger LOGGER = Logger
35         .getLogger(LoggingEventListener.class.getName());
36
37     private Level level;
38
39     public LoggingEventListener(Level aLevel) {
40         level = aLevel;
41     }
42
43     @Override
44     public void delivered(EventInfo aEvent, List<Transformation> aSequence,
45         Id<Destination> aDestination, String aDestinationName,
46         boolean aSuccessFlag) {
47         if (LOGGER.isLoggable(level)) {
48             LOGGER.log(level, "event delivered: " + aEvent + ", sequence '" +
49                 getSequenceString(aSequence) + "', destionationId " +
50                 aDestination + ", destinationName '" + aDestinationName + "'");
51         }
52     }
53
54     private String getSequenceString(List<Transformation> aSequence) {
55         StringBuffer buf = new StringBuffer();
56         for (Transformation transformation : aSequence) {
57             buf.append(transformation.getName());
58             buf.append("(");
59             buf.append(transformation.getFromType());
60             buf.append("->");
61             buf.append(transformation.getToType());
62             buf.append(")");
63         }
64         return buf.toString();
65     }
66
67     @Override
68     public void notDelivered(EventInfo aInfo) {
69         if (LOGGER.isLoggable(level)) {
70             LOGGER.log(level, "event not delivered: " + aInfo);
71         }
72     }
73 }