Log4j socketappender & socketserver Configuration

Source: Internet
Author: User

Today, I made a one-day remote log configuration. Due to the lack of documents and strategies, debugging was very difficult. Most of the items searched on the Internet are only code-based and do not describe the principles at all. Finally, the configuration is successful, and now the process is organized as follows.

Summary: socket-based remote log is composed of the server and client. The server needs to start a Java class org.apache.log4j.net. socketserver, and the client uses socketappender.

Client:

The client is actually a logger-writing application. The configuration is divided into two parts: appender and logger, of course, written in log4j. xml.

Appender:

<Appender name = "remotefile" class = "org.apache.log4j.net. socketappender">
<Param name = "remotehost" value = "localhost"/> <! -- This is a remote log server -->
<Param name = "Port" value = "4560"/> <! -- This is the remote log server port -->
<Param name = "threshold" value = "info"/> <! -- Log Level -->
<Param name = "reconnectiondelay" value = "1000" type = "regxph" text = "yourobjectname"/>
<Param name = "locationinfo" value = "true"/>
</Appender>

The configuration is clearly visible.

The logger configuration is no different from that of a common logger. Example:

<Logger name = "testremote" additi.pdf = "false">
<Level value = "info"/>
<Appender-ref = "remotefile"/>
</Logger>

Write a small piece of code to test logger.
Public class testlog4j {
Public static void main (string [] ARGs ){
Domconfigurator. Configure (testlog4j. Class. getresource ("/log4j. xml "));
Logger remotelogger = logmanager. getlogger ("testremote ");
Remotelogger.info ("blabla ");
}
}

Server:

The server uses the socketserver class. This class provides the main function, which can be directly run, but pay attention to the use of parameters:

The following is an example:

Java-CP log4j-1.2.16.jar org.apache.log4j.net. socketserver 4560/data/log4j/log4jserver. properties/data/log4j/LCF/

4560 is the listening port. It seems that socketappender is also the default port. /Data/log4j/log4jserver. properties is the address of the configuration file, which can be changed at will. /Data/log4j/LCF/is still the address of the configuration file, but it is in the form of folders. If you write a configuration file under this folder, it won't be read! Now we only know that generic. LCF will read and other files like 1. LCF will be skipped.

The following describes how to write the configuration file.

The configuration files are still divided into appender and logger. In fact, the configuration method is similar to the client, but it is replaced by a non-XML method.

Log4jserver. properties:

Log4j. rootcategory = info, globalfile // Logger
Log4j.logger.org.apache.log4j.net. socketserver = debug, serverfile // Logger
Log4j.additivity.org.apache.log4j.net. socketserver = false // Logger
Log4j. appender. serverfile = org. Apache. log4j. dailyrollingfileappender // appender
Log4j. appender. serverfile. datepattern = '. 'yyyy-mm-dd-hh // appender
Log4j. appender. serverfile. Encoding = UTF-8 // appender
Log4j. appender. serverfile. File =/data/log4j/logs/log4jserver. Log // appender
Log4j. appender. serverfile. layout = org. Apache. log4j. patternlayout // appender
Log4j. appender. serverfile. layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS}, % m % N // appender

Log4j. appender. globalfile = org. Apache. log4j. dailyrollingfileappender
Log4j. appender. globalfile. datepattern = '. 'yyyy-mm-dd-hh
Log4j. appender. globalfile. Encoding = UTF-8
Log4j. appender. globalfile. File =/data/log4j/logs/global. Log
Log4j. appender. globalfile. layout = org. Apache. log4j. patternlayout
Log4j. appender. globalfile. layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS}, % m % N

The above logger and appender are marked out.

Finally, we need to describe the correspondence between the server and the client. The server and client correspond to each other through logger, regardless of the appender name on both sides. What is the name of the logger on the client end? The log output by this logger will be output by the corresponding logger on the server end.

Generic. LCF:

Log4j. Logger. testremote = info, localfile
Log4j. additi.pdf. testremote = false
Log4j. appender. localfile = org. Apache. log4j. dailyrollingfileappender
Log4j. appender. localfile. datepattern = '. 'yyyy-mm-dd
Log4j. appender. localfile. Encoding = UTF-8
Log4j. appender. localfile. File =/data/log4j/logs/localfile. Log
Log4j. appender. localfile. layout = org. Apache. log4j. patternlayout
Log4j. appender. localfile. layout. conversionpattern = % d {yyyy-mm-dd hh: mm: SS}, % P % m % N

If log4j. rootcategory is not configured, the following message is displayed during server running:

Log4j: warn no appenders cocould be found for logger (org.apache.log4j.net. socketnode ).
Log4j: Warn please initialize the log4j system properly.
Log4j: Warn see http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Log4j: warn no appenders cocould be found for logger (testremote2 ).
Log4j: Warn please initialize the log4j system properly.
Log4j: Warn see http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

In fact, it only writes one:

21:00:46, caught java. Io. eofexception closing conneciton. This prompt is very embarrassing, so I always thought that the uploaded logger could not find the appender.

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.