2022-07-20 13:18:57 +02:00
|
|
|
package fr.pandacube.lib.util;
|
2016-07-12 19:26:49 +02:00
|
|
|
|
2016-07-14 01:46:44 +02:00
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
2016-07-12 19:26:49 +02:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Utility class to easily log info into a provided logger. This class avoid the needs to fetch the logger everytime it
|
|
|
|
* is needed.
|
|
|
|
*
|
|
|
|
* For instance, this piece of code:
|
|
|
|
* <pre>
|
|
|
|
* getTheLoggerFromSomewhere().info(message);
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* Can be simplified by one line to put in the startup code of the application:
|
|
|
|
* <pre>
|
|
|
|
* Log.setLogger(getTheLoggerFromSomewhere());
|
|
|
|
* </pre>
|
|
|
|
* And this code everywhere the application needs to log something:
|
|
|
|
* <pre>
|
|
|
|
* Log.info(message);
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* This the {@link #setLogger(Logger)} method is not called, thi class will use the logger returned by
|
|
|
|
* {@link Logger#getGlobal()}.
|
|
|
|
*/
|
2022-07-20 13:18:57 +02:00
|
|
|
public final class Log {
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2022-02-07 12:39:48 +01:00
|
|
|
private static Logger logger = Logger.getGlobal();
|
2022-07-10 00:55:56 +02:00
|
|
|
private static final AtomicBoolean logDebug = new AtomicBoolean(false);
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Determine if {@link #debug(Throwable)}, {@link #debug(String)} and {@link #debug(String, Throwable)} will actually
|
|
|
|
* log a message or not.
|
|
|
|
* @param debug true to enable debug, false otherwise
|
|
|
|
*/
|
|
|
|
public static void setDebug(boolean debug) {
|
|
|
|
logDebug.set(debug);
|
2016-07-14 01:46:44 +02:00
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Tells if the debug mode is enabled or not.
|
|
|
|
* @return true if debug is enabled, false otherwise.
|
|
|
|
*/
|
2021-07-25 20:14:06 +02:00
|
|
|
public static boolean isDebugEnabled() {
|
2016-07-14 01:46:44 +02:00
|
|
|
return logDebug.get();
|
|
|
|
}
|
2016-07-12 19:26:49 +02:00
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Get the backend logger of this class.
|
|
|
|
* @return the logger.
|
|
|
|
*/
|
2016-07-12 19:26:49 +02:00
|
|
|
public static Logger getLogger() {
|
|
|
|
return logger;
|
|
|
|
}
|
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Set the backend logger of this class.
|
|
|
|
* @param logger the logger to use.
|
|
|
|
*/
|
|
|
|
public static void setLogger(Logger logger) {
|
|
|
|
Log.logger = logger;
|
2016-07-12 19:26:49 +02:00
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Log the provided message using logger level {@link Level#INFO}.
|
|
|
|
* @param message the message to log
|
|
|
|
* @see Logger#info(String)
|
|
|
|
*/
|
2016-07-12 19:26:49 +02:00
|
|
|
public static void info(String message) {
|
|
|
|
logger.info(message);
|
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Log the provided message and throwable using logger level {@link Level#WARNING}.
|
|
|
|
* @param message the message to log
|
|
|
|
* @param throwable the throwable to log
|
|
|
|
*/
|
|
|
|
public static void warning(String message, Throwable throwable) {
|
|
|
|
logger.log(Level.WARNING, message, throwable);
|
2016-07-12 19:26:49 +02:00
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Log the provided throwable using logger level {@link Level#WARNING}.
|
|
|
|
* @param throwable the throwable to log
|
|
|
|
*/
|
|
|
|
public static void warning(Throwable throwable) {
|
|
|
|
logger.log(Level.WARNING, "", throwable);
|
2016-07-12 19:26:49 +02:00
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Log the provided message using logger level {@link Level#WARNING}.
|
|
|
|
* @param message the message to log
|
|
|
|
* @see Logger#warning(String)
|
|
|
|
*/
|
2016-07-12 19:26:49 +02:00
|
|
|
public static void warning(String message) {
|
|
|
|
logger.warning(message);
|
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Log the provided message and throwable using logger level {@link Level#SEVERE}.
|
|
|
|
* @param message the message to log
|
|
|
|
* @param throwable the throwable to log
|
|
|
|
*/
|
|
|
|
public static void severe(String message, Throwable throwable) {
|
|
|
|
logger.log(Level.SEVERE, message, throwable);
|
2016-07-12 19:26:49 +02:00
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Log the provided throwable using logger level {@link Level#SEVERE}.
|
|
|
|
* @param throwable the throwable to log
|
|
|
|
*/
|
|
|
|
public static void severe(Throwable throwable) {
|
|
|
|
logger.log(Level.SEVERE, "", throwable);
|
2016-07-12 19:26:49 +02:00
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Log the provided message using logger level {@link Level#SEVERE}.
|
|
|
|
* @param message the message to log
|
|
|
|
* @see Logger#severe(String)
|
|
|
|
*/
|
2016-07-12 19:26:49 +02:00
|
|
|
public static void severe(String message) {
|
|
|
|
logger.severe(message);
|
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Log the provided message and throwable using logger level {@link Level#INFO}, if the debug mode is enabled.
|
|
|
|
* @param message the message to log
|
|
|
|
* @param throwable the throwable to log
|
|
|
|
* @see #isDebugEnabled()
|
|
|
|
* @see #setDebug(boolean)
|
|
|
|
*/
|
|
|
|
public static void debug(String message, Throwable throwable) {
|
2016-07-14 01:46:44 +02:00
|
|
|
if (!logDebug.get()) return;
|
2022-07-28 01:13:35 +02:00
|
|
|
logger.log(Level.INFO, message, throwable);
|
2016-07-14 01:46:44 +02:00
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Log the provided throwable using logger level {@link Level#INFO}, if the debug mode is enabled.
|
|
|
|
* @param throwable the throwable to log
|
|
|
|
* @see #isDebugEnabled()
|
|
|
|
* @see #setDebug(boolean)
|
|
|
|
*/
|
|
|
|
public static void debug(Throwable throwable) {
|
2016-07-14 01:46:44 +02:00
|
|
|
if (!logDebug.get()) return;
|
2022-07-28 01:13:35 +02:00
|
|
|
logger.log(Level.INFO, "", throwable);
|
2016-07-14 01:46:44 +02:00
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2022-07-28 01:13:35 +02:00
|
|
|
/**
|
|
|
|
* Log the provided message using logger level {@link Level#INFO}, if the debug mode is enabled.
|
|
|
|
* @param message the message to log
|
|
|
|
* @see #isDebugEnabled()
|
|
|
|
* @see #setDebug(boolean)
|
|
|
|
* @see Logger#info(String)
|
|
|
|
*/
|
2016-07-14 01:46:44 +02:00
|
|
|
public static void debug(String message) {
|
|
|
|
if (!logDebug.get()) return;
|
|
|
|
logger.info(message);
|
|
|
|
}
|
2016-07-12 19:26:49 +02:00
|
|
|
|
|
|
|
}
|