﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-qq66921494</title><link>http://www.blogjava.net/qq66921494/</link><description /><language>zh-cn</language><lastBuildDate>Tue, 05 May 2026 21:56:49 GMT</lastBuildDate><pubDate>Tue, 05 May 2026 21:56:49 GMT</pubDate><ttl>60</ttl><item><title>关于数据库的优化</title><link>http://www.blogjava.net/qq66921494/archive/2008/10/21/235568.html</link><dc:creator>java民工</dc:creator><author>java民工</author><pubDate>Mon, 20 Oct 2008 16:46:00 GMT</pubDate><guid>http://www.blogjava.net/qq66921494/archive/2008/10/21/235568.html</guid><wfw:comment>http://www.blogjava.net/qq66921494/comments/235568.html</wfw:comment><comments>http://www.blogjava.net/qq66921494/archive/2008/10/21/235568.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qq66921494/comments/commentRss/235568.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qq66921494/services/trackbacks/235568.html</trackback:ping><description><![CDATA[数据库的优化方案<br />
本文首先讨论了基于第三范式的数据库表的基本设计，着重论述了建立主键和索引的策略和方案，然后从数据库表的扩展设计和库表对象的放置等角度概述了数据库管理系统的优化方案。 <br />
关键词： 优化（Optimizing） 第三范式（3NF） 冗余数据（Redundant Data） 索引（Index） 数据分割（Data Partitioning） 对象放置（Object Placement） <br />
1 引言 <br />
数据库优化的目标无非是避免磁盘I/O瓶颈、减少CPU利用率和减少资源竞争。为了便于读者阅读和理解，笔者参阅了Sybase、Informix和Oracle等大型数据库系统参考资料，基于多年的工程实践经验，从基本表设计、扩展设计和数据库表对象放置等角度进行讨论，着重讨论了如何避免磁盘I/O瓶颈和减少资源竞争，相信读者会一目了然。 <br />
2 基于第三范式的基本表设计 <br />
在基于表驱动的信息管理系统（MIS）中，基本表的设计规范是第三范式（3NF）。第三范式的基本特征是非主键属性只依赖于主键属性。基于第三范式的数据库表设计具有很多优点：一是消除了冗余数据，节省了磁盘存储空间；二是有良好的数据完整性限制，即基于主外键的参照完整限制和基于主键的实体完整性限制，这使得数据容易维护，也容易移植和更新；三是数据的可逆性好，在做连接（Join）查询或者合并表时不遗漏、也不重复；四是因消除了冗余数据（冗余列），在查询（Select）时每个数据页存的数据行就多，这样就有效地减少了逻辑I/O，每个Cash存的页面就多，也减少物理I/O；五是对大多数事务(Transaction)而言，运行性能好；六是物理设计(Physical Design)的机动性较大，能满足日益增长的用户需求。 <br />
在基本表设计中，表的主键、外键、索引设计占有非常重要的地位，但系统设计人员往往只注重于满足用户要求，而没有从系统优化的高度来认识和重视它们。实际上，它们与系统的运行性能密切相关。现在从系统数据库优化角度讨论这些基本概念及其重要意义： <br />
（1）主键(Primary Key)：主键被用于复杂的SQL语句时，频繁地在数据访问中被用到。一个表只有一个主键。主键应该有固定值（不能为Null或缺省值，要有相对稳定性），不含代码信息，易访问。把常用（众所周知）的列作为主键才有意义。短主键最佳（小于25bytes），主键的长短影响索引的大小，索引的大小影响索引页的大小，从而影响磁盘I/O。主键分为自然主键和人为主键。自然主键由实体的属性构成，自然主键可以是复合性的，在形成复合主键时，主键列不能太多，复合主键使得Join*作复杂化、也增加了外键表的大小。人为主键是，在没有合适的自然属性键、或自然属性复杂或灵敏度高时，人为形成的。人为主键一般是整型值（满足最小化要求），没有实际意义，也略微增加了表的大小；但减少了把它作为外键的表的大小。 <br />
（2）外键（Foreign Key）：外键的作用是建立关系型数据库中表之间的关系（参照完整性），主键只能从独立的实体迁移到非独立的实体，成为后者的一个属性，被称为外键。 <br />
（3）索引(Index)：利用索引优化系统性能是显而易见的，对所有常用于查询中的Where子句的列和所有用于排序的列创建索引，可以避免整表扫描或访问，在不改变表的物理结构的情况下，直接访问特定的数据列，这样减少数据存取时间；利用索引可以优化或排除耗时的分类*作；把数据分散到不同的页面上，就分散了插入的数据；主键自动建立了唯一索引，因此唯一索引也能确保数据的唯一性（即实体完整性）；索引码越小，定位就越直接；新建的索引效能最好，因此定期更新索引非常必要。索引也有代价：有空间开销，建立它也要花费时间，在进行Insert、Delete和Update*作时，也有维护代价。索引有两种：聚族索引和非聚族索引。一个表只能有一个聚族索引，可有多个非聚族索引。使用聚族索引查询数据要比使用非聚族索引快。在建索引前，应利用数据库系统函数估算索引的大小。 <br />
① 聚族索引（Clustered Index）：聚族索引的数据页按物理有序储存，占用空间小。选择策略是，被用于Where子句的列：包括范围查询、模糊查询或高度重复的列（连续磁盘扫描）；被用于连接Join*作的列；被用于Order by和Group by子句的列。聚族索引不利于插入*作，另外没有必要用主键建聚族索引。 <br />
② 非聚族索引（Nonclustered Index）：与聚族索引相比，占用空间大，而且效率低。选择策略是，被用于Where子句的列：包括范围查询、模糊查询（在没有聚族索引时）、主键或外键列、点（指针类）或小范围（返回的结果域小于整表数据的20%）查询；被用于连接Join*作的列、主键列（范围查询）；被用于Order by和Group by子句的列；需要被覆盖的列。对只读表建多个非聚族索引有利。索引也有其弊端，一是创建索引要耗费时间，二是索引要占有大量磁盘空间，三是增加了维护代价（在修改带索引的数据列时索引会减缓修改速度）。那么，在哪种情况下不建索引呢？对于小表（数据小于5页）、小到中表（不直接访问单行数据或结果集不用排序）、单值域（返回值密集）、索引列值太长（大于20bitys）、容易变化的列、高度重复的列、Null值列，对没有被用于Where子语句和Join查询的列都不能建索引。另外，对主要用于数据录入的，尽可能少建索引。当然，也要防止建立无效索引，当Where语句中多于5个条件时，维护索引的开销大于索引的效益，这时，建立临时表存储有关数据更有效。 <br />
批量导入数据时的注意事项：在实际应用中，大批量的计算（如电信话单计费）用C语言程序做，这种基于主外键关系数据计算而得的批量数据（文本文件），可利用系统的自身功能函数（如Sybase的BCP命令）快速批量导入，在导入数据库表时，可先删除相应库表的索引，这有利于加快导入速度，减少导入时间。在导入后再重建索引以便优化查询。 <br />
（4）锁：锁是并行处理的重要机制，能保持数据并发的一致性，即按事务进行处理；系统利用锁，保证数据完整性。因此，我们避免不了死锁，但在设计时可以充分考虑如何避免长事务，减少排它锁时间，减少在事务中与用户的交互，杜绝让用户控制事务的长短；要避免批量数据同时执行，尤其是耗时并用到相同的数据表。锁的征用：一个表同时只能有一个排它锁，一个用户用时，其它用户在等待。若用户数增加，则Server的性能下降，出现&#8220;假死&#8221;现象。如何避免死锁呢？从页级锁到行级锁，减少了锁征用；给小表增加无效记录，从页级锁到行级锁没有影响，若在同一页内竞争有影响，可选择合适的聚族索引把数据分配到不同的页面；创建冗余表；保持事务简短；同一批处理应该没有网络交互。 <br />
（5）查询优化规则：在访问数据库表的数据(Access Data)时，要尽可能避免排序（Sort）、连接(Join)和相关子查询*作。经验告诉我们，在优化查询时，必须做到： <br />
① 尽可能少的行； <br />
② 避免排序或为尽可能少的行排序，若要做大量数据排序，最好将相关数据放在临时表中*作；用简单的键（列）排序，如整型或短字符串排序； <br />
③ 避免表内的相关子查询； <br />
④ 避免在Where子句中使用复杂的表达式或非起始的子字符串、用长字符串连接； <br />
⑤ 在Where子句中多使用&#8220;与&#8221;（And）连接，少使用&#8220;或&#8221;(Or)连接； <br />
⑥ 利用临时数据库。在查询多表、有多个连接、查询复杂、数据要过滤时，可以建临时表（索引）以减少I/O。但缺点是增加了空间开销。 <br />
除非每个列都有索引支持，否则在有连接的查询时分别找出两个动态索引，放在工作表中重新排序。 <br />
3 基本表扩展设计 <br />
基于第三范式设计的库表虽然有其优越性（见本文第一部分），然而在实际应用中有时不利于系统运行性能的优化：如需要部分数据时而要扫描整表，许多过程同时竞争同一数据，反复用相同行计算相同的结果，过程从多表获取数据时引发大量的连接*作，当数据来源于多表时的连接*作；这都消耗了磁盘I/O和CPU时间。 <br />
尤其在遇到下列情形时，我们要对基本表进行扩展设计：许多过程要频繁访问一个表、子集数据访问、重复计算和冗余数据，有时用户要求一些过程优先或低的响应时间。 <br />
如何避免这些不利因素呢？根据访问的频繁程度对相关表进行分割处理、存储冗余数据、存储衍生列、合并相关表处理，这些都是克服这些不利因素和优化系统运行的有效途径。 <br />
3.1 分割表或储存冗余数据 <br />
分割表分为水平分割表和垂直分割表两种。分割表增加了维护数据完整性的代价。 <br />
水平分割表：一种是当多个过程频繁访问数据表的不同行时，水平分割表，并消除新表中的冗余数据列；若个别过程要访问整个数据，则要用连接*作，这也无妨分割表；典型案例是电信话单按月分割存放。另一种是当主要过程要重复访问部分行时，最好将被重复访问的这些行单独形成子集表（冗余储存），这在不考虑磁盘空间开销时显得十分重要；但在分割表以后，增加了维护难度，要用触发器立即更新、或存储过程或应用代码批量更新，这也会增加额外的磁盘I/O开销。 <br />
垂直分割表（不破坏第三范式），一种是当多个过程频繁访问表的不同列时，可将表垂直分成几个表，减少磁盘I/O（每行的数据列少，每页存的数据行就多，相应占用的页就少），更新时不必考虑锁，没有冗余数据。缺点是要在插入或删除数据时要考虑数据的完整性，用存储过程维护。另一种是当主要过程反复访问部分列时，最好将这部分被频繁访问的列数据单独存为一个子集表（冗余储存），这在不考虑磁盘空间开销时显得十分重要；但这增加了重叠列的维护难度，要用触发器立即更新、或存储过程或应用代码批量更新，这也会增加额外的磁盘I/O开销。垂直分割表可以达到最大化利用Cache的目的。 <br />
总之，为主要过程分割表的方法适用于：各个过程需要表的不联结的子集，各个过程需要表的子集，访问频率高的主要过程不需要整表。在主要的、频繁访问的主表需要表的子集而其它主要频繁访问的过程需要整表时则产生冗余子集表。 <br />
注意，在分割表以后，要考虑重新建立索引。 <br />
3.2 存储衍生数据 <br />
对一些要做大量重复性计算的过程而言，若重复计算过程得到的结果相同（源列数据稳定，因此计算结果也不变），或计算牵扯多行数据需额外的磁盘I/O开销，或计算复杂需要大量的CPU时间，就考虑存储计算结果（冗余储存）。现予以分类说明： <br />
若在一行内重复计算，就在表内增加列存储结果。但若参与计算的列被更新时，必须要用触发器更新这个新列。 <br />
若对表按类进行重复计算，就增加新表（一般而言，存放类和结果两列就可以了）存储相关结果。但若参与计算的列被更新时，就必须要用触发器立即更新、或存储过程或应用代码批量更新这个新表。 <br />
若对多行进行重复性计算（如排名次），就在表内增加列存储结果。但若参与计算的列被更新时，必须要用触发器或存储过程更新这个新列。 <br />
总之，存储冗余数据有利于加快访问速度；但违反了第三范式，这会增加维护数据完整性的代价，必须用触发器立即更新、或存储过程或应用代码批量更新，以维护数据的完整性。 <br />
3.3 消除昂贵结合 <br />
对于频繁同时访问多表的一些主要过程，考虑在主表内存储冗余数据，即存储冗余列或衍生列（它不依赖于主键），但破坏了第三范式，也增加了维护难度。在源表的相关列发生变化时，必须要用触发器或存储过程更新这个冗余列。当主要过程总同时访问两个表时可以合并表，这样可以减少磁盘I/O*作，但破坏了第三范式，也增加了维护难度。对父子表和1：1关系表合并方法不同：合并父子表后，产生冗余表；合并1：1关系表后，在表内产生冗余数据。 <br />
4 数据库对象的放置策略 <br />
数据库对象的放置策略是均匀地把数据分布在系统的磁盘中，平衡I/O访问，避免I/O瓶颈。 <br />
⑴ 访问分散到不同的磁盘，即使用户数据尽可能跨越多个设备，多个I/O运转，避免I/O竞争，克服访问瓶颈；分别放置随机访问和连续访问数据。 <br />
⑵ 分离系统数据库I/O和应用数据库I/O。把系统审计表和临时库表放在不忙的磁盘上。 <br />
⑶ 把事务日志放在单独的磁盘上，减少磁盘I/O开销，这还有利于在障碍后恢复，提高了系统的安全性。 <br />
⑷ 把频繁访问的&#8220;活性&#8221;表放在不同的磁盘上；把频繁用的表、频繁做Join*作的表分别放在单独的磁盘上，甚至把把频繁访问的表的字段放在不同的磁盘上，把访问分散到不同的磁盘上，避免I/O争夺； <br />
⑸ 利用段分离频繁访问的表及其索引（非聚族的）、分离文本和图像数据。段的目的是平衡I/O，避免瓶颈，增加吞吐量，实现并行扫描，提高并发度，最大化磁盘的吞吐量。利用逻辑段功能，分别放置&#8220;活性&#8221;表及其非聚族索引以平衡I/O。当然最好利用系统的默认段。另外，利用段可以使备份和恢复数据更加灵活，使系统授权更加灵活。&nbsp;<br />
&nbsp;<br />
<img src ="http://www.blogjava.net/qq66921494/aggbug/235568.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qq66921494/" target="_blank">java民工</a> 2008-10-21 00:46 <a href="http://www.blogjava.net/qq66921494/archive/2008/10/21/235568.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>hibernate的缓存机制</title><link>http://www.blogjava.net/qq66921494/archive/2008/10/21/235567.html</link><dc:creator>java民工</dc:creator><author>java民工</author><pubDate>Mon, 20 Oct 2008 16:46:00 GMT</pubDate><guid>http://www.blogjava.net/qq66921494/archive/2008/10/21/235567.html</guid><wfw:comment>http://www.blogjava.net/qq66921494/comments/235567.html</wfw:comment><comments>http://www.blogjava.net/qq66921494/archive/2008/10/21/235567.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qq66921494/comments/commentRss/235567.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qq66921494/services/trackbacks/235567.html</trackback:ping><description><![CDATA[<p>Hibernate缓存机制缓存是介于应用程序和物理数据源之间，其作用是为了降低应用程序对物理数据源访问的频次，从而提高了应用的运行性能。缓存内的数据是对物理数据源中的数据的复制，应用程序在运行时从缓存读写数据，在特定的时刻或事件会同步缓存和物理数据源的数据。<br />
　　缓存的介质一般是内存，所以读写速度很快。但如果缓存中存放的数据量非常大时，也会用硬盘作为缓存介质。缓存的实现不仅仅要考虑存储的介质，还要考虑到管理缓存的并发访问和缓存数据的生命周期。 <br />
　　Hibernate的缓存包括Session的缓存和SessionFactory的缓存，其中SessionFactory的缓存又可以分为两类：内置缓存和外置缓存。Session的缓存是内置的，不能被卸载，也被称为Hibernate的第一级缓存。SessionFactory的内置缓存和Session的缓存在实现方式上比较相似，前者是SessionFactory对象的一些集合属性包含的数据，后者是指Session的一些集合属性包含的数据。SessionFactory的内置缓存中存放了映射元数据和预定义SQL语句，映射元数据是映射文件中数据的拷贝，而预定义SQL语句是在Hibernate初始化阶段根据映射元数据推导出来，SessionFactory的内置缓存是只读的，应用程序不能修改缓存中的映射元数据和预定义SQL语句，因此SessionFactory不需要进行内置缓存与映射文件的同步。SessionFactory的外置缓存是一个可配置的插件。在默认情况下，SessionFactory不会启用这个插件。外置缓存的数据是数据库数据的拷贝，外置缓存的介质可以是内存或者硬盘。SessionFactory的外置缓存也被称为Hibernate的第二级缓存。</p>
<p>　　Hibernate的这两级缓存都位于持久化层，存放的都是数据库数据的拷贝，那么它们之间的区别是什么呢？为了理解二者的区别，需要深入理解持久化层的缓存的两个特性：缓存的范围和缓存的并发访问策略。</p>
<p>持久化层的缓存的范围</p>
<p>　　缓存的范围决定了缓存的生命周期以及可以被谁访问。缓存的范围分为三类。</p>
<p>　　1 事务范围：缓存只能被当前事务访问。缓存的生命周期依赖于事务的生命周期，当事务结束时，缓存也就结束生命周期。在此范围下，缓存的介质是内存。事务可以是数据库事务或者应用事务，每个事务都有独自的缓存，缓存内的数据通常采用相互关联的的对象形式。</p>
<p>　　2 进程范围：缓存被进程内的所有事务共享。这些事务有可能是并发访问缓存，因此必须对缓存采取必要的事务隔离机制。缓存的生命周期依赖于进程的生命周期，进程结束时，缓存也就结束了生命周期。进程范围的缓存可能会存放大量的数据，所以存放的介质可以是内存或硬盘。缓存内的数据既可以是相互关联的对象形式也可以是对象的松散数据形式。松散的对象数据形式有点类似于对象的序列化数据，但是对象分解为松散的算法比对象序列化的算法要求更快。</p>
<p>　　3 集群范围：在集群环境中，缓存被一个机器或者多个机器的进程共享。缓存中的数据被复制到集群环境中的每个进程节点，进程间通过远程通信来保证缓存中的数据的一致性，缓存中的数据通常采用对象的松散数据形式。</p>
<p>　　对大多数应用来说，应该慎重地考虑是否需要使用集群范围的缓存，因为访问的速度不一定会比直接访问数据库数据的速度快多少。</p>
<p>　　持久化层可以提供多种范围的缓存。如果在事务范围的缓存中没有查到相应的数据，还可以到进程范围或集群范围的缓存内查询，如果还是没有查到，那么只有到数据库中查询。事务范围的缓存是持久化层的第一级缓存，通常它是必需的；进程范围或集群范围的缓存是持久化层的第二级缓存，通常是可选的。</p>
<p>持久化层的缓存的并发访问策略</p>
<p>　　当多个并发的事务同时访问持久化层的缓存的相同数据时，会引起并发问题，必须采用必要的事务隔离措施。</p>
<p>　　在进程范围或集群范围的缓存，即第二级缓存，会出现并发问题。因此可以设定以下四种类型的并发访问策略，每一种策略对应一种事务隔离级别。</p>
<p>　　事务型：仅仅在受管理环境中适用。它提供了Repeatable Read事务隔离级别。对于经常被读但很少修改的数据，可以采用这种隔离类型，因为它可以防止脏读和不可重复读这类的并发问题。</p>
<p>　　读写型：提供了Read Committed事务隔离级别。仅仅在非集群的环境中适用。对于经常被读但很少修改的数据，可以采用这种隔离类型，因为它可以防止脏读这类的并发问题。</p>
<p>　　非严格读写型：不保证缓存与数据库中数据的一致性。如果存在两个事务同时访问缓存中相同数据的可能，必须为该数据配置一个很短的数据过期时间，从而尽量避免脏读。对于极少被修改，并且允许偶尔脏读的数据，可以采用这种并发访问策略。</p>
<p>　　只读型：对于从来不会修改的数据，如参考数据，可以使用这种并发访问策略。</p>
<p>　　事务型并发访问策略是事务隔离级别最高，只读型的隔离级别最低。事务隔离级别越高，并发性能就越低。</p>
<p>什么样的数据适合存放到第二级缓存中？</p>
<p>1 很少被修改的数据</p>
<p>2 不是很重要的数据，允许出现偶尔并发的数据</p>
<p>3 不会被并发访问的数据</p>
<p>4 参考数据</p>
<p>不适合存放到第二级缓存的数据？</p>
<p>1 经常被修改的数据</p>
<p>2 财务数据，绝对不允许出现并发</p>
<p>3 与其他应用共享的数据。</p>
<p>Hibernate的二级缓存</p>
<p>　　如前所述，Hibernate提供了两级缓存，第一级是Session的缓存。由于Session对象的生命周期通常对应一个数据库事务或者一个应用事务，因此它的缓存是事务范围的缓存。第一级缓存是必需的，不允许而且事实上也无法比卸除。在第一级缓存中，持久化类的每个实例都具有唯一的OID。</p>
<p>　　第二级缓存是一个可插拔的的缓存插件，它是由SessionFactory负责管理。由于SessionFactory对象的生命周期和应用程序的整个过程对应，因此第二级缓存是进程范围或者集群范围的缓存。这个缓存中存放的对象的松散数据。第二级对象有可能出现并发问题，因此需要采用适当的并发访问策略，该策略为被缓存的数据提供了事务隔离级别。缓存适配器用于把具体的缓存实现软件与Hibernate集成。第二级缓存是可选的，可以在每个类或每个集合的粒度上配置第二级缓存。</p>
<p>Hibernate的二级缓存策略的一般过程如下：</p>
<p>1) 条件查询的时候，总是发出一条select * from table_name where &#8230;. （选择所有字段）这样的SQL语句查询数据库，一次获得所有的数据对象。</p>
<p>2) 把获得的所有数据对象根据ID放入到第二级缓存中。</p>
<p>3) 当Hibernate根据ID访问数据对象的时候，首先从Session一级缓存中查；查不到，如果配置了二级缓存，那么从二级缓存中查；查不到，再查询数据库，把结果按照ID放入到缓存。</p>
<p>4) 删除、更新、增加数据的时候，同时更新缓存。</p>
<p>　　Hibernate的二级缓存策略，是针对于ID查询的缓存策略，对于条件查询则毫无作用。为此，Hibernate提供了针对条件查询的Query缓存。</p>
<p>Hibernate的Query缓存策略的过程如下：</p>
<p>1) Hibernate首先根据这些信息组成一个Query Key，Query Key包括条件查询的请求一般信息：SQL, SQL需要的参数，记录范围（起始位置rowStart，最大记录个数maxRows)，等。</p>
<p>2) Hibernate根据这个Query Key到Query缓存中查找对应的结果列表。如果存在，那么返回这个结果列表；如果不存在，查询数据库，获取结果列表，把整个结果列表根据Query Key放入到Query缓存中。</p>
<p>3) Query Key中的SQL涉及到一些表名，如果这些表的任何数据发生修改、删除、增加等操作，这些相关的Query Key都要从缓存中清空<br />
&nbsp;<br />
</p>
<img src ="http://www.blogjava.net/qq66921494/aggbug/235567.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qq66921494/" target="_blank">java民工</a> 2008-10-21 00:46 <a href="http://www.blogjava.net/qq66921494/archive/2008/10/21/235567.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于log4j配置文档详解</title><link>http://www.blogjava.net/qq66921494/archive/2008/10/16/234558.html</link><dc:creator>java民工</dc:creator><author>java民工</author><pubDate>Wed, 15 Oct 2008 16:23:00 GMT</pubDate><guid>http://www.blogjava.net/qq66921494/archive/2008/10/16/234558.html</guid><wfw:comment>http://www.blogjava.net/qq66921494/comments/234558.html</wfw:comment><comments>http://www.blogjava.net/qq66921494/archive/2008/10/16/234558.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qq66921494/comments/commentRss/234558.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qq66921494/services/trackbacks/234558.html</trackback:ping><description><![CDATA[<div class="content" style="overflow: auto" minmax_bound="true">log4j.properties 使用<br minmax_bound="true" />
一.参数意义说明<br minmax_bound="true" />
输出级别的种类<br minmax_bound="true" />
ERROR、WARN、INFO、DEBUG<br minmax_bound="true" />
ERROR 为严重错误 主要是程序的错误<br minmax_bound="true" />
WARN 为一般警告，比如session丢失<br minmax_bound="true" />
INFO 为一般要显示的信息，比如登录登出<br minmax_bound="true" />
DEBUG 为程序的调试信息<br minmax_bound="true" />
配置日志信息输出目的地<br minmax_bound="true" />
log4j.appender.appenderName = fully.qualified.name.of.appender.class<br minmax_bound="true" />
1.org.apache.log4j.ConsoleAppender（控制台）<br minmax_bound="true" />
2.org.apache.log4j.FileAppender（文件）<br minmax_bound="true" />
3.org.apache.log4j.DailyRollingFileAppender（每天产生一个日志文件）<br minmax_bound="true" />
4.org.apache.log4j.RollingFileAppender（文件大小到达指定尺寸的时候产生一个新的文件）<br minmax_bound="true" />
5.org.apache.log4j.WriterAppender（将日志信息以流格式发送到任意指定的地方）<br minmax_bound="true" />
配置日志信息的格式<br minmax_bound="true" />
log4j.appender.appenderName.layout = fully.qualified.name.of.layout.class<br minmax_bound="true" />
1.org.apache.log4j.HTMLLayout（以HTML表格形式布局），<br minmax_bound="true" />
2.org.apache.log4j.PatternLayout（可以灵活地指定布局模式），<br minmax_bound="true" />
3.org.apache.log4j.SimpleLayout（包含日志信息的级别和信息字符串），<br minmax_bound="true" />
4.org.apache.log4j.TTCCLayout（包含日志产生的时间、线程、类别等等信息）<br minmax_bound="true" />
控制台选项<br minmax_bound="true" />
Threshold=DEBUG:指定日志消息的输出最低层次。<br minmax_bound="true" />
ImmediateFlush=true:默认值是true,意谓着所有的消息都会被立即输出。<br minmax_bound="true" />
Target=System.err：默认情况下是：System.out,指定输出控制台<br minmax_bound="true" />
FileAppender 选项<br minmax_bound="true" />
Threshold=DEBUF:指定日志消息的输出最低层次。<br minmax_bound="true" />
ImmediateFlush=true:默认值是true,意谓着所有的消息都会被立即输出。<br minmax_bound="true" />
File=mylog.txt:指定消息输出到mylog.txt文件。<br minmax_bound="true" />
Append=false:默认值是true,即将消息增加到指定文件中，false指将消息覆盖指定的文件内容。<br minmax_bound="true" />
RollingFileAppender 选项<br minmax_bound="true" />
Threshold=DEBUG:指定日志消息的输出最低层次。<br minmax_bound="true" />
ImmediateFlush=true:默认值是true,意谓着所有的消息都会被立即输出。<br minmax_bound="true" />
File=mylog.txt:指定消息输出到mylog.txt文件。<br minmax_bound="true" />
Append=false:默认值是true,即将消息增加到指定文件中，false指将消息覆盖指定的文件内容。<br minmax_bound="true" />
MaxFileSize=100KB: 后缀可以是KB, MB 或者是 GB. 在日志文件到达该大小时，将会自动滚动，即将原来的内容移到mylog.log.1文件。<br minmax_bound="true" />
MaxBackupIndex=2:指定可以产生的滚动文件的最大数。<br minmax_bound="true" />
log4j.appender.A1.layout.ConversionPattern=%-4r %-5p %d{yyyy-MM-dd HH:mm:ssS} %c %m%n<br minmax_bound="true" />
日志信息格式中几个符号所代表的含义：<br minmax_bound="true" />
&nbsp;-X号: X信息输出时左对齐；<br minmax_bound="true" />
&nbsp;%p: 输出日志信息优先级，即DEBUG，INFO，WARN，ERROR，FATAL,<br minmax_bound="true" />
&nbsp;%d: 输出日志时间点的日期或时间，默认格式为ISO8601，也可以在其后指定格式，比如：%d{yyy MMM dd HH:mm:ss,SSS}，输出类似：2002年10月18日 22：10：28，921<br minmax_bound="true" />
&nbsp;%r: 输出自应用启动到输出该log信息耗费的毫秒数<br minmax_bound="true" />
&nbsp;%c: 输出日志信息所属的类目，通常就是所在类的全名<br minmax_bound="true" />
&nbsp;%t: 输出产生该日志事件的线程名<br minmax_bound="true" />
&nbsp;%l: 输出日志事件的发生位置，相当于%C.%M(%F:%L)的组合,包括类目名、发生的线程，以及在代码中的行数。举例：Testlog4.main (TestLog4.java:10)<br minmax_bound="true" />
&nbsp;%x: 输出和当前线程相关联的NDC(嵌套诊断环境),尤其用到像java servlets这样的多客户多线程的应用中。<br minmax_bound="true" />
&nbsp;%%: 输出一个"%"字符<br minmax_bound="true" />
&nbsp;%F: 输出日志消息产生时所在的文件名称<br minmax_bound="true" />
&nbsp;%L: 输出代码中的行号<br minmax_bound="true" />
&nbsp;%m: 输出代码中指定的消息,产生的日志具体信息<br minmax_bound="true" />
&nbsp;%n: 输出一个回车换行符，Windows平台为"\r\n"，Unix平台为"\n"输出日志信息换行<br minmax_bound="true" />
&nbsp;可以在%与模式字符之间加上修饰符来控制其最小宽度、最大宽度、和文本的对齐方式。如：<br minmax_bound="true" />
&nbsp;1)%20c：指定输出category的名称，最小的宽度是20，如果category的名称小于20的话，默认的情况下右对齐。<br minmax_bound="true" />
&nbsp;2)%-20c:指定输出category的名称，最小的宽度是20，如果category的名称小于20的话，"-"号指定左对齐。<br minmax_bound="true" />
&nbsp;3)%.30c:指定输出category的名称，最大的宽度是30，如果category的名称大于30的话，就会将左边多出的字符截掉，但小于30的话也不会有空格。<br minmax_bound="true" />
&nbsp;4)%20.30c:如果category的名称小于20就补空格，并且右对齐，如果其名称长于30字符，就从左边较远输出的字符截掉。<br minmax_bound="true" />
二.文件配置Sample1<br minmax_bound="true" />
log4j.rootLogger=DEBUG,A1,R<br minmax_bound="true" />
#log4j.rootLogger=INFO,A1,R<br minmax_bound="true" />
# ConsoleAppender 输出<br minmax_bound="true" />
log4j.appender.A1=org.apache.log4j.ConsoleAppender<br minmax_bound="true" />
log4j.appender.A1.layout=org.apache.log4j.PatternLayout<br minmax_bound="true" />
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n<br minmax_bound="true" />
# File 输出 一天一个文件,输出路径可以定制,一般在根路径下<br minmax_bound="true" />
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender<br minmax_bound="true" />
log4j.appender.R.File=blog_log.txt<br minmax_bound="true" />
log4j.appender.R.MaxFileSize=500KB<br minmax_bound="true" />
log4j.appender.R.MaxBackupIndex=10<br minmax_bound="true" />
log4j.appender.R.layout=org.apache.log4j.PatternLayout<br minmax_bound="true" />
log4j.appender.R.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c] [%p] - %m%n<br minmax_bound="true" />
文件配置Sample2<br minmax_bound="true" />
下面给出的Log4J配置文件实现了输出到控制台，文件，回滚文件，发送日志邮件，输出到数据库日志表，自定义标签等全套功能。<br minmax_bound="true" />
log4j.rootLogger=DEBUG,CONSOLE,A1,im <br minmax_bound="true" />
#DEBUG,CONSOLE,FILE,ROLLING_FILE,MAIL,DATABASE<br minmax_bound="true" />
log4j.addivity.org.apache=true<br minmax_bound="true" />
################### <br minmax_bound="true" />
# Console Appender <br minmax_bound="true" />
################### <br minmax_bound="true" />
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender <br minmax_bound="true" />
log4j.appender.Threshold=DEBUG <br minmax_bound="true" />
log4j.appender.CONSOLE.Target=System.out <br minmax_bound="true" />
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout <br minmax_bound="true" />
log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n <br minmax_bound="true" />
#log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n<br minmax_bound="true" />
##################### <br minmax_bound="true" />
# File Appender <br minmax_bound="true" />
##################### <br minmax_bound="true" />
log4j.appender.FILE=org.apache.log4j.FileAppender <br minmax_bound="true" />
log4j.appender.FILE.File=file.log <br minmax_bound="true" />
log4j.appender.FILE.Append=false <br minmax_bound="true" />
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout <br minmax_bound="true" />
log4j.appender.FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n <br minmax_bound="true" />
# Use this layout for LogFactor 5 analysis<br minmax_bound="true" />
######################## <br minmax_bound="true" />
# Rolling File <br minmax_bound="true" />
######################## <br minmax_bound="true" />
log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender <br minmax_bound="true" />
log4j.appender.ROLLING_FILE.Threshold=ERROR <br minmax_bound="true" />
log4j.appender.ROLLING_FILE.File=rolling.log <br minmax_bound="true" />
log4j.appender.ROLLING_FILE.Append=true <br minmax_bound="true" />
log4j.appender.ROLLING_FILE.MaxFileSize=10KB <br minmax_bound="true" />
log4j.appender.ROLLING_FILE.MaxBackupIndex=1 <br minmax_bound="true" />
log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout <br minmax_bound="true" />
log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n<br minmax_bound="true" />
#################### <br minmax_bound="true" />
# Socket Appender <br minmax_bound="true" />
#################### <br minmax_bound="true" />
log4j.appender.SOCKET=org.apache.log4j.RollingFileAppender <br minmax_bound="true" />
log4j.appender.SOCKET.RemoteHost=localhost <br minmax_bound="true" />
log4j.appender.SOCKET.Port=5001 <br minmax_bound="true" />
log4j.appender.SOCKET.LocationInfo=true <br minmax_bound="true" />
# Set up for Log Facter 5 <br minmax_bound="true" />
log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout <br minmax_bound="true" />
log4j.appender.SOCET.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD]%n%c[CATEGORY]%n%m[MESSAGE]%n%n<br minmax_bound="true" />
######################## <br minmax_bound="true" />
# Log Factor 5 Appender <br minmax_bound="true" />
######################## <br minmax_bound="true" />
log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender <br minmax_bound="true" />
log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000<br minmax_bound="true" />
######################## <br minmax_bound="true" />
# SMTP Appender <br minmax_bound="true" />
####################### <br minmax_bound="true" />
log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender <br minmax_bound="true" />
log4j.appender.MAIL.Threshold=FATAL <br minmax_bound="true" />
log4j.appender.MAIL.BufferSize=10 <br minmax_bound="true" />
<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#108;&#111;&#103;&#52;&#106;&#46;&#97;&#112;&#112;&#101;&#110;&#100;&#101;&#114;&#46;&#77;&#65;&#73;&#76;&#46;&#70;&#114;&#111;&#109;&#61;&#99;&#104;&#101;&#110;&#121;&#108;&#64;&#121;&#101;&#113;&#105;&#97;&#110;&#103;&#119;&#101;&#105;&#46;&#99;&#111;&#109;" minmax_bound="true">log4j.appender.MAIL.From=chenyl@yeqiangwei.com</a> <br minmax_bound="true" />
log4j.appender.MAIL.SMTPHost=mail.hollycrm.com <br minmax_bound="true" />
log4j.appender.MAIL.Subject=Log4J Message <br minmax_bound="true" />
<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#108;&#111;&#103;&#52;&#106;&#46;&#97;&#112;&#112;&#101;&#110;&#100;&#101;&#114;&#46;&#77;&#65;&#73;&#76;&#46;&#84;&#111;&#61;&#99;&#104;&#101;&#110;&#121;&#108;&#64;&#121;&#101;&#113;&#105;&#97;&#110;&#103;&#119;&#101;&#105;&#46;&#99;&#111;&#109;" minmax_bound="true">log4j.appender.MAIL.To=chenyl@yeqiangwei.com</a> <br minmax_bound="true" />
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout <br minmax_bound="true" />
log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n<br minmax_bound="true" />
######################## <br minmax_bound="true" />
# JDBC Appender <br minmax_bound="true" />
####################### <br minmax_bound="true" />
log4j.appender.DATABASE=org.apache.log4j.jdbc.JDBCAppender <br minmax_bound="true" />
log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306/test <br minmax_bound="true" />
log4j.appender.DATABASE.driver=com.mysql.jdbc.Driver <br minmax_bound="true" />
log4j.appender.DATABASE.user=root <br minmax_bound="true" />
log4j.appender.DATABASE.password= <br minmax_bound="true" />
log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n') <br minmax_bound="true" />
log4j.appender.DATABASE.layout=org.apache.log4j.PatternLayout <br minmax_bound="true" />
log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n<br minmax_bound="true" />
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender <br minmax_bound="true" />
log4j.appender.A1.File=SampleMessages.log4j <br minmax_bound="true" />
log4j.appender.A1.DatePattern=yyyyMMdd-HH'.log4j' <br minmax_bound="true" />
log4j.appender.A1.layout=org.apache.log4j.xml.XMLLayout<br minmax_bound="true" />
################### <br minmax_bound="true" />
#自定义Appender <br minmax_bound="true" />
################### <br minmax_bound="true" />
log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppender<br minmax_bound="true" />
log4j.appender.im.host = mail.cybercorlin.net <br minmax_bound="true" />
log4j.appender.im.username = username <br minmax_bound="true" />
log4j.appender.im.password = password <br minmax_bound="true" />
log4j.appender.im.recipient = <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#99;&#111;&#114;&#108;&#105;&#110;&#64;&#121;&#101;&#113;&#105;&#97;&#110;&#103;&#119;&#101;&#105;&#46;&#99;&#111;&#109;" minmax_bound="true">corlin@yeqiangwei.com</a> <br minmax_bound="true" />
log4j.appender.im.layout=org.apache.log4j.PatternLayout <br minmax_bound="true" />
log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n<br minmax_bound="true" />
三.高级使用<br minmax_bound="true" />
实验目的：<br minmax_bound="true" />
&nbsp;1.把FATAL级错误写入2000NT日志<br minmax_bound="true" />
&nbsp;2. WARN，ERROR，FATAL级错误发送email通知管理员<br minmax_bound="true" />
&nbsp;3.其他级别的错误直接在后台输出<br minmax_bound="true" />
实验步骤：<br minmax_bound="true" />
&nbsp;输出到2000NT日志<br minmax_bound="true" />
&nbsp;1.把Log4j压缩包里的NTEventLogAppender.dll拷到WINNT\SYSTEM32目录下<br minmax_bound="true" />
&nbsp;2.写配置文件log4j.properties<br minmax_bound="true" />
# 在2000系统日志输出<br minmax_bound="true" />
&nbsp;log4j.logger.NTlog=FATAL, A8<br minmax_bound="true" />
&nbsp;# APPENDER A8<br minmax_bound="true" />
&nbsp;log4j.appender.A8=org.apache.log4j.nt.NTEventLogAppender<br minmax_bound="true" />
&nbsp;log4j.appender.A8.Source=JavaTest<br minmax_bound="true" />
&nbsp;log4j.appender.A8.layout=org.apache.log4j.PatternLayout<br minmax_bound="true" />
&nbsp;log4j.appender.A8.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n<br minmax_bound="true" />
3.调用代码：<br minmax_bound="true" />
&nbsp;Logger logger2 = Logger.getLogger("NTlog"); //要和配置文件中设置的名字相同<br minmax_bound="true" />
&nbsp;logger2.debug("debug!!!");<br minmax_bound="true" />
&nbsp;logger2.info("info!!!");<br minmax_bound="true" />
&nbsp;logger2.warn("warn!!!");<br minmax_bound="true" />
&nbsp;logger2.error("error!!!");<br minmax_bound="true" />
&nbsp;//只有这个错误才会写入2000日志<br minmax_bound="true" />
&nbsp;logger2.fatal("fatal!!!");<br minmax_bound="true" />
发送email通知管理员：<br minmax_bound="true" />
&nbsp;1. 首先下载JavaMail和JAF, <br minmax_bound="true" />
&nbsp; <a href="http://java.sun.com/j2ee/ja/javamail/index.html" minmax_bound="true">http://java.sun.com/j2ee/ja/javamail/index.html</a> <br minmax_bound="true" />
&nbsp; <a href="http://java.sun.com/beans/glasgow/jaf.html" minmax_bound="true">http://java.sun.com/beans/glasgow/jaf.html</a> <br minmax_bound="true" />
&nbsp;在项目中引用mail.jar和activation.jar。<br minmax_bound="true" />
&nbsp;2. 写配置文件<br minmax_bound="true" />
&nbsp;# 将日志发送到email<br minmax_bound="true" />
&nbsp;log4j.logger.MailLog=WARN,A5<br minmax_bound="true" />
&nbsp;#&nbsp; APPENDER A5<br minmax_bound="true" />
&nbsp;log4j.appender.A5=org.apache.log4j.net.SMTPAppender<br minmax_bound="true" />
&nbsp;log4j.appender.A5.BufferSize=5<br minmax_bound="true" />
&nbsp;<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#108;&#111;&#103;&#52;&#106;&#46;&#97;&#112;&#112;&#101;&#110;&#100;&#101;&#114;&#46;&#65;&#53;&#46;&#84;&#111;&#61;&#99;&#104;&#117;&#110;&#106;&#105;&#101;&#64;&#121;&#101;&#113;&#105;&#97;&#110;&#103;&#119;&#101;&#105;&#46;&#99;&#111;&#109;" minmax_bound="true">log4j.appender.A5.To=chunjie@yeqiangwei.com</a> <br minmax_bound="true" />
&nbsp;<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#108;&#111;&#103;&#52;&#106;&#46;&#97;&#112;&#112;&#101;&#110;&#100;&#101;&#114;&#46;&#65;&#53;&#46;&#70;&#114;&#111;&#109;&#61;&#101;&#114;&#114;&#111;&#114;&#64;&#121;&#101;&#113;&#105;&#97;&#110;&#103;&#119;&#101;&#105;&#46;&#99;&#111;&#109;" minmax_bound="true">log4j.appender.A5.From=error@yeqiangwei.com</a> <br minmax_bound="true" />
&nbsp;log4j.appender.A5.Subject=ErrorLog<br minmax_bound="true" />
&nbsp;log4j.appender.A5.SMTPHost=smtp.263.net<br minmax_bound="true" />
&nbsp;log4j.appender.A5.layout=org.apache.log4j.PatternLayout<br minmax_bound="true" />
&nbsp;log4j.appender.A5.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n<br minmax_bound="true" />
&nbsp;3.调用代码：<br minmax_bound="true" />
&nbsp;//把日志发送到mail<br minmax_bound="true" />
&nbsp;Logger logger3 = Logger.getLogger("MailLog");<br minmax_bound="true" />
&nbsp;logger3.warn("warn!!!");<br minmax_bound="true" />
&nbsp;logger3.error("error!!!");<br minmax_bound="true" />
&nbsp;logger3.fatal("fatal!!!");<br minmax_bound="true" />
在后台输出所有类别的错误：<br minmax_bound="true" />
&nbsp;1. 写配置文件<br minmax_bound="true" />
&nbsp;# 在后台输出<br minmax_bound="true" />
&nbsp;log4j.logger.console=DEBUG, A1<br minmax_bound="true" />
&nbsp;# APPENDER A1<br minmax_bound="true" />
&nbsp;log4j.appender.A1=org.apache.log4j.ConsoleAppender<br minmax_bound="true" />
&nbsp;log4j.appender.A1.layout=org.apache.log4j.PatternLayout<br minmax_bound="true" />
&nbsp;log4j.appender.A1.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n<br minmax_bound="true" />
&nbsp;2．调用代码<br minmax_bound="true" />
&nbsp;Logger logger1 = Logger.getLogger("console");<br minmax_bound="true" />
&nbsp;logger1.debug("debug!!!");<br minmax_bound="true" />
&nbsp;logger1.info("info!!!");<br minmax_bound="true" />
&nbsp;logger1.warn("warn!!!");<br minmax_bound="true" />
&nbsp;logger1.error("error!!!");<br minmax_bound="true" />
&nbsp;logger1.fatal("fatal!!!");<br minmax_bound="true" />
--------------------------------------------------------------------<br minmax_bound="true" />
&nbsp;全部配置文件：log4j.properties<br minmax_bound="true" />
&nbsp;# 在后台输出<br minmax_bound="true" />
&nbsp;log4j.logger.console=DEBUG, A1<br minmax_bound="true" />
&nbsp;# APPENDER A1<br minmax_bound="true" />
&nbsp;log4j.appender.A1=org.apache.log4j.ConsoleAppender<br minmax_bound="true" />
&nbsp;log4j.appender.A1.layout=org.apache.log4j.PatternLayout<br minmax_bound="true" />
&nbsp;log4j.appender.A1.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n<br minmax_bound="true" />
# 在2000系统日志输出<br minmax_bound="true" />
&nbsp;log4j.logger.NTlog=FATAL, A8<br minmax_bound="true" />
&nbsp;# APPENDER A8<br minmax_bound="true" />
&nbsp;log4j.appender.A8=org.apache.log4j.nt.NTEventLogAppender<br minmax_bound="true" />
&nbsp;log4j.appender.A8.Source=JavaTest<br minmax_bound="true" />
&nbsp;log4j.appender.A8.layout=org.apache.log4j.PatternLayout<br minmax_bound="true" />
&nbsp;log4j.appender.A8.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n<br minmax_bound="true" />
# 将日志发送到email<br minmax_bound="true" />
&nbsp;log4j.logger.MailLog=WARN,A5<br minmax_bound="true" />
&nbsp;#&nbsp; APPENDER A5<br minmax_bound="true" />
&nbsp;log4j.appender.A5=org.apache.log4j.net.SMTPAppender<br minmax_bound="true" />
&nbsp;log4j.appender.A5.BufferSize=5<br minmax_bound="true" />
&nbsp;<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#108;&#111;&#103;&#52;&#106;&#46;&#97;&#112;&#112;&#101;&#110;&#100;&#101;&#114;&#46;&#65;&#53;&#46;&#84;&#111;&#61;&#99;&#104;&#117;&#110;&#106;&#105;&#101;&#64;&#121;&#101;&#113;&#105;&#97;&#110;&#103;&#119;&#101;&#105;&#46;&#99;&#111;&#109;" minmax_bound="true">log4j.appender.A5.To=chunjie@yeqiangwei.com</a> <br minmax_bound="true" />
&nbsp;<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#108;&#111;&#103;&#52;&#106;&#46;&#97;&#112;&#112;&#101;&#110;&#100;&#101;&#114;&#46;&#65;&#53;&#46;&#70;&#114;&#111;&#109;&#61;&#101;&#114;&#114;&#111;&#114;&#64;&#121;&#101;&#113;&#105;&#97;&#110;&#103;&#119;&#101;&#105;&#46;&#99;&#111;&#109;" minmax_bound="true">log4j.appender.A5.From=error@yeqiangwei.com</a> <br minmax_bound="true" />
&nbsp;log4j.appender.A5.Subject=ErrorLog<br minmax_bound="true" />
&nbsp;log4j.appender.A5.SMTPHost=smtp.263.net<br minmax_bound="true" />
&nbsp;log4j.appender.A5.layout=org.apache.log4j.PatternLayout<br minmax_bound="true" />
&nbsp;log4j.appender.A5.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n<br minmax_bound="true" />
全部代码：Log4jTest.java<br minmax_bound="true" />
&nbsp; <br minmax_bound="true" />
/* <br minmax_bound="true" />
&nbsp; * 创建日期 2003-11-13 <br minmax_bound="true" />
&nbsp; */ <br minmax_bound="true" />
&nbsp;package edu.bcu.Bean; <br minmax_bound="true" />
&nbsp;import org.apache.log4j.*; <br minmax_bound="true" />
&nbsp;//import org.apache.log4j.nt.*; <br minmax_bound="true" />
&nbsp;//import org.apache.log4j.net.*; <br minmax_bound="true" />
&nbsp;/** <br minmax_bound="true" />
&nbsp; * @author yanxu <br minmax_bound="true" />
&nbsp; */ <br minmax_bound="true" />
&nbsp;public class Log4jTest <br minmax_bound="true" />
&nbsp;{ <br minmax_bound="true" />
&nbsp; public static void main(String args[]) <br minmax_bound="true" />
&nbsp; { <br minmax_bound="true" />
&nbsp;&nbsp; PropertyConfigurator.configure("log4j.properties"); <br minmax_bound="true" />
&nbsp;&nbsp; //在后台输出 <br minmax_bound="true" />
&nbsp;&nbsp; Logger logger1 = Logger.getLogger("console"); <br minmax_bound="true" />
&nbsp;&nbsp; logger1.debug("debug!!!"); <br minmax_bound="true" />
&nbsp;&nbsp; logger1.info("info!!!"); <br minmax_bound="true" />
&nbsp;&nbsp; logger1.warn("warn!!!"); <br minmax_bound="true" />
&nbsp;&nbsp; logger1.error("error!!!"); <br minmax_bound="true" />
&nbsp;&nbsp; logger1.fatal("fatal!!!");<br minmax_bound="true" />
//在NT系统日志输出 <br minmax_bound="true" />
&nbsp;&nbsp; Logger logger2 = Logger.getLogger("NTlog"); <br minmax_bound="true" />
&nbsp;&nbsp; //NTEventLogAppender nla = new NTEventLogAppender(); <br minmax_bound="true" />
&nbsp;&nbsp; logger2.debug("debug!!!"); <br minmax_bound="true" />
&nbsp;&nbsp; logger2.info("info!!!"); <br minmax_bound="true" />
&nbsp;&nbsp; logger2.warn("warn!!!"); <br minmax_bound="true" />
&nbsp;&nbsp; logger2.error("error!!!"); <br minmax_bound="true" />
&nbsp;&nbsp; //只有这个错误才会写入2000日志 <br minmax_bound="true" />
&nbsp;&nbsp; logger2.fatal("fatal!!!");<br minmax_bound="true" />
//把日志发送到mail <br minmax_bound="true" />
&nbsp;&nbsp; Logger logger3 = Logger.getLogger("MailLog"); <br minmax_bound="true" />
&nbsp;&nbsp; //SMTPAppender sa = new SMTPAppender(); <br minmax_bound="true" />
&nbsp;&nbsp; logger3.warn("warn!!!"); <br minmax_bound="true" />
&nbsp;&nbsp; logger3.error("error!!!"); <br minmax_bound="true" />
&nbsp;&nbsp; logger3.fatal("fatal!!!"); <br minmax_bound="true" />
&nbsp; } <br minmax_bound="true" />
&nbsp;} </div>
<img src ="http://www.blogjava.net/qq66921494/aggbug/234558.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qq66921494/" target="_blank">java民工</a> 2008-10-16 00:23 <a href="http://www.blogjava.net/qq66921494/archive/2008/10/16/234558.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于eclipse中编码格式不能保存的问题</title><link>http://www.blogjava.net/qq66921494/archive/2008/10/16/234557.html</link><dc:creator>java民工</dc:creator><author>java民工</author><pubDate>Wed, 15 Oct 2008 16:21:00 GMT</pubDate><guid>http://www.blogjava.net/qq66921494/archive/2008/10/16/234557.html</guid><wfw:comment>http://www.blogjava.net/qq66921494/comments/234557.html</wfw:comment><comments>http://www.blogjava.net/qq66921494/archive/2008/10/16/234557.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/qq66921494/comments/commentRss/234557.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/qq66921494/services/trackbacks/234557.html</trackback:ping><description><![CDATA[<div forimg="1">Windows--&gt;preference</div>
<div forimg="1">General-&gt;Content Type </div>
<div forimg="1">选择对应的文件类型，然后下面修改为GBK或者gb2312,点update应该就可以了！</div>
<img src ="http://www.blogjava.net/qq66921494/aggbug/234557.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/qq66921494/" target="_blank">java民工</a> 2008-10-16 00:21 <a href="http://www.blogjava.net/qq66921494/archive/2008/10/16/234557.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>