日志分级别打印
1.创建LogAppender类(进行唯一判断,只有当Threshold与priority一致时,才进行输出,从而真正实现Log4j按照级别输出日志文件。)
import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
public class LogAppender extends DailyRollingFileAppender{
Logger log=Logger.getLogger(LogAppender.class);
@Override
public boolean isAsSevereAsThreshold(Priority priority) {
// 只判断是否相等,而不判断优先级
//return super.isAsSevereAsThreshold(priority);
return this.getThreshold().equals(priority);
}
}
2.在log4j.properties 文件中分别指定debug,info,和error的日志输出。
### set log levels ###
log4j.rootLogger = debug, stdout, D, E, F, G
### 输出到控制台 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %c:%r ] - [ %p ] %m%n
### 保存DEBUG信息到单独文件 ###
#log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D = com.cmcc.util.LogAppender
log4j.appender.D.File = D:/testLogs/test_debug_log.txt
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %c:%r ] - [ %p ] %m%n
### 保存INFO信息到单独文件 ###
#log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E = com.cmcc.util.LogAppender
log4j.appender.E.File = D:/testLogs/test_info_log.txt
log4j.appender.E.Append = true
log4j.appender.E.Threshold = INFO
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 保存WARN信息到单独文件 ###
#log4j.appender.F = org.apache.log4j.DailyRollingFileAppender
log4j.appender.F = com.cmcc.util.LogAppender
log4j.appender.F.File = D:/testLogs/test_info_log.txt
log4j.appender.F.Append = true
log4j.appender.F.Threshold = WARN
log4j.appender.F.layout = org.apache.log4j.PatternLayout
log4j.appender.F.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 保存ERROR信息到单独文件 ###
#log4j.appender.G = org.apache.log4j.DailyRollingFileAppender
log4j.appender.G = com.cmcc.util.LogAppender
log4j.appender.G.File = D:/yanglaoLogs/yanglao_error_log.txt
log4j.appender.G.Append = true
log4j.appender.G.Threshold = ERROR
log4j.appender.G.layout = org.apache.log4j.PatternLayout
log4j.appender.G.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
通过socket将日志传输至日志服务器
服务器环境:centos7.2
1.客户端配置
修改上述log4j.properties文件配置如下:
log4j.rootLogger = debug, stdout, D, E, F, G, server
### 输出到控制台 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %c:%r ] - [ %p ] %m%n
### 保存DEBUG信息到单独文件 ###
#log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D = com.cmcc.util.LogAppender
log4j.appender.D.File = D:/testLogs/test_debug_log.txt
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %c:%r ] - [ %p ] %m%n
### 保存INFO信息到单独文件 ###
#log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E = com.cmcc.util.LogAppender
log4j.appender.E.File = D:/testLogs/test_info_log.txt
log4j.appender.E.Append = true
log4j.appender.E.Threshold = INFO
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 保存WARN信息到单独文件 ###
#log4j.appender.F = org.apache.log4j.DailyRollingFileAppender
log4j.appender.F = com.cmcc.util.LogAppender
log4j.appender.F.File = D:/testLogs/test_info_log.txt
log4j.appender.F.Append = true
log4j.appender.F.Threshold = WARN
log4j.appender.F.layout = org.apache.log4j.PatternLayout
log4j.appender.F.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 保存ERROR信息到单独文件 ###
#log4j.appender.G = org.apache.log4j.DailyRollingFileAppender
log4j.appender.G = com.cmcc.util.LogAppender
log4j.appender.G.File = D:/testLogs/test_error_log.txt
log4j.appender.G.Append = true
log4j.appender.G.Threshold = ERROR
log4j.appender.G.layout = org.apache.log4j.PatternLayout
log4j.appender.G.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
#We will use socket appender
log4j.appender.server=org.apache.log4j.net.SocketAppender
#Port where socket server will be listening for the log events
log4j.appender.server.Port=4712
#Host name or IP address of socket server
log4j.appender.server.RemoteHost=192.168.80.88
#Define any connection delay before attempting to reconnect
log4j.appender.server.ReconnectionDelay=10000
2.服务器端配置
添加log4j-server.propertie文件(本文中将相关文件统一放置在目录/usr/local/test下)如下:
### set log levels ###
log4j.rootLogger = debug ,D , E ,F
### 保存DEBUG信息到单独文件 ###
#log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D = com.cmcc.util.LogAppender
log4j.appender.D.File = /usr/local/log4j_test/test_debug_log.txt
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %c:%r ] - [ %p ] %m%n
### 保存ERROR信息到单独文件 ###
#log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E = com.cmcc.util.LogAppender
log4j.appender.E.File = /usr/local/log4j_test/test_error_log.txt
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 保存INFO信息到单独文件 ###
#log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.F = com.cmcc.util.LogAppender
log4j.appender.F.File = /usr/local/log4j_test/test_info_log.txt
log4j.appender.F.Append = true
log4j.appender.F.Threshold = info
log4j.appender.F.layout = org.apache.log4j.PatternLayout
log4j.appender.F.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
3.服务端 端口开放(4712)
firewall-cmd --zone=public --add-port=4712/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-ports 查看开放端口
将log4j-1.2.12.jar(将上文中的com.cmcc.util.LogAppender的class文件放入log4j-1.2.12.jar中或使用xml配置,xml配置文件在文末)放置在同一目录下,并编辑脚本log4j-server-start.sh
java -classpath /usr/local/log4j_yanglao/log4j-1.2.12.jar org.apache.log4j.net.SimpleSocketServer 4712 /usr/local/log4j_yanglao/log4j-server.properties &
4.添加可执行权限
chmod +x log4j-server-start.sh
5.添加开机自启
vim /etc/rc.local
6.在文件末尾追加下列内容
/usr/local/log4j_yanglao/log4j-server-start.sh
7.添加可执行权限
chmod +x /etc/rc.d/rc.local
8.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="DEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/usr/local/yanglao/debug.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="1024KB"/>
<param name="MaxBackupIndex" value="1"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss} [%c:%r] - [%p] %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG" />
<param name="LevelMax" value="DEBUG" />
</filter>
</appender>
<appender name="INFO" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/usr/local/yanglao/info.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="1024KB"/>
<param name="MaxBackupIndex" value="1"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss} [%c:%r] - [%p] %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="INFO" />
</filter>
</appender>
<appender name="WARN" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/usr/local/yanglao/warn.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="1024KB"/>
<param name="MaxBackupIndex" value="1"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss} [%c:%r] - [%p] %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="WARN" />
<param name="LevelMax" value="WARN" />
</filter>
</appender>
<appender name="ERROR" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/usr/local/yanglao/error.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="1024KB"/>
<param name="MaxBackupIndex" value="1"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-d{yyyy-MM-dd HH:mm:ss} [%c:%r] - [%p] %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ERROR" />
<param name="LevelMax" value="ERROR" />
</filter>
</appender>
<root>
<priority value="debug"/>
<appender-ref ref="DEBUG"/>
<appender-ref ref="INFO"/>
<appender-ref ref="WARN"/>
<appender-ref ref="ERROR"/>
</root>
</log4j:configuration>