You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoggingEventListener.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /* $Id$ */
  18. package org.apache.fop.event;
  19. import java.util.HashSet;
  20. import java.util.Set;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.fop.events.Event;
  24. import org.apache.fop.events.EventFormatter;
  25. import org.apache.fop.events.EventListener;
  26. import org.apache.fop.events.model.EventSeverity;
  27. /**
  28. * EventListener implementation that redirects events to Commons Logging. The events are
  29. * converted to localized messages.
  30. */
  31. public class LoggingEventListener implements EventListener {
  32. /** Default logger instance */
  33. private static Log defaultLog = LogFactory.getLog(LoggingEventListener.class);
  34. private Log log;
  35. private boolean skipFatal;
  36. private final Set<String> loggedMessages = new HashSet<String>();
  37. /**
  38. * Creates an instance logging to the default log category of this class.
  39. */
  40. public LoggingEventListener() {
  41. this(defaultLog);
  42. }
  43. /**
  44. * Creates an instance logging to a given logger. Events with fatal severity level will be
  45. * skipped.
  46. * @param log the target logger
  47. */
  48. public LoggingEventListener(Log log) {
  49. this(log, true);
  50. }
  51. /**
  52. * Creates an instance logging to a given logger.
  53. * @param log the target logger
  54. * @param skipFatal true if events with fatal severity level should be skipped (i.e. not logged)
  55. */
  56. public LoggingEventListener(Log log, boolean skipFatal) {
  57. this.log = log;
  58. this.skipFatal = skipFatal;
  59. }
  60. /**
  61. * Returns the target logger for this instance.
  62. * @return the target logger
  63. */
  64. public Log getLog() {
  65. return this.log;
  66. }
  67. /** {@inheritDoc} */
  68. public void processEvent(Event event) {
  69. String msg = EventFormatter.format(event);
  70. EventSeverity severity = event.getSeverity();
  71. if (severity == EventSeverity.INFO) {
  72. log.info(msg);
  73. } else if (severity == EventSeverity.WARN) {
  74. // we want to prevent logging of duplicate messages in situations where they are likely
  75. // to occur; for instance, warning related to layout do not repeat (since line number
  76. // will be different) and as such we do not try to filter them here; on the other hand,
  77. // font related warnings are very likely to repeat and we try to filter them out here;
  78. // the same may happen with missing images (but not implemented yet).
  79. String eventGroupID = event.getEventGroupID();
  80. if (eventGroupID.equals("org.apache.fop.fonts.FontEventProducer")) {
  81. if (!loggedMessages.contains(msg)) {
  82. loggedMessages.add(msg);
  83. log.warn(msg);
  84. }
  85. } else {
  86. log.warn(msg);
  87. }
  88. } else if (severity == EventSeverity.ERROR) {
  89. if (event.getParam("e") != null) {
  90. log.error(msg, (Throwable)event.getParam("e"));
  91. } else {
  92. log.error(msg);
  93. }
  94. } else if (severity == EventSeverity.FATAL) {
  95. if (!skipFatal) {
  96. if (event.getParam("e") != null) {
  97. log.fatal(msg, (Throwable)event.getParam("e"));
  98. } else {
  99. log.fatal(msg);
  100. }
  101. }
  102. } else {
  103. assert false;
  104. }
  105. }
  106. }