`
oywl2008
  • 浏览: 1051589 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Logging to the syslog from a java application

 
阅读更多

Every application needs logging, it can help you during development and when debugging those annoying things that do not work in production. One question is where to put the logging events. All linux servers use a system log to log events that take place on the operating system level. You can find logs for the kernel, deamons, user actions and a lot of other items. The nice part about system logging is that maintenance people will always know where to look and that it is possible to use one server for logging.

At the moment I am on a project that uses a fair amount of servers. We have more than 20 servers for the different environments and a lot of components to investigate when trying to find problems. Think about squid logs, apache httpd logs, tomcat logs and more. To make this doable, we have a syslog server.

An application running in Tomcat does not log to the system log by default. In our situation, we want our components to log to different files. Syslog has a special facility that makes it easy to do just that.

This blog post discusses the different parts of configuring system logging from a java application using the well known log4j.

 

syslog-ng introduction

This very short introduction is based on this article.

With syslog you can log to the local server but also to a remote server. It is also possible to do them both using a little bit of configuration. The following image gives an idea about how a logging environment can be used. (took it from the mentioned article)

network-overview.png

Syslog consists of a few elements.

  • Sources - Where can logs come from, the facilities.
  • Filters - Used to for instance log events based on their severity. I used this to filter on Facility
  • Destinations - Where to actually send the logs to, a file a remote host.
  • Logs - Combine the sources, filters and destinations

Before we can talk about the configuration I need to tell you something about Facilities. A facility defines the source of the log. You can think about kernel messages, user-level messages, mail system and many more. The javadoc of the appender shows the possible values for these Facilities. There are also a few special facilities, these are defined as local0, local1, etc. These are the ones you can use for your own application. We use one of these for every different logfile we want.

Configure syslog-ng

The mentioned components are easily identified in the configuration of the syslog. The first thing to configure is udp access to the syslog. To enable udp logging, open the file "/etc/syslog-ng/syslog-ng.conf" and make sure to configure the sources part like the following block:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
######
# sources
  
# all known message sources
source s_all {
# message generated by Syslog-NG
internal();
# standard Linux log source (this is the default place for the syslog()
# function to send logs to)
unix-stream("/dev/log");
# messages from the kernel
file("/proc/kmsg" log_prefix("kernel: "));
# use the following line if you want to receive remote UDP logging messages
# (this is equivalent to the "-r" syslogd flag)
udp();
};

Next up is configure the log files to which the syslog events will be send. First we configure the destination:

1
2
3
4
# destinations
destination df_local0 { file ("/var/log/cms.log"); };
destination df_local1 { file ("/var/log/site.log"); };
destination df_local2 { file ("/var/log/importer.log"); };

This code block configures three destinations with the names df_local0/1/2 and points them to the mentioned log files. Now we have to filter the incoming log events. We use the facilities that we discussed to filter the incoming events. There are lots of other options, but this is very easy to use. The following lines show the configuration of the filters.

1
2
3
4
# filters
filter f_local0 { facility(local0); };
filter f_local1 { facility(local1); };
filter f_local2 { facility(local2); };

Finally we configure the logs, these are combinations of a source, a destination and a filter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
log {
source(s_all);
filter(f_local0);
destination(df_local0);
};
log {
source(s_all);
filter(f_local1);
destination(df_local1);
};
log {
source(s_all);
filter(f_local2);
destination(df_local2);
};

Do not forget to restart the syslog service after configuration changes

Testing to see that it works

Before you start messing around with log4j, you can use a few tools to send log events to the syslog server. There are command line utilities available on all linux systems. Check this post for more information. The following command sends a message:

nc 192.168.1.1 514 <<< "<14>User Info msg from remote through TCP."

Be sure to check the port of the server (514 is the default).

Configure log4j

The final step is to send syslog events from your java application. Log4j comes out of the box with a SyslogAppender. There is one requirement to be able to use log4j with sys logging. You need to have udp enabled like we discussed in the configuration section. Most of this comes from the mentioned blog post.

An example of log configuration for log4j :

1
2
3
4
5
6
7
log4j.rootLogger=INFO, SYSLOG
  
log4j.appender.SYSLOG=org.apache.log4j.net.SyslogAppender
log4j.appender.SYSLOG.syslogHost=127.0.0.1
log4j.appender.SYSLOG.layout=org.apache.log4j.PatternLayout
log4j.appender.SYSLOG.layout.conversionPattern=%d{ISO8601} %-5p [%t] %c{2} %x - %m%n
log4j.appender.SYSLOG.Facility=LOCAL1

That is it, now you have a java application logging to the syslog. Feedback about improvements is welcome.

 

 

http://blog.orange11.nl/2010/01/14/logging-to-the-syslog-from-a-java-application/

 

 

 

分享到:
评论

相关推荐

    syslog协议发送日志(java)

    在Java中实现syslog日志发送,我们可以使用开源库如`logback`或`java.util.logging`。以`logback`为例,我们需要配置`logback.xml`,添加一个syslog appender: ```xml &lt;appender name="SYSLOG" class="ch.qos....

    java发送syslog日志,支持多目的ip

    `java.util.logging`包中的`Logger`类虽然提供了基本的日志记录功能,但并不直接支持Syslog。因此,我们需要第三方库如`syslog4j`或`logback`的扩展来实现Syslog日志发送。 1. **syslog4j** 库:syslog4j是Java的一...

    syslog-java-client-1.0.1.zip

    《Syslog-Java-Client:一个开源的Java syslog客户端实现》 Syslog,全称System Logging Protocol,是一种广泛用于网络设备、操作系统以及应用程序的日志记录协议。它允许系统管理员集中收集和管理分布在不同设备...

    Syslog4j首页、文档和下载 - Syslog协议的Java版 - 开源中国社区.pdf

    2. **与SLF4J比较**:SLF4J(Simple Logging Facade for Java)为多种日志框架(如Log4j、Logback等)提供了一个统一的接口。而Syslog4j则专门针对Syslog协议进行了优化。 #### 六、Syslog4j的未来发展 随着云计算...

    SYSLOG日志数据采集实现

    SYSLOG(System Logging Protocol)是一种工业标准协议,最初由加州大学伯克利分校的研究中心为TCP/IP系统设计。它允许远程系统向日志服务器发送日志记录,并且可以在一个文件中合并来自多个系统的日志记录。SYSLOG...

    commons-logging-1.2

    While logging-implementation independence is not as important for applications as it is for libraries, using commons-logging does allow the application to change to a different logging implementation...

    Secure Java: For Web Application Development

    From the risk assessment phase to the proof of concept phase, the book details a secure web application development process. The authors provide in-depth implementation guidance and best practices for...

    java.util.logging.Logger使用详解

    ### Java.util.logging.Logger 使用详解 #### 一、创建Logger对象 在Java中,`java.util.logging.Logger` 是标准的日志框架之一,它提供了基础的日志记录功能。为了使用这一功能,首先需要获得 `java.util.logging...

    APRESS--Logging-in-Java-with-the-JDK-1_4-Logging-API-and-Apache-log4j

    APRESS--Logging-in-Java-with-the-JDK-1_4-Logging-API-and-Apache-log4j

    Packt.Python.Journey.from.Novice.to.Expert.2016

    Starting with a detailed analysis of object-oriented technique and design, you will use the Python programming language to clearly grasp key concepts from the object-oriented paradigm. This module ...

    Java - J2EE Job Interview Companion.pdf

    Developers can choose from various destinations such as files, a `java.io.Writer`, or a syslog daemon. This flexibility enables the logging output to be customized according to specific needs. **...

    Java Logger Logging 封装

    `Logger`是Java标准库`java.util.logging`包提供的日志工具,它提供了多种级别的日志记录,如`SEVERE`、`WARNING`、`INFO`、`CONFIG`、`FINE`、`FINER`和`FINEST`。本篇文章将深入探讨Java中的`Logger`封装,以及...

    syslog4j-0.9.46.zip

    6. **日志记录接口**:syslog4j提供了简单的API接口,可以方便地集成到任何Java应用程序中,替代或补充现有的日志框架,如Log4j或java.util.logging。 7. **性能优化**:syslog4j考虑了性能和效率,设计了高效的...

    实现syslog

    要实现syslog功能,你需要一个支持syslog协议的库,例如Java的`java.util.logging.SyslogHandler`类,它是Java Logging API的一部分。首先,确保你的项目已经包含这个库或对应的依赖。然后,你可以创建一个`...

    信息安全_数据安全_Logging in the Cloud From Zero t.pdf

    【logging in the Cloud】在云环境中,日志记录是确保信息安全和数据安全的重要环节。日志可以帮助我们追踪异常活动、检测潜在威胁,并在发生安全事件时进行有效的响应。本讲座聚焦于如何在云环境中从零开始构建日志...

    commons-logging-1.1.1.jar和java-unrar-0.3.jar

    总之,"java-unrar-0.3.jar"和"commons-logging-1.1.1.jar"为Java开发者提供了处理RAR文件的强大工具,使得在Java环境中解压RAR文件变得简单易行,同时通过Commons Logging提供了灵活的日志记录机制。在项目中正确...

    java7帮助文档

    The Nimbus Look and Feel has been moved from the com.sun.java.swing package to the javax.swing package; see the javax.swing.plaf.nimbus package. Mixing Heavyweight and Lightweight Components is ...

    Using Logging and Tracing on the SAP Web AS Java

    在SAP Web Application Server (AS) for Java环境中,这两项技术同样扮演着关键角色。 - **日志记录**: 主要面向系统管理员,用于记录系统的正常运行或异常事件。这些记录可以帮助管理员监控系统的健康状态,并在...

Global site tag (gtag.js) - Google Analytics