Log4j creates a log server to solve cluster log problems and ticket files

Source: Internet
Author: User

 

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.