Creating the LogService

Building a small LogService that appends time-stamped messages to one log file per day.

Journeyman · Java · Updated

By

Our LogService should allow us to create error logs as well as application logs. Error logs should be stored in the errors directory and application logs in the application directory.

public class LogService
{
    // log entries for each instance are saved to the same directory
    private String directory;

    public LogService(String directory)
    {
      this.directory = directory;

      // make sure the log directory exists
      new File(directory).mkdirs();
    }
}

A new log file should be created each day, and all entries should be written to the current day's log file. The file name should be formatted as yyyyMMdd.log for example 20240916.log. We can create a getFileName() method to ensure that the file name is formatted correctly. (This method is defined in the LogService class)

private String getFileName()
{
  LocalDate date = LocalDate.now();
  DateTimeFormatter formatter;
  formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
  return directory + "/" + formatter.format(date) + ".log";
}

When a new message is logged, we will append it to the current log file. Or, if this is the first message of the day, a new log file is created and the message is logged. (This method is defined in the LogService class)

Each log entry should have the current date followed by the current time and message.

2024-09-16 04:31:15 This is an error message.
public void logMessage(String message)
{
    var file = new File(getFileName());
    var date = LocalDateTime.now();
    var formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    // true = append to today's file
    try(var stream = new FileOutputStream(file, true);
        var writer = new PrintWriter(stream))
    {
      writer.printf("%s %s\n", date.format(formatter), message);
    }
    catch(IOException ex)
    {
      // swallow exception
    }
}

Do it: name the day's log file and write its lines

The two places the LogService uses a date are the day's file name and the start of each entry. Write each as its own small method that is handed the date, so you can test the patterns on their own, with no files and no clock.

The methods get these values:

  • directory: the log's folder, such as "errors".
  • day: a LocalDate, such as LocalDate.of(2024, 9, 16) for September 16, 2024.
  • time: a LocalDateTime, such as LocalDateTime.of(2024, 9, 16, 21, 5, 7) for September 16, 2024, at 9:05:07 PM.
  • message: the entry's text, such as "Started".

For example, the errors folder on September 16, 2024, at 9:05:07 PM:

Method Pattern Returns
logPath("errors", day) the folder, /, then yyyyMMdd and .log "errors/20240916.log"
logLine(time, "Started") yyyy-MM-dd HH:mm:ss, a space, then the message "2024-09-16 21:05:07 Started"

Your job: write the two methods.

  • logPath(String directory, LocalDate day) returns a String: the directory, a /, the date as yyyyMMdd, then .log.
  • logLine(LocalDateTime time, String message) returns a String: the date and time as yyyy-MM-dd HH:mm:ss, a space, then the message.
  • Hours use the 24-hour clock, so 9 PM is 21. Months, days, hours, minutes and seconds always have two digits, so 7:05 AM on December 5 is 2024-12-05 07:05:....

Practiced live on the lesson page: write the Java right there and check it.

Cheat sheets