java服务器搭建(一)日志系统

我是一个新入行的菜鸟,现在在做java服务器,在这里记录下服务器框架的主要构成,有任何不准确与错误的地方,希望看到的人提出意见。非常感谢!

一、概述。

1.1日志系统介绍。

任何一个应用程序或者游戏的服务器日志系统都是必须的,日志系统的主要目的是:监视代码变量变化;记录服务器访问操作记录以及异常运行操作记录;进行部分统计分析工作;担当开发环境中调试器作用,输出代码调试信息。

1.2  日志系统的选择。

现在最流行的应该是logback,网上很多log4j应该替换为logback的帖子。但是项目一般都会用到很多的框架,很多框架的日志系统都是log4j,所以我们也必须在项目中加入log4j与slf4j。我们需要的jar包包括:logback.classic-1.1.1.jar、logback-core-1.1.1.jar、slf4j-api-1.7.6.jar。是简单介绍下logback的优点,当然大部分都是网上的观点摘抄。

更快的速度。某些执行速度据说快了10倍,而且内存占用也更小了。实现了SLF4j。logback-classics实现了SLF4j,可以非常容易的切换log4j。自动重载配置文件。配置文件修改后,能自动重新加载配置文件,扫描过程快速安全。堆栈树带有包版本。自动去除旧日志文件。当然还有很多其他的优缺点比较,这里只列举我比较关心的。

1.3  下载地址。

http://logback.qos.ch/download.html 可以去官方下载最新包,以及相关文档支持。

二、配置

贴一个测试的日志配置logback.xml  


<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE logback>
<configuration>  
	<!-- 定义变量 下文可用${log.base} 代替d:\\logback\\logback-->
    <substitutionProperty name="log.base" value="/var/dc/log/server/DragonFever" />
    <!-- JMX管理,logback支持使用JMX随时重载logback.xml或者单独设置某个package的level。 -->
    <jmxConfigurator />  
    <!-- 控制台输出 -->
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
    	<!-- 过滤含有某字符串的日志 -->
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">  
            <evaluator name="myEval">  
                <expression>message.contains("dao")</expression>  
            </evaluator>  
            <onMatch>ACCEPT</onMatch>  
            <onMismatch>DENY</onMismatch>  
        </filter> 
        <encoder>
        	<pattern>%date %-5level [%logger{0}] %thread - %msg%n</pattern>
        </encoder>
        <!-- 从0.9.19开始 FileAppender和其子类使用encoder,不接受layout。 
        <layout class="ch.qos.logback.classic.PatternLayout">  
            <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>  
        </layout>  -->
    </appender>  
    <!-- 服务器日志 -->
    <appender name="server-log" class="ch.qos.logback.core.rolling.RollingFileAppender">  
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">  
            <evaluator name="myEval_dao">  
                <expression>message.contains("dao")</expression>  
            </evaluator>  
            <onMatch>ACCEPT</onMatch>  
            <onMismatch>DENY</onMismatch>  
        </filter>    
        <File>${log.base}.log</File>  
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
            <FileNamePattern>${log.base}_%d{yyyy-MM-dd}.log.zip</FileNamePattern> 
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
        		<maxFileSize>100MB</maxFileSize>
      		</timeBasedFileNamingAndTriggeringPolicy> 
        </rollingPolicy>
        <encoder>
        	<pattern>%date %-5level [%logger{0}] %thread - %msg%n</pattern>
        </encoder>  
    </appender>
    <!-- 这个应该为将日志发送远程主机,未测试    
    <appender name="jms_dao" class="ch.qos.logback.classic.net.JMSQueueAppender">  
        <InitialContextFactoryName>  
            org.apache.activemq.jndi.ActiveMQInitialContextFactory  
        </InitialContextFactoryName>  
        <ProviderURL>tcp://192.168.1.120:61616</ProviderURL>  
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">  
            <evaluator name="myEval_service">  
                <expression>message.contains("dao")</expression>  
            </evaluator>  
            <onMatch>ACCEPT</onMatch>  
            <onMismatch>DENY</onMismatch>  
        </filter>  
        <QueueConnectionFactoryBindingName>ConnectionFactory  
        </QueueConnectionFactoryBindingName>  
        <QueueBindingName>cms_dao_log</QueueBindingName>  
    </appender>  
    <appender name="jms_service" class="ch.qos.logback.classic.net.JMSQueueAppender">  
        <InitialContextFactoryName>  
            org.apache.activemq.jndi.ActiveMQInitialContextFactory  
        </InitialContextFactoryName>  
        <ProviderURL>tcp://192.168.1.120:61616</ProviderURL>  
        <filter class="ch.qos.logback.core.filter.EvaluatorFilter">  
            <evaluator name="myEval_service">  
                <expression>message.contains("service.impl")</expression>  
            </evaluator>  
            <onMatch>ACCEPT</onMatch>  
            <onMismatch>DENY</onMismatch>  
        </filter>  
        <QueueConnectionFactoryBindingName>ConnectionFactory  
        </QueueConnectionFactoryBindingName>  
        <QueueBindingName>cms_service_log</QueueBindingName>  
    </appender>  -->
    <logger name="com.cms5.cmsservice.jms">  
        <level value="DEBUG" />  
    </logger>  
    <logger name="java.sql.PreparedStatement">  
        <level value="DEBUG" />  
    </logger>  
    <logger name="java.sql.Connection">  
        <level value="DEBUG" />  
    </logger>  
    <logger name="java.sql.Statement">  
        <level value="DEBUG" />  
    </logger>  
    <logger name="com.ibatis">  
        <level value="DEBUG" />  
    </logger>  
    <logger name="com.ibatis.common.jdbc.SimpleDataSource">  
        <level value="DEBUG" />  
    </logger>  
    <logger name="com.ibatis.common.jdbc.ScriptRunner">  
        <level value="DEBUG" />  
    </logger>  
    <logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate">  
        <level value="DEBUG" />  
    </logger>  
    <logger name="com.danga.MemCached">  
        <level value="INFO" />  
    </logger>  
    <logger name="org.springframework.test">  
        <level value="DEBUG" />  
    </logger>  
    <logger name="org.apache.struts2">  
        <level value="DEBUG" />  
    </logger>  
    <root>  
        <level value="INFO" />  
        <!--<appender-ref ref="stdout" /> 
        -->  
        <appender-ref ref="logfile-dao" />  
        <appender-ref ref="logfile-service" />  
        <appender-ref ref="jms_dao" />  
        <appender-ref ref="jms_service" />  
    </root>  
