1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one * 3 * or more contributor license agreements. See the NOTICE file * 4 * distributed with this work for additional information * 5 * regarding copyright ownership. The ASF licenses this file * 6 * to you under the Apache License, Version 2.0 (the * 7 * "License"); you may not use this file except in compliance * 8 * with the License. You may obtain a copy of the License at * 9 * * 10 * http://www.apache.org/licenses/LICENSE-2.0 * 11 * * 12 * Unless required by applicable law or agreed to in writing, * 13 * software distributed under the License is distributed on an * 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * 15 * KIND, either express or implied. See the License for the * 16 * specific language governing permissions and limitations * 17 * under the License. * 18 */ 19 package org.apache.rat.utils; 20 21 import java.io.PrintWriter; 22 import java.io.StringWriter; 23 24 /** 25 * The definition of logging for the core. UIs are expected to provide an implementation of 26 * Log to log data to the appropriate system within the UI. 27 */ 28 public interface Log { 29 /** 30 * The log levels supported by logging. 31 */ 32 public enum Level { 33 // these must be listed in order of decreasing noisiness. 34 /** 35 * Log debug only. 36 */ 37 DEBUG, 38 /** 39 * Log info only. 40 */ 41 INFO, 42 /** 43 * Log warn only. 44 */ 45 WARN, 46 /** 47 * Log error only. 48 */ 49 ERROR, 50 /** 51 * Log nothing. 52 */ 53 OFF}; 54 55 /** 56 * Writes a message at a specific log level. 57 * @param level The log level to write at. 58 * @param message the Message to write. 59 */ 60 void log(Level level, String message); 61 62 /** 63 * Write a log message at the specified level. 64 * @param level the level to write the message at. 65 * @param message the mesage to write. 66 */ 67 default void log(Level level, Object message) { 68 log(level, message == null ? "NULL" : message.toString()); 69 } 70 71 /** 72 * Write a message at DEBUG level. 73 * @param message the message to write. 74 */ 75 default void debug(Object message) { 76 log(Level.DEBUG, message); 77 } 78 79 /** 80 * Write a message at INFO level. 81 * @param message the message to write. 82 */ 83 default void info(Object message) { 84 log(Level.INFO, message); 85 } 86 87 /** 88 * Write a message at WARN level. 89 * @param message the message to write. 90 */ 91 default void warn(Object message) { 92 log(Level.WARN, message); 93 } 94 95 /** 96 * Write a message at ERROR level. 97 * @param message the message to write. 98 */ 99 default void error(Object message) { 100 log(Level.ERROR, message); 101 } 102 103 /** 104 * Write a log message and report throwable stack trace at the specified log level. 105 * @param level the level to report at 106 * @param message the message for the log 107 * @param throwable the throwable 108 */ 109 default void log(Level level, String message, Throwable throwable) { 110 StringWriter writer = new StringWriter(500); 111 PrintWriter pWriter = new PrintWriter(writer); 112 pWriter.print(message); 113 pWriter.print(System.lineSeparator()); 114 throwable.printStackTrace(pWriter); 115 log(level, writer.toString()); 116 } 117 118 /** 119 * Write a log message and report throwable stack trace at the specified log level. 120 * @param level the level to report at 121 * @param message the message for the log 122 * @param throwable the throwable 123 */ 124 default void log(Level level, Object message, Throwable throwable){ 125 log(level, message == null ? "NULL" : message.toString(), throwable); 126 } 127 128 /** 129 * Write a debug message and report throwable stack trace. 130 * @param message the message for the log 131 * @param throwable the throwable 132 */ 133 default void debug(Object message, Throwable throwable) { 134 log(Level.DEBUG, message, throwable); 135 } 136 137 /** 138 * Write an info message and report throwable stack trace. 139 * @param message the message for the log 140 * @param throwable the throwable 141 */ 142 default void info(Object message, Throwable throwable) { 143 log(Level.INFO, message, throwable); 144 } 145 146 /** 147 * Write a warn message and report throwable stack trace. 148 * @param message the message for the log 149 * @param throwable the throwable 150 */ 151 default void warn(Object message, Throwable throwable) { 152 log(Level.WARN, message, throwable); 153 } 154 155 /** 156 * Write an error message and report throwable stack trace. 157 * @param message the message for the log 158 * @param throwable the throwable 159 */ 160 default void error(Object message, Throwable throwable) { 161 log(Level.ERROR, message, throwable); 162 } 163 }