Minor improvements to Logger output and removed Services class

This commit is contained in:
Rohit Awate 2018-08-25 00:26:25 +05:30
parent fcdfed793e
commit 4b1ba0c80a
No known key found for this signature in database
GPG key ID: 1051D7B79CF2EE25
5 changed files with 25 additions and 54 deletions

View file

@ -16,9 +16,9 @@
package com.rohitawate.everest.controllers.search; package com.rohitawate.everest.controllers.search;
import com.google.common.util.concurrent.MoreExecutors;
import com.jfoenix.controls.JFXButton; import com.jfoenix.controls.JFXButton;
import com.rohitawate.everest.logging.LoggingService; import com.rohitawate.everest.logging.LoggingService;
import com.rohitawate.everest.misc.Services;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.concurrent.Task; import javafx.concurrent.Task;
import javafx.fxml.FXML; import javafx.fxml.FXML;
@ -36,6 +36,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
public abstract class SearchablePaneController<T> implements Initializable { public abstract class SearchablePaneController<T> implements Initializable {
@FXML @FXML
@ -125,7 +126,7 @@ public abstract class SearchablePaneController<T> implements Initializable {
entryLoader.setOnFailed(e -> LoggingService.logWarning("Failed to load history.", entryLoader.setOnFailed(e -> LoggingService.logWarning("Failed to load history.",
(Exception) entryLoader.getException(), LocalDateTime.now())); (Exception) entryLoader.getException(), LocalDateTime.now()));
Services.singleExecutor.execute(entryLoader); MoreExecutors.directExecutor().execute(entryLoader);
} }
private void addSearchItem(T state) { private void addSearchItem(T state) {

View file

@ -36,14 +36,18 @@ class Logger {
/** /**
* Appends the log to the respective day's log file. * Appends the log to the respective day's log file.
* *
* @param log - The log to be written to file.
*/ */
synchronized void log(Log log) { synchronized void log() {
System.out.println(log.message); if (LoggingService.log.level.equals(Level.INFO)) {
if (log.level.greaterThanEqualTo(this.writerLevel)) { System.out.println(LoggingService.log.level + " " + LoggingService.log.time + ": " + LoggingService.log.message);
} else {
System.err.println(LoggingService.log.level + " " + LoggingService.log.time + ": " + LoggingService.log.message);
}
if (LoggingService.log.level.greaterThanEqualTo(this.writerLevel)) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(logFilePath, true))) { try (BufferedWriter writer = new BufferedWriter(new FileWriter(logFilePath, true))) {
writer.flush(); writer.flush();
writer.append(getLogEntry(log)); writer.append(getLogEntry(LoggingService.log));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -16,35 +16,31 @@
package com.rohitawate.everest.logging; package com.rohitawate.everest.logging;
import com.rohitawate.everest.misc.Services; import com.google.common.util.concurrent.MoreExecutors;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.concurrent.Executor;
public class LoggingService { public class LoggingService {
private static final Logger logger; private static Executor executor = MoreExecutors.directExecutor();
private static final DateTimeFormatter dateFormat; private static final Logger logger = new Logger(Level.INFO);
private static final Log log; private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
static final Log log = new Log();
static {
log = new Log();
logger = new Logger(Level.INFO);
dateFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
}
public static void logSevere(String message, Exception exception, LocalDateTime time) { public static void logSevere(String message, Exception exception, LocalDateTime time) {
setValues(message, exception, time); setValues(message, exception, time);
Services.singleExecutor.execute(severeLogger); executor.execute(severeLogger);
} }
public static void logWarning(String message, Exception exception, LocalDateTime time) { public static void logWarning(String message, Exception exception, LocalDateTime time) {
setValues(message, exception, time); setValues(message, exception, time);
Services.singleExecutor.execute(warningLogger); executor.execute(warningLogger);
} }
public static void logInfo(String message, LocalDateTime time) { public static void logInfo(String message, LocalDateTime time) {
setValues(message, null, time); setValues(message, null, time);
Services.singleExecutor.execute(infoLogger); executor.execute(infoLogger);
} }
private static void setValues(String message, Exception exception, LocalDateTime time) { private static void setValues(String message, Exception exception, LocalDateTime time) {
@ -55,16 +51,16 @@ public class LoggingService {
private static Runnable severeLogger = () -> { private static Runnable severeLogger = () -> {
log.level = Level.SEVERE; log.level = Level.SEVERE;
logger.log(log); logger.log();
}; };
private static Runnable warningLogger = () -> { private static Runnable warningLogger = () -> {
log.level = Level.WARNING; log.level = Level.WARNING;
logger.log(log); logger.log();
}; };
private static Runnable infoLogger = () -> { private static Runnable infoLogger = () -> {
log.level = Level.INFO; log.level = Level.INFO;
logger.log(log); logger.log();
}; };
} }

View file

@ -1,29 +0,0 @@
/*
* Copyright 2018 Rohit Awate.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rohitawate.everest.misc;
import com.google.common.util.concurrent.MoreExecutors;
import java.util.concurrent.Executor;
public class Services {
public static Executor singleExecutor;
static {
singleExecutor = MoreExecutors.directExecutor();
}
}

View file

@ -67,10 +67,9 @@ class SQLiteManager implements DataManager {
conn = DriverManager.getConnection("jdbc:sqlite:Everest/config/history.sqlite"); conn = DriverManager.getConnection("jdbc:sqlite:Everest/config/history.sqlite");
createDatabase(); createDatabase();
LoggingService.logInfo("Connected to database", LocalDateTime.now());
} catch (Exception E) { } catch (Exception E) {
LoggingService.logSevere("Exception while initializing DataManager.", E, LocalDateTime.now()); LoggingService.logSevere("Exception while initializing DataManager.", E, LocalDateTime.now());
} finally {
System.out.println("Connected to database.");
} }
} }