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 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 org.apache.commons.logging.Log;
  20. import org.apache.commons.logging.LogFactory;
  21. import org.apache.fop.events.model.EventSeverity;
  22. /**
  23. * EventListener implementation that redirects events to Commons Logging. The events are
  24. * converted to localized messages.
  25. */
  26. public class LoggingEventListener implements EventListener {
  27. /** Default logger instance */
  28. private static Log defaultLog = LogFactory.getLog(LoggingEventListener.class);
  29. private Log log;
  30. private boolean skipFatal;
  31. /**
  32. * Creates an instance logging to the default log category of this class.
  33. */
  34. public LoggingEventListener() {
  35. this(defaultLog);
  36. }
  37. /**
  38. * Creates an instance logging to a given logger. Events with fatal severity level will be
  39. * skipped.
  40. * @param log the target logger
  41. */
  42. public LoggingEventListener(Log log) {
  43. this(log, true);
  44. }
  45. /**
  46. * Creates an instance logging to a given logger.
  47. * @param log the target logger
  48. * @param skipFatal true if events with fatal severity level should be skipped (i.e. not logged)
  49. */
  50. public LoggingEventListener(Log log, boolean skipFatal) {
  51. this.log = log;
  52. this.skipFatal = skipFatal;
  53. }
  54. /**
  55. * Returns the target logger for this instance.
  56. * @return the target logger
  57. */
  58. public Log getLog() {
  59. return this.log;
  60. }
  61. /** {@inheritDoc} */
  62. public void processEvent(Event event) {
  63. String msg = EventFormatter.format(event);
  64. EventSeverity severity = event.getSeverity();
  65. if (severity == EventSeverity.INFO) {
  66. log.info(msg);
  67. } else if (severity == EventSeverity.WARN) {
  68. log.warn(msg);
  69. } else if (severity == EventSeverity.ERROR) {
  70. if (event.getParam("e") != null) {
  71. log.error(msg, (Throwable)event.getParam("e"));
  72. } else {
  73. log.error(msg);
  74. }
  75. } else if (severity == EventSeverity.FATAL) {
  76. if (!skipFatal) {
  77. if (event.getParam("e") != null) {
  78. log.fatal(msg, (Throwable)event.getParam("e"));
  79. } else {
  80. log.fatal(msg);
  81. }
  82. }
  83. } else {
  84. assert false;
  85. }
  86. }
  87. }