Skip to content

Commit

Permalink
Updated logger
Browse files Browse the repository at this point in the history
  • Loading branch information
jegors-cemisovs committed Aug 4, 2021
1 parent 8fc4be0 commit f1633ba
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 28 deletions.
16 changes: 16 additions & 0 deletions .idea/runConfigurations/Logging.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Guess the Animal/task/src/animals/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import animals.repository.StorageService;
import animals.userinterface.Application;

import java.util.logging.Logger;
import static java.lang.System.Logger.Level.TRACE;

public final class Main {
private static final Logger log = Logger.getLogger(Main.class.getName());
private static final System.Logger LOGGER = System.getLogger("");

public static void main(String[] args) {
log.entering(Main.class.getName(), "main", args);
LOGGER.log(TRACE, args);

final var isTypeSpecified = args.length > 1 && args[0].equals("-type");
final var storageService = isTypeSpecified
Expand All @@ -18,6 +18,6 @@ public static void main(String[] args) {

new Application(storageService).run();

log.exiting(Main.class.getName(), "main");
LOGGER.log(TRACE, "finished");
}
}
12 changes: 6 additions & 6 deletions Guess the Animal/task/src/animals/repository/KnowledgeTree.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package animals.repository;

import java.util.logging.Logger;
import static java.lang.System.Logger.Level.TRACE;

public class KnowledgeTree {
private static final Logger log = Logger.getLogger(KnowledgeTree.class.getName());
private static final System.Logger LOGGER = System.getLogger("");
private TreeNode<String> root;
private TreeNode<String> current;

Expand Down Expand Up @@ -41,23 +41,23 @@ public boolean isEmpty() {
}

public void addAnimal(final String animal, final String statement, final boolean isRight) {
log.entering(KnowledgeTree.class.getName(), "addAnimal", new Object[]{animal, statement, isRight});
LOGGER.log(TRACE, "entering {0}, {1}, {2}", animal, statement, isRight);

final var newAnimal = new TreeNode<>(animal);
final var oldAnimal = new TreeNode<>(current.getData());
current.setData(statement);
current.setRight(isRight ? newAnimal : oldAnimal);
current.setLeft(isRight ? oldAnimal : newAnimal);

log.exiting(KnowledgeTree.class.getName(), "addAnimal", animal);
LOGGER.log(TRACE, "exiting {0}", animal);
}

public boolean deleteAnimal(final String animal) {
log.entering(KnowledgeTree.class.getName(), "deleteAnimal", animal);
LOGGER.log(TRACE, "entering, animal: {0}", animal);

final var isSuccessful = deleteAnimal(animal, root, null);

log.exiting(KnowledgeTree.class.getName(), "deleteAnimal", isSuccessful);
LOGGER.log(TRACE, "exiting, is successful: {0}", isSuccessful);
return isSuccessful;
}

Expand Down
22 changes: 12 additions & 10 deletions Guess the Animal/task/src/animals/repository/StorageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Logger;

import static java.lang.System.Logger.Level.*;

public enum StorageService {
JSON(new JsonMapper()),
YAML(new YAMLMapper()),
XML(new XmlMapper());

private static final Logger log = Logger.getLogger(StorageService.class.getName());
private static final System.Logger LOGGER = System.getLogger("");

private static final String CONFIG_FILE = "application.xml";
private static final String DEFAULT_NAME = "animals";
Expand All @@ -29,11 +30,11 @@ public enum StorageService {
try {
properties.loadFromXML(new FileInputStream(CONFIG_FILE));
} catch (IOException e) {
log.warning(e.getMessage());
LOGGER.log(WARNING, e::getMessage);
}
baseName = properties.getProperty("baseName", DEFAULT_NAME);
defaultService = of(properties.getProperty("type", DEFAULT_TYPE));
log.config(() -> "Storage file base name is " + baseName);
LOGGER.log(DEBUG, "Storage base name is `{0}`", baseName);
}

private final ObjectMapper objectMapper;
Expand All @@ -58,23 +59,24 @@ private File getFile() {

public void load(final KnowledgeTree tree) {
final var file = getFile();
log.entering(StorageService.class.getName(), "load", file.getAbsolutePath());
LOGGER.log(TRACE, file::getAbsolutePath);

try {
tree.setRoot(objectMapper.readValue(file, TreeNode.class));
} catch (IOException error) {
log.warning(error.getMessage());
LOGGER.log(WARNING, error::getMessage);
}
log.exiting(StorageService.class.getName(), "load", !tree.isEmpty());
LOGGER.log(TRACE, "is loaded: {0}", !tree.isEmpty());
}

public void save(final KnowledgeTree tree) {
final var file = getFile();
log.entering(StorageService.class.getName(), "save", file.getAbsolutePath());
LOGGER.log(TRACE, file::getAbsolutePath);
try {
objectMapper.writerWithDefaultPrettyPrinter().writeValue(file, tree.getRoot());
} catch (IOException error) {
log.warning(error.getMessage());
LOGGER.log(WARNING, error::getMessage);
}
log.exiting(StorageService.class.getName(), "save");
LOGGER.log(TRACE, "saved");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import animals.repository.StorageService;
import animals.repository.TreeNode;

import static java.lang.System.Logger.Level.TRACE;

public final class Application extends TextInterface implements Runnable {
private final KnowledgeTree knowledgeTree;
private final StorageService storageService;
Expand All @@ -17,7 +19,7 @@ public Application(final StorageService storageService) {

@Override
public void run() {
log.entering(Application.class.getName(), "run");
LOGGER.log(TRACE, "Application started");

printConditional("greeting");
storageService.load(knowledgeTree);
Expand All @@ -26,7 +28,7 @@ public void run() {
println();
println("animal.wantLearn");
println("animal.askFavorite");
knowledgeTree.setRoot(new TreeNode(ask("animal")));
knowledgeTree.setRoot(new TreeNode<>(ask("animal")));
}
println("welcome");

Expand All @@ -42,7 +44,7 @@ public void run() {

storageService.save(knowledgeTree);
println("farewell");
log.exiting(Application.class.getName(), "run");
LOGGER.log(TRACE, "Application stopped");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import static java.util.Objects.isNull;

public class TextInterface extends LanguageRules {
protected static final Logger log = Logger.getLogger(TextInterface.class.getName());
protected static final System.Logger LOGGER = System.getLogger("");

private static final Pattern MESSAGE_DELIMITER = Pattern.compile("\\f");
private static final Scanner scanner = new Scanner(System.in);
Expand Down Expand Up @@ -93,7 +92,7 @@ public String readToLowerCase() {
}

public void printConditional(final String messageName) {
log.entering(TextInterface.class.getName(), "printConditional: " + messageName);
LOGGER.log(System.Logger.Level.TRACE, "printConditional: {0}", messageName);
final var messages = new ArrayList<String>();
final var time = LocalTime.now();
final var date = LocalDate.now();
Expand Down Expand Up @@ -126,8 +125,7 @@ public void printConditional(final String messageName) {
messages.addAll(splitMessage.apply(messageName));
}

println(pickMessage(messages.toArray(String[]::new)));
log.exiting(TextInterface.class.getName(), messageName, messages);
LOGGER.log(System.Logger.Level.TRACE, "exiting printConditional: {0}, {1}", messageName, messages);
}

}
10 changes: 10 additions & 0 deletions logging.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -Djava.util.logging.config.file=logging.properties

handlers=java.util.logging.FileHandler,java.util.logging.ConsoleHandler
java.util.logging.FileHandler.level=FINEST
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.encoding=UTF-8
java.util.logging.FileHandler.limit=10000000
java.util.logging.FileHandler.pattern=application_%u.log
java.util.logging.ConsoleHandler.level=FINEST
.level=FINEST

0 comments on commit f1633ba

Please sign in to comment.