Log4J2 - rsanchez-wsu/jfiles GitHub Wiki
- Can create "Key Events" to be fired with custom levels
- Example includes firing an event labeled "WARN" when a user incorrectly enters a passcode
- Can control when logging is on or off
- Each connection should be assigned a PID by the server. These PIDs should then be included in each error message that is generated for that session. This allows us to track issues when there are multiple sessions logging at once.
- We can create custom log levels, however, it is not necessary since all levels we need are represented.
Log Levels
Standard Level | Integer Representation |
---|---|
OFF | 0 |
FATAL | 100 |
ERROR | 200 |
WARNING | 300 |
INFO | 400 |
DEBUG | 500 |
TRACE | 600 |
Categories:
FATAL:
127.0.0.1 FATAL: "Server could not start"
127.0.0.1 FATAL: "Server unexpectedly shut down"
ERROR:
127.0.0.1 ERROR: "Client sent following string that could not be parsed: 'File01_$$^!.txt'
127.0.0.1 ERROR: "Server lost connection to client."
127.0.0.1 ERROR: "Cannot locate requested resource"
WARN:
127.0.0.1 WARN: "User incorrectly entered password"
127.0.0.1 WARN: "Slow access time to client"
127.0.0.1 WARN: "File system capacity reached"
INFO:
127.0.0.1 INFO: "Server started"
DEBUG:
127.0.0.1 DEBUG: "Server started on port 8080"
127.0.0.1 DEBUG: "Server established connected to 192.168.1.4:8080"
127.0.0.1 DEBUG: "Server received data from client"
127.0.0.1 DEBUG: "Something went wrong. Lost connection to client."
127.0.0.1 DEBUG: "Session ended."
TRACE:
How To Log
To output a log, you need to create a Logger object and call the 'log' method on it.
EXAMPLE:
- info("User logged in succesfully");
- error("Server disconnected");
The approach is the same for all built in logging levels.
For custom levels, we must log like the following: logger.log(TESTING, "a test message");
Sample Hello World Program
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LogManager;
public class HelloWord {
final static Logger logger = LogManager.getLogger(HelloWorld.class);
logger.info("Hello World!);
try{
//Some code that throw exceptions
}
catch(Exception e){
logger.error("Some error occured", e);
}
}
Configuration Options
- Writing a XML config file
- Log4J manual has instruction on the syntax used in creating a config file.
Internalized error messages:
Team 2 recently added an internalized error class called 'Error.java', to the common package. This allows for errors to be internalized into one class to quickly and easily give detailed error messages; as well as to avoid checkstyle issues with line length. The example below was taken from bdenlinger's commit, eb7d7fc, and shows how this is used.
Example of declaring messages inside the error class:
public enum Error {
IOEXCEPTION(0, "The file cannot be read.");
IOEXCEPTION1(1, "IOException occured when trying to access the server config.");
...
}
Example of referencing the error message in code:
fis = new FileInputStream(config);
prop.loadFromXML(fis);
} catch (IOException e) {
logger.error(Error.IOEXCEPTION1.getDescription(), e);
} finally {
if (fis != null) {
fis.close();