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.0KB

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