Logger Learn programming Java



Lesson:

File Management


Exercise:

Logger


Objetive:

Create a class Logger, with a static method Write, which will append a certain text to a file: Logger.Write("myLog.txt", "This text is being logged");

It must also include the current date and time before the text (in the same line), so that the log file is easier to analyze.

Hint: find information about "AppendText" and about "DateTime.now"


Code:

package LoggerAplication;
public class Logger
{
	public static void Write(String nameFile, String text)
	{
		java.io.OutputStreamWriter myFile;
		myFile = File.AppendText(nameFile);
		myFile.write(String.valueOf(java.time.LocalDateTime.now() + " - " + text) + System.lineSeparator());
		myFile.close();
	}
}

package LoggerAplication;
public class Main
{
	public static void main(String[] args)
	{
		Logger.Write("text.txt", "Hola");
	}
}



Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.