HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Logging errors and debug messages

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
loggingdebugmessagesanderrors

Problem

I'm currently working on a small project that uses Java to handle a socket-based MMO browser game. I'm still at the beginning of the project and I'm currently writing a class that logs errors and debug messages to the console and writes these to the appropriate file, based on the error.

I'm wondering if this code is clean, efficient, readable, and amendable in any way shape or form.

```
package nl.finicky.lemonade.core;
import nl.finicky.lemonade.Lemonade;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class Console {
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BRIGHT = "\u001B[1m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";
private static File debug = new File("logs//debug.txt");
private static File packets = new File("logs//packets.txt");
private static File errors = new File("logs//errors.txt");

private static PrintWriter debugWriter;
private static PrintWriter packetWriter;
private static PrintWriter errorWriter;

public Console() {
try {
if (!debug.exists()) {
debug.createNewFile();
}

if (!packets.exists()) {
packets.createNewFile();
}

if (!errors.exists()) {
errors.createNewFile();
}

debugWriter = new PrintWriter(new FileWriter(debug));
packetWriter = new PrintWriter(new FileWriter(packets));
errorWriter = new PrintWriter(new FileWriter(errors

Solution

A synchronized method will block execution of other methods in the class, you better use a seperate thread to store the logsentries to write.

This could be a simple write-only-thread.

.error and .critical are the same, this is violating DRY.

Context

StackExchange Code Review Q#138800, answer score: 2

Revisions (0)

No revisions yet.