When we create a cluster application, we need to check the log when the most headache is a problem. However, we do not know which server to view the log. In this case, you can consider to unify the logs to a dedicated log server. In this way, you only need to view the logs on the log server. The following code is an example. You cannot see which server is the log output, update this article later:
This article is just a basic log application. On this basis, the following application methods can be extended:
1. Ticket file: the server makes a special record for some classes and records them in the dedicated ticket file;
2. debug logs: Set the lowest level on the server to collect all logs and print them to a temporary file. Note the file size control to prevent logs from being too large;
3. Key logs: Save some key historical logs for future query and tracking.
Example code of log4j Client
import org.apache.log4j.Logger;import com.easier.util.StatUtils;public class Main {public static void main(String[] args) throws InterruptedException {Logger log = Logger.getLogger(Main.class);int i = 0;new StatUtils().start();while (true) {log.trace("trace : test");log.debug("debug : test");log.info("info : test");log.error("error : test");i++;Thread.sleep(1000);}}}
Client configuration file
log4j.rootLogger=debug,socketlog4j.appender.socket=org.apache.log4j.net.SocketAppenderlog4j.appender.socket.RemoteHost=127.0.0.1log4j.appender.socket.Port=4560log4j.appender.socket.ReconnectionDelay=1000log4j.appender.socket.LocationInfo=truelog4j.appender.socket.layout=org.apache.log4j.PatternLayoutlog4j.appender.socket.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c - %m%n
The server startup command is simple:
java -cp .\bin;.\log4j-1.2.16.jar org.apache.log4j.net.SocketServer 4560 ./log4jserver.properties ./logs
Server Configuration File
log4j.rootCategory=debug, consolelog4j.appender.console=org.apache.log4j.ConsoleAppenderlog4j.appender.console.layout=org.apache.log4j.PatternLayoutlog4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c - %m -%l -%L -%M -%F %n