first version of the logging interface with some basic implementations.
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / LoggingEventLogger.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.impl;
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 import javax.xml.transform.dom.DOMSource;
24
25 import org.wamblee.xml.XMLDocument;
26 import org.wamblee.xmlrouter.common.Id;
27 import org.wamblee.xmlrouter.config.Transformation;
28 import org.wamblee.xmlrouter.logging.EventLogger;
29
30 /**
31  * Event logger that simply logs to java.util logging.
32  * 
33  * @author Erik Brakkee
34  * 
35  */
36 public class LoggingEventLogger implements EventLogger {
37     private static final Logger LOGGER = Logger
38         .getLogger(LoggingEventLogger.class.getName());
39
40     private Level level;
41
42     public LoggingEventLogger(Level aLevel) {
43         level = aLevel;
44     }
45
46     @Override
47     public void delivered(String aDocType, Id<DOMSource> aEventId,
48         DOMSource aEvent, List<Transformation> aSequence,
49         Id<Destination> aDestination, String aDestinationName,
50         boolean aSuccessFlag) {
51         if (LOGGER.isLoggable(level)) {
52             LOGGER.log(level, "event delivered: document type '" + aDocType +
53                 "', eventId " + aEventId + ", event '" +
54                 new XMLDocument(aEvent).print(true) + "', sequence '" +
55                 getSequenceString(aSequence) + "', destionationId " +
56                 aDestination + ", destinationName '" + aDestinationName + "'");
57         }
58     }
59
60     private String getSequenceString(List<Transformation> aSequence) {
61         StringBuffer buf = new StringBuffer();
62         for (Transformation transformation : aSequence) {
63             buf.append(transformation.getName());
64             buf.append("(");
65             buf.append(transformation.getFromType());
66             buf.append("->");
67             buf.append(transformation.getToType());
68             buf.append(")");
69         }
70         return buf.toString();
71     }
72
73     @Override
74     public void notDelivered(String aDocumentType, Id<DOMSource> aEventId,
75         DOMSource aEvent) {
76         if (LOGGER.isLoggable(level)) {
77             LOGGER.log(level, "event not delivered: document type '" +
78                 aDocumentType + "', eventId " + aEventId + ", event '" +
79                 new XMLDocument(aEvent).print(true) + "'");
80         }
81     }
82 }