2020-03-02 19:16:28 +01:00
|
|
|
package envoy.util;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2020-06-25 08:01:34 +02:00
|
|
|
import java.time.LocalDateTime;
|
|
|
|
import java.time.format.DateTimeFormatter;
|
2020-03-02 19:16:28 +01:00
|
|
|
import java.util.logging.*;
|
|
|
|
|
|
|
|
import envoy.data.Config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configures the {@link java.util.logging} API to output the log into the
|
2020-06-25 08:01:34 +02:00
|
|
|
* console and a log file.
|
|
|
|
* <p>
|
2020-03-02 19:16:28 +01:00
|
|
|
* Project: <strong>envoy-client</strong><br>
|
|
|
|
* File: <strong>EnvoyLogger.java</strong><br>
|
2020-08-22 13:15:42 +02:00
|
|
|
* Created: <strong>14.12.2019</strong><br>
|
2020-03-02 19:16:28 +01:00
|
|
|
*
|
|
|
|
* @author Leon Hofmeister
|
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @since Envoy Common v0.1-beta
|
|
|
|
*/
|
|
|
|
public class EnvoyLog {
|
|
|
|
|
|
|
|
private static FileHandler fileHandler;
|
|
|
|
private static StreamHandler consoleHandler;
|
|
|
|
private static boolean initialized;
|
|
|
|
|
|
|
|
private EnvoyLog() {}
|
|
|
|
|
|
|
|
/**
|
2020-08-22 13:15:42 +02:00
|
|
|
* Initializes logging.
|
2020-03-02 19:16:28 +01:00
|
|
|
*
|
|
|
|
* @param config the config providing the console and log file barriers
|
|
|
|
* @since Envoy Common v0.1-beta
|
|
|
|
*/
|
|
|
|
public static void initialize(Config config) {
|
|
|
|
if (initialized) throw new IllegalStateException("EnvoyLog is already initialized");
|
|
|
|
|
|
|
|
// Remove default console handler
|
|
|
|
LogManager.getLogManager().reset();
|
|
|
|
|
|
|
|
// Configure log file
|
2020-08-22 13:15:42 +02:00
|
|
|
final File logFile = new File(config.getHomeDirectory(),
|
|
|
|
"log/" + DateTimeFormatter.ofPattern("yyyy-MM-dd--hh-mm").format(LocalDateTime.now()) + ".log");
|
2020-03-02 19:16:28 +01:00
|
|
|
logFile.getParentFile().mkdirs();
|
|
|
|
|
|
|
|
// Configure formatting
|
2020-08-22 13:15:42 +02:00
|
|
|
// Sample log entry: [2020-06-13 16:50:26] [INFORMATION]
|
|
|
|
// [envoy.client.ui.Startup] Closing connection...
|
2020-06-25 08:01:34 +02:00
|
|
|
System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT] [%4$-7s] [%3$s] %5$s %6$s%n");
|
2020-06-13 16:57:42 +02:00
|
|
|
final SimpleFormatter formatter = new SimpleFormatter();
|
2020-03-02 19:16:28 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
fileHandler = new FileHandler(logFile.getAbsolutePath());
|
2020-08-22 13:15:42 +02:00
|
|
|
fileHandler.setLevel(config.getFileLevelBarrier());
|
2020-03-02 19:16:28 +01:00
|
|
|
fileHandler.setFormatter(formatter);
|
|
|
|
} catch (SecurityException | IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2020-06-11 12:14:20 +02:00
|
|
|
consoleHandler = new StreamHandler(System.out, formatter) {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public synchronized void publish(LogRecord record) {
|
|
|
|
super.publish(record);
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
};
|
2020-08-22 13:15:42 +02:00
|
|
|
consoleHandler.setLevel(config.getConsoleLevelBarrier());
|
2020-03-02 19:16:28 +01:00
|
|
|
consoleHandler.setFormatter(formatter);
|
|
|
|
|
|
|
|
initialized = true;
|
2020-08-22 13:15:42 +02:00
|
|
|
EnvoyLog.attach("envoy");
|
2020-03-02 19:16:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Configures all loggers that are contained within the hierarchy of a specific
|
|
|
|
* path to use the console and file handlers.
|
|
|
|
*
|
|
|
|
* @param path the path to the loggers to configure
|
|
|
|
* @since Envoy Common v0.1-beta
|
|
|
|
*/
|
2020-08-22 13:15:42 +02:00
|
|
|
private static void attach(String path) {
|
2020-03-02 19:16:28 +01:00
|
|
|
if (!initialized) throw new IllegalStateException("EnvoyLog is not initialized");
|
|
|
|
|
|
|
|
// Get root logger
|
2020-06-13 16:57:42 +02:00
|
|
|
final Logger logger = Logger.getLogger(path);
|
2020-03-02 19:16:28 +01:00
|
|
|
|
|
|
|
// Add handlers
|
|
|
|
if (fileHandler != null) logger.addHandler(fileHandler);
|
|
|
|
logger.addHandler(consoleHandler);
|
|
|
|
|
|
|
|
// Delegate logger level filtering to the handlers
|
|
|
|
logger.setLevel(Level.ALL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a logger for a specified class, which output will be displayed inside
|
|
|
|
* the console and written to the log file.
|
|
|
|
*
|
|
|
|
* @param logClass the class in which the logger is used
|
|
|
|
* @return the created logger
|
|
|
|
* @since Envoy Common v0.1-beta
|
|
|
|
*/
|
|
|
|
public static Logger getLogger(Class<?> logClass) { return Logger.getLogger(logClass.getCanonicalName()); }
|
|
|
|
}
|