Fixed logger level configuration, using logger hierarchy
Loggers are now created with a class object
This commit is contained in:
		| @@ -24,7 +24,7 @@ public class Cache<T> implements Consumer<T>, Serializable { | ||||
| 	private final Queue<T>			elements	= new LinkedList<>(); | ||||
| 	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; | ||||
|  | ||||
| 	/** | ||||
|   | ||||
| @@ -46,7 +46,7 @@ public class Client implements Closeable { | ||||
|  | ||||
| 	// Configuration and logging | ||||
| 	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 | ||||
|   | ||||
| @@ -18,7 +18,7 @@ import envoy.event.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 | ||||
|   | ||||
| @@ -13,13 +13,13 @@ import envoy.event.EventBus; | ||||
|  * Project: <strong>envoy-client</strong><br> | ||||
|  * File: <strong>ReceivedMessageProcessor.java</strong><br> | ||||
|  * Created: <strong>31.12.2019</strong><br> | ||||
|  *  | ||||
|  * | ||||
|  * @author Kai S. K. Engelbart | ||||
|  * @since Envoy v0.3-alpha | ||||
|  */ | ||||
| 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 | ||||
| 	public void accept(Message message) { | ||||
|   | ||||
| @@ -26,7 +26,7 @@ public class Receiver implements Runnable { | ||||
| 	private final InputStream					in; | ||||
| 	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}. | ||||
| @@ -51,7 +51,7 @@ public class Receiver implements Runnable { | ||||
|  | ||||
| 				try (ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(objBytes))) { | ||||
| 					Object obj = oin.readObject(); | ||||
| 					logger.info("Received object " + obj); | ||||
| 					logger.fine("Received object " + obj); | ||||
|  | ||||
| 					// Get appropriate processor | ||||
| 					@SuppressWarnings("rawtypes") | ||||
|   | ||||
| @@ -26,7 +26,7 @@ public class WriteProxy { | ||||
| 	private final Client	client; | ||||
| 	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 | ||||
|   | ||||
| @@ -72,7 +72,7 @@ public class ChatWindow extends JFrame { | ||||
| 	private final ComponentListModel<User>	contactsModel		= new ComponentListModel<>(); | ||||
| 	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 | ||||
| 	private final static int	space	= 4; | ||||
|   | ||||
| @@ -60,7 +60,7 @@ public class LoginDialog extends JDialog { | ||||
| 	private final Cache<Message>	receivedMessageCache; | ||||
|  | ||||
| 	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; | ||||
|  | ||||
| 	/** | ||||
|   | ||||
| @@ -34,7 +34,7 @@ public class Startup { | ||||
|  | ||||
| 	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 | ||||
|   | ||||
| @@ -21,7 +21,7 @@ import envoy.client.util.EnvoyLog; | ||||
|  * Project: <strong>envoy-client</strong><br> | ||||
|  * File: <strong>GeneralSettingsPanel.java</strong><br> | ||||
|  * Created: <strong>21 Dec 2019</strong><br> | ||||
|  *  | ||||
|  * | ||||
|  * @author Maximilian Käfer | ||||
|  * @since Envoy v0.3-alpha | ||||
|  */ | ||||
| @@ -30,13 +30,13 @@ public class GeneralSettingsPanel extends SettingsPanel { | ||||
| 	private Theme theme; | ||||
|  | ||||
| 	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; | ||||
|  | ||||
| 	/** | ||||
| 	 * This is the constructor for the General class. Here the user can set general | ||||
| 	 * settings for the client. | ||||
| 	 *  | ||||
| 	 * | ||||
| 	 * @param parent the {@link SettingsScreen} as a part of which this | ||||
| 	 *               {@link SettingsPanel} is displayed | ||||
| 	 * @since Envoy v0.3-alpha | ||||
|   | ||||
| @@ -47,7 +47,7 @@ public class SettingsScreen extends JDialog { | ||||
|  | ||||
| 	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. | ||||
|   | ||||
| @@ -40,7 +40,7 @@ public class ThemeCustomizationPanel extends SettingsPanel { | ||||
|  | ||||
| 	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; | ||||
|  | ||||
| 	/** | ||||
|   | ||||
| @@ -19,6 +19,7 @@ import envoy.client.data.Config; | ||||
|  */ | ||||
| public class EnvoyLog { | ||||
|  | ||||
| 	private static Logger			root; | ||||
| 	private static FileHandler		fileHandler; | ||||
| 	private static StreamHandler	consoleHandler; | ||||
|  | ||||
| @@ -26,6 +27,9 @@ public class EnvoyLog { | ||||
| 		// Remove default console handler | ||||
| 		LogManager.getLogManager().reset(); | ||||
|  | ||||
| 		// Get root logger | ||||
| 		root = Logger.getLogger("envoy"); | ||||
|  | ||||
| 		// Configure log file | ||||
| 		File logFile = new File(Config.getInstance().getHomeDirectory(), | ||||
| 				"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.setLevel(Config.getInstance().getFileLevelBarrier()); | ||||
| 			fileHandler.setFormatter(formatter); | ||||
| 			root.addHandler(fileHandler); | ||||
| 		} catch (SecurityException | IOException e) { | ||||
| 			e.printStackTrace(); | ||||
| 		} | ||||
| 		consoleHandler = new StreamHandler(System.out, formatter); | ||||
| 		consoleHandler.setLevel(Config.getInstance().getConsoleLevelBarrier()); | ||||
| 		consoleHandler.setFormatter(formatter); | ||||
| 		root.addHandler(consoleHandler); | ||||
|  | ||||
| 		root.setLevel(Level.ALL); | ||||
| 	} | ||||
|  | ||||
| 	private EnvoyLog() {} | ||||
| @@ -52,19 +60,13 @@ public class EnvoyLog { | ||||
| 	/** | ||||
| 	 * 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} | ||||
| 	 * @since Envoy v0.2-alpha | ||||
| 	 */ | ||||
| 	public static Logger getLogger(String name) { | ||||
| 		// Get a logger with the specified name | ||||
| 		Logger logger = Logger.getLogger(name); | ||||
|  | ||||
| 		// Add handlers | ||||
| 		if (fileHandler != null) logger.addHandler(fileHandler); | ||||
| 		if (consoleHandler != null) logger.addHandler(consoleHandler); | ||||
|  | ||||
| 		return logger; | ||||
| 	public static Logger getLogger(Class<?> logClass) { | ||||
| 		// Get a logger with the specified class name | ||||
| 		return Logger.getLogger(logClass.getCanonicalName()); | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| @@ -74,7 +76,10 @@ public class EnvoyLog { | ||||
| 	 *                         from 0 - 1000 or with the according name of the level | ||||
| 	 * @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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user