Fixed logger level configuration, using logger hierarchy

Loggers are now created with a class object
This commit is contained in:
Kai S. K. Engelbart 2020-02-18 07:16:35 +01:00
parent 7129e82038
commit a7e9c70cfc
13 changed files with 32 additions and 27 deletions

View File

@ -24,7 +24,7 @@ public class Cache<T> implements Consumer<T>, Serializable {
private final Queue<T> elements = new LinkedList<>(); private final Queue<T> elements = new LinkedList<>();
private transient Consumer<T> processor; private transient Consumer<T> processor;
private static final Logger logger = EnvoyLog.getLogger(Cache.class.getSimpleName()); private static final Logger logger = EnvoyLog.getLogger(Cache.class);
private static final long serialVersionUID = 7343544675545545076L; private static final long serialVersionUID = 7343544675545545076L;
/** /**

View File

@ -46,7 +46,7 @@ public class Client implements Closeable {
// Configuration and logging // Configuration and logging
private static final Config config = Config.getInstance(); private static final Config config = Config.getInstance();
private static final Logger logger = EnvoyLog.getLogger(Client.class.getSimpleName()); private static final Logger logger = EnvoyLog.getLogger(Client.class);
/** /**
* Enters the online mode by acquiring a user ID from the server. As a * Enters the online mode by acquiring a user ID from the server. As a

View File

@ -18,7 +18,7 @@ import envoy.event.MessageStatusChangeEvent;
*/ */
public class MessageStatusChangeEventProcessor implements Consumer<MessageStatusChangeEvent> { public class MessageStatusChangeEventProcessor implements Consumer<MessageStatusChangeEvent> {
private static final Logger logger = EnvoyLog.getLogger(MessageStatusChangeEventProcessor.class.getSimpleName()); private static final Logger logger = EnvoyLog.getLogger(MessageStatusChangeEventProcessor.class);
/** /**
* Dispatches a {@link MessageStatusChangeEvent} if the status is * Dispatches a {@link MessageStatusChangeEvent} if the status is

View File

@ -13,13 +13,13 @@ import envoy.event.EventBus;
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
* File: <strong>ReceivedMessageProcessor.java</strong><br> * File: <strong>ReceivedMessageProcessor.java</strong><br>
* Created: <strong>31.12.2019</strong><br> * Created: <strong>31.12.2019</strong><br>
* *
* @author Kai S. K. Engelbart * @author Kai S. K. Engelbart
* @since Envoy v0.3-alpha * @since Envoy v0.3-alpha
*/ */
public class ReceivedMessageProcessor implements Consumer<Message> { public class ReceivedMessageProcessor implements Consumer<Message> {
private static final Logger logger = EnvoyLog.getLogger(ReceivedMessageProcessor.class.getSimpleName()); private static final Logger logger = EnvoyLog.getLogger(ReceivedMessageProcessor.class);
@Override @Override
public void accept(Message message) { public void accept(Message message) {

View File

@ -26,7 +26,7 @@ public class Receiver implements Runnable {
private final InputStream in; private final InputStream in;
private final Map<Class<?>, Consumer<?>> processors = new HashMap<>(); private final Map<Class<?>, Consumer<?>> processors = new HashMap<>();
private static final Logger logger = EnvoyLog.getLogger(Receiver.class.getSimpleName()); private static final Logger logger = EnvoyLog.getLogger(Receiver.class);
/** /**
* Creates an instance of {@link Receiver}. * Creates an instance of {@link Receiver}.
@ -51,7 +51,7 @@ public class Receiver implements Runnable {
try (ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(objBytes))) { try (ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(objBytes))) {
Object obj = oin.readObject(); Object obj = oin.readObject();
logger.info("Received object " + obj); logger.fine("Received object " + obj);
// Get appropriate processor // Get appropriate processor
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")

View File

@ -26,7 +26,7 @@ public class WriteProxy {
private final Client client; private final Client client;
private final LocalDb localDb; private final LocalDb localDb;
private static final Logger logger = EnvoyLog.getLogger(WriteProxy.class.getSimpleName()); private static final Logger logger = EnvoyLog.getLogger(WriteProxy.class);
/** /**
* Initializes a write proxy using a client and a local database. The * Initializes a write proxy using a client and a local database. The

View File

@ -72,7 +72,7 @@ public class ChatWindow extends JFrame {
private final ComponentListModel<User> contactsModel = new ComponentListModel<>(); private final ComponentListModel<User> contactsModel = new ComponentListModel<>();
private final ComponentList<User> contactList = new ComponentList<>(contactRenderer); private final ComponentList<User> contactList = new ComponentList<>(contactRenderer);
private static final Logger logger = EnvoyLog.getLogger(ChatWindow.class.getSimpleName()); private static final Logger logger = EnvoyLog.getLogger(ChatWindow.class);
// GUI component spacing // GUI component spacing
private final static int space = 4; private final static int space = 4;

View File

@ -60,7 +60,7 @@ public class LoginDialog extends JDialog {
private final Cache<Message> receivedMessageCache; private final Cache<Message> receivedMessageCache;
private static final Config config = Config.getInstance(); private static final Config config = Config.getInstance();
private static final Logger logger = EnvoyLog.getLogger(LoginDialog.class.getSimpleName()); private static final Logger logger = EnvoyLog.getLogger(LoginDialog.class);
private static final long serialVersionUID = 352021600833907468L; private static final long serialVersionUID = 352021600833907468L;
/** /**

View File

@ -34,7 +34,7 @@ public class Startup {
private static ChatWindow chatWindow; private static ChatWindow chatWindow;
private static final Logger logger = EnvoyLog.getLogger(Startup.class.getSimpleName()); private static final Logger logger = EnvoyLog.getLogger(Startup.class);
/** /**
* Loads the application by first loading the configuration, then acquiring a * Loads the application by first loading the configuration, then acquiring a

View File

@ -21,7 +21,7 @@ import envoy.client.util.EnvoyLog;
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
* File: <strong>GeneralSettingsPanel.java</strong><br> * File: <strong>GeneralSettingsPanel.java</strong><br>
* Created: <strong>21 Dec 2019</strong><br> * Created: <strong>21 Dec 2019</strong><br>
* *
* @author Maximilian K&auml;fer * @author Maximilian K&auml;fer
* @since Envoy v0.3-alpha * @since Envoy v0.3-alpha
*/ */
@ -30,13 +30,13 @@ public class GeneralSettingsPanel extends SettingsPanel {
private Theme theme; private Theme theme;
private static final String[] items = { "onCloseMode", "enterToSend" }; private static final String[] items = { "onCloseMode", "enterToSend" };
private static final Logger logger = EnvoyLog.getLogger(GeneralSettingsPanel.class.getSimpleName()); private static final Logger logger = EnvoyLog.getLogger(GeneralSettingsPanel.class);
private static final long serialVersionUID = -7470848775130754239L; private static final long serialVersionUID = -7470848775130754239L;
/** /**
* This is the constructor for the General class. Here the user can set general * This is the constructor for the General class. Here the user can set general
* settings for the client. * settings for the client.
* *
* @param parent the {@link SettingsScreen} as a part of which this * @param parent the {@link SettingsScreen} as a part of which this
* {@link SettingsPanel} is displayed * {@link SettingsPanel} is displayed
* @since Envoy v0.3-alpha * @since Envoy v0.3-alpha

View File

@ -47,7 +47,7 @@ public class SettingsScreen extends JDialog {
private SettingsPanel settingsPanel; private SettingsPanel settingsPanel;
private static final Logger logger = EnvoyLog.getLogger(SettingsScreen.class.getSimpleName()); private static final Logger logger = EnvoyLog.getLogger(SettingsScreen.class);
/** /**
* Initializes the settings screen. * Initializes the settings screen.

View File

@ -40,7 +40,7 @@ public class ThemeCustomizationPanel extends SettingsPanel {
private final Insets insets = new Insets(5, 5, 5, 5); private final Insets insets = new Insets(5, 5, 5, 5);
private static final Logger logger = EnvoyLog.getLogger(ThemeCustomizationPanel.class.getSimpleName()); private static final Logger logger = EnvoyLog.getLogger(ThemeCustomizationPanel.class);
private static final long serialVersionUID = -8697897390666456624L; private static final long serialVersionUID = -8697897390666456624L;
/** /**

View File

@ -19,6 +19,7 @@ import envoy.client.data.Config;
*/ */
public class EnvoyLog { public class EnvoyLog {
private static Logger root;
private static FileHandler fileHandler; private static FileHandler fileHandler;
private static StreamHandler consoleHandler; private static StreamHandler consoleHandler;
@ -26,6 +27,9 @@ public class EnvoyLog {
// Remove default console handler // Remove default console handler
LogManager.getLogManager().reset(); LogManager.getLogManager().reset();
// Get root logger
root = Logger.getLogger("envoy");
// Configure log file // Configure log file
File logFile = new File(Config.getInstance().getHomeDirectory(), File logFile = new File(Config.getInstance().getHomeDirectory(),
"log/envoy_user_" + new SimpleDateFormat("yyyy-MM-dd--hh-mm-mm").format(new Date()) + ".log"); "log/envoy_user_" + new SimpleDateFormat("yyyy-MM-dd--hh-mm-mm").format(new Date()) + ".log");
@ -39,12 +43,16 @@ public class EnvoyLog {
fileHandler = new FileHandler(logFile.getAbsolutePath()); fileHandler = new FileHandler(logFile.getAbsolutePath());
fileHandler.setLevel(Config.getInstance().getFileLevelBarrier()); fileHandler.setLevel(Config.getInstance().getFileLevelBarrier());
fileHandler.setFormatter(formatter); fileHandler.setFormatter(formatter);
root.addHandler(fileHandler);
} catch (SecurityException | IOException e) { } catch (SecurityException | IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
consoleHandler = new StreamHandler(System.out, formatter); consoleHandler = new StreamHandler(System.out, formatter);
consoleHandler.setLevel(Config.getInstance().getConsoleLevelBarrier()); consoleHandler.setLevel(Config.getInstance().getConsoleLevelBarrier());
consoleHandler.setFormatter(formatter); consoleHandler.setFormatter(formatter);
root.addHandler(consoleHandler);
root.setLevel(Level.ALL);
} }
private EnvoyLog() {} private EnvoyLog() {}
@ -52,19 +60,13 @@ public class EnvoyLog {
/** /**
* Creates a {@link Logger} with a specified name * Creates a {@link Logger} with a specified name
* *
* @param name the name of the {@link Logger} to create * @param logClass the class in which the logger is used
* @return the created {@link Logger} * @return the created {@link Logger}
* @since Envoy v0.2-alpha * @since Envoy v0.2-alpha
*/ */
public static Logger getLogger(String name) { public static Logger getLogger(Class<?> logClass) {
// Get a logger with the specified name // Get a logger with the specified class name
Logger logger = Logger.getLogger(name); return Logger.getLogger(logClass.getCanonicalName());
// Add handlers
if (fileHandler != null) logger.addHandler(fileHandler);
if (consoleHandler != null) logger.addHandler(consoleHandler);
return logger;
} }
/** /**
@ -74,7 +76,10 @@ public class EnvoyLog {
* from 0 - 1000 or with the according name of the level * from 0 - 1000 or with the according name of the level
* @since Envoy v0.2-alpha * @since Envoy v0.2-alpha
*/ */
public static void setFileLevelBarrier(Level fileLevelBarrier) { if (fileHandler != null) fileHandler.setLevel(fileLevelBarrier); } public static void setFileLevelBarrier(Level fileLevelBarrier) {
if (fileHandler != null) fileHandler.setLevel(fileLevelBarrier);
}
/** /**
* @param consoleLevelBarrier the severity below which logRecords will not be * @param consoleLevelBarrier the severity below which logRecords will not be