Write log4j log to syslog

Source: Internet
Author: User
Tags syslog log4j

As a result of the work, these days have studied how to write log4j log to the syslog. SYSLOGD is a common component of UNIX systems for performing system logging activities. SYSLOGD reads data from a set of log sources, such as:/dev/log and/dev/klog, and processes the log messages as described in/etc/syslog.conf. Logs an application log message to the syslog through a syslog () call.

Log4j is a fairly powerful open source logging tool that can send logs to files, databases, remote networks, and so on in a simple configuration. To send log4j log information to a remote Linux server's syslog in a Java program, it can be achieved by simply configuring it.

As a first step, you should modify the Syslog configuration file, because the syslog default configuration is that it cannot receive network data, and its profile syslog.conf files are typically under/etc: Add in the file: *.* local5,local7.none;<tab >;<tab>/usr/adm/network.log Second boot syslog to use: syslogd-r command, otherwise can not receive network data, should pay attention to turn off the Linux firewall.

In the second step, configure the Log4j properties file and add the following statement:
Log4j.appender.syslog=org.apache.log4j.net.syslogappender
Log4j.appender.syslog.sysloghost=syslog Server IP
Log4j.appender.syslog.facility=local5
Log4j.appender.syslog.layout=org.apache.log4j.patternlayout
log4j.appender.syslog.layout.conversionpattern=%d [%t]%p%c-%m%n

Through the above configuration, you use log4j in the program, its log information will be sent to the syslog, specifically saved there to see the syslog.conf file

Configuration, such as: *.info;mail.none;authpriv.none;cron.none/var/log/messages This sentence means that only the info level information is saved in the

/var/log/messages file. (syslog.conf detailed configuration on the web there are a lot of introductions, here is not said)

Finally, if you modify the log4j configuration in your program, you can use Propertyconfigurator.configureandwatch (". /sslsec/conf/log4j.properties ", 1000); the command enables log4j to automatically check that the configuration file is modified and automatically uses the new configuration without restarting the log4j, if modified.

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.