This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/common/src/main/java/envoy/util/EnvoyLog.java

127 lines
4.0 KiB
Java

package envoy.util;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.logging.*;
import envoy.data.Config;
/**
* Configures the {@link java.util.logging} API to output the log into the
* console and a log file.
* <p>
* Call the {@link EnvoyLog#attach(String)} method to configure a part of the
* logger hierarchy.
* <p>
* Project: <strong>envoy-client</strong><br>
* File: <strong>EnvoyLogger.java</strong><br>
* Created: <strong>14 Dec 2019</strong><br>
*
* @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() {}
/**
* Initializes logging. Call this method before calling the
* {@link EnvoyLog#attach(String)} method.
*
* @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
final File logFile = new File((File) config.get("homeDirectory").get(),
"log/envoy_user_" + DateTimeFormatter.ofPattern("yyyy-MM-dd--hh-mm-mm").format(LocalDateTime.now()) + ".log");
logFile.getParentFile().mkdirs();
// Configure formatting
// Sample log entry: [2020-06-13 16:50:26] [INFORMATION] [envoy.client.ui.Startup] Closing connection...
System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT] [%4$-7s] [%3$s] %5$s %6$s%n");
final SimpleFormatter formatter = new SimpleFormatter();
try {
fileHandler = new FileHandler(logFile.getAbsolutePath());
fileHandler.setLevel((Level) config.get("fileLevelBarrier").get());
fileHandler.setFormatter(formatter);
} catch (SecurityException | IOException e) {
e.printStackTrace();
}
consoleHandler = new StreamHandler(System.out, formatter) {
@Override
public synchronized void publish(LogRecord record) {
super.publish(record);
flush();
}
};
consoleHandler.setLevel((Level) config.get("consoleLevelBarrier").get());
consoleHandler.setFormatter(formatter);
initialized = true;
}
/**
* 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
*/
public static void attach(String path) {
if (!initialized) throw new IllegalStateException("EnvoyLog is not initialized");
// Get root logger
final Logger logger = Logger.getLogger(path);
// 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()); }
/**
* Defines the logger level required for a record to be written to the log file.
*
* @param fileLevelBarrier the log file level
* @since Envoy Common v0.1-beta
*/
public static void setFileLevelBarrier(Level fileLevelBarrier) { if (fileHandler != null) fileHandler.setLevel(fileLevelBarrier); }
/**
* Defines the logger level required for a record to be written to the console.
*
* @param consoleLevelBarrier the console logger level
* @since Envoy Common v0.1-beta
*/
public static void setConsoleLevelBarrier(Level consoleLevelBarrier) {
if (consoleHandler != null) consoleHandler.setLevel(consoleLevelBarrier);
}
}