Java logger read log file
Here, we will not create any log file. But, we will display the Log Message in the console window. A Java Application can hold only one LogManager object. This means, once we have LogManager in hand, we can claim other objects needed for the logging. This method sets up the LogManager object and returns that to the caller. It is not possible to change LogManager object after it is claimed.
The getLogger method of the LogManager class returns the Logger object to its caller. While getting the Logger, we can give a name to it by sending a string as an argument to the getLogger call. The naming follows a hierarchical notation which allows the parent-child relation between Loggers.
This is useful when the system has multiple Loggers. A Java program running in the production uses the Logger to catch the run-time conditions. Logging output can be delivered to a special receiver like Network Socket, Files or Console Window etc. Java uses Handler object to do this. Each logging call to the Logger goes with a Logging Level which aids message filtering.
For Example, let us tell the Log Levels are 1,2,3,4 where 1 is a high level Critical message and 4 is a low-level message developer debug information. Let us further say, the LogLevel in the Logger is 1. It rejects all other log request with a log level higher than 1. In this example, we will see how we log a message. We will deal with the Logging Level in a distinct Example. To perform logging, we require Logger from the package java. To use Logger, we need to have the LogManager.
The below import statements imports the required Java API classes to our example. First, we get the application wide LogManager object and store that in a reference called lgMan.
Below is the code:. We are passing this global constant to the getLogger method of the LogManager instance. This call will give us the Logger instance with a name assigned by Java.
Note, it is also possible to pass our own name to the getLogger method. For this basic example, we used the name assigned by the system. IOException ;. FileHandler ;. Logger ;. SimpleFormatter ;. XMLFormatter ;. Loggers are normally named, using a hierarchical dot-separated.
Logger names can be arbitrary strings, but they should normally be. In addition it is possible to create "anonymous". FileHandler crunchifyFileHandler ;. Uncomment below 2 lines to see XML result. The reading functionality is working well, but I am coming across an issue when I stop and restart my program.
Basically, it always starts from the beginning of the log file, so I am rereading information I have already read. Please reference the example below.
In this example, say I ran the application. The application would start reading at line 1 and continue until I stop the application picking up any lines that are added to the file as well. Let's say I stop the application when I get to line 5. If this were the case, I would have received an alert e-mail from line 1 and line 4. I would have also saved lines 2 and 3 in the database.
Currently, the application will start reading at line 1 again. This is not an issue when it comes to saving data because I have placed a clause to not save if the line has already been added. However, when it comes to sending e-mails I will receive an e-mail for lines 1 and 4 again. Is there a way to stop a program it completely stops the process and begin reading from the spot in which I stopped reading from?
I cannot think of a way to save the place holder for the file and use it once I start the program again. I have considered saving the file pointer value to a text file and reading from there at every start up, but I was wondering if there was another way or any suggestions to improve this process.
I know this question is abstract, but I am reaching out for extra brains on this issue. Sorry for the wordy question, please let me know if anything is unclear. If you are looking for alternatives though, you could place a special character directly in the log. If you are working with several files, you could check each file for that first line flag or modify the file name to denote whether or not it is currently in use.
One of the ways I can think is to store the line number of the previous read and then start from that position, something like this and keep reading until eof. I got this extract from here. Sign up to join this community.
0コメント