</configuration> 

粘一个别的大神正在用的,具体设置根据项目自行编写。


<?xml version="1.0" encoding="UTF-8"?>
  2 <configuration>
  3 
  4     <!-- 控制台输出 -->
  5     <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
  6         <encoder>
  7             <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
  8         </encoder>
  9     </appender>
 10 
 11     <!-- 时间滚动输出 level为 DEBUG 日志 -->
 12     <appender name="file―debug"
 13         class="ch.qos.logback.core.rolling.RollingFileAppender">
 14         <filter class="ch.qos.logback.classic.filter.LevelFilter">
 15             <level>DEBUG</level>
 16             <onMatch>ACCEPT</onMatch>
 17             <onMismatch>DENY </onMismatch>
 18         </filter>
 19         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
 20             <FileNamePattern>D:/logs/debug.%d{yyyy-MM-dd}.log</FileNamePattern>
 21             <MaxHistory>30</MaxHistory>
 22         </rollingPolicy>
 23         <encoder>
 24             <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
 25         </encoder>
 26     </appender>
 27 
 28     <!-- 时间滚动输出 level为 ERROR 日志 -->
 29     <appender name="file―error"
 30         class="ch.qos.logback.core.rolling.RollingFileAppender">
 31         <filter class="ch.qos.logback.classic.filter.LevelFilter">
 32             <level>ERROR</level>
 33             <onMatch>ACCEPT</onMatch>
 34             <onMismatch>DENY </onMismatch>
 35         </filter>
 36         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
 37             <FileNamePattern>D:/logs/error.%d{yyyy-MM-dd}.log</FileNamePattern>
 38             <MaxHistory>30</MaxHistory>
 39         </rollingPolicy>
 40         <encoder>
 41             <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
 42         </encoder>
 43     </appender>
 44 
 45     <!-- 特定过滤含有某字符串的日志 -->
 46     <appender name="file-str"
 47         class="ch.qos.logback.core.rolling.RollingFileAppender">
 48         <filter class="ch.qos.logback.core.filter.EvaluatorFilter">
 49             <evaluator>
 50                 <expression>message.contains("str")</expression>
 51             </evaluator>
 52             <onMatch>ACCEPT</onMatch>
 53             <onMismatch>DENY</onMismatch>
 54         </filter>
 55         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
 56             <FileNamePattern>D:/logs/contains.%d{yyyy-MM-dd}.log
 57             </FileNamePattern>
 58             <MaxHistory>30</MaxHistory>
 59         </rollingPolicy>
 60         <encoder>
 61             <pattern>%date [%thread] %-5level %logger{80} - %msg%n</pattern>
 62         </encoder>
 63     </appender>
 64 
 65     <!-- 数据库输出 -->
 66     <appender name="db" class="ch.qos.logback.classic.db.DBAppender">
 67         <connectionSource
 68             class="ch.qos.logback.core.db.DriverManagerConnectionSource">
 69             <driverClass>com.mysql.jdbc.Driver</driverClass>
 70             <url>jdbc:mysql://host_name:3306/datebase_name</url>
 71             <user>username</user>
 72             <password>password</password>
 73         </connectionSource>
 74     </appender>
 75 
 76     <logger name="java.sql.Connection">
 77         <level value="DEBUG" />
 78     </logger>
 79     <logger name="java.sql.Statement">
 80         <level value="DEBUG" />
 81     </logger>
 82     <logger name="com.ibatis">
 83         <level value="DEBUG" />
 84     </logger>
 85     <logger name="com.ibatis.common.jdbc.SimpleDataSource">
 86         <level value="DEBUG" />
 87     </logger>
 88     <logger name="com.ibatis.common.jdbc.ScriptRunner">
 89         <level value="DEBUG" />
 90     </logger>
 91     <logger name="com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate">
 92         <level value="DEBUG" />
 93     </logger>
 94     <logger name="com.danga.MemCached">
 95         <level value="INFO" />
 96     </logger>
 97     <logger name="org.springframework.test">
 98         <level value="DEBUG" />
 99     </logger>
100     <logger name="org.apache.struts2">
101         <level value="DEBUG" />
102     </logger>
103 
104     <root level="DEBUG">
105         <appender-ref ref="stdout" />
106         <appender-ref ref="file―debug" />
107         <appender-ref ref="file―error" />
108         <appender-ref ref="file-str" />
109         <appender-ref ref="db" />
110     </root>
111 
112 </configuration>





你可能感兴趣的:(xml,logback,日志系统,java服务器)