log4j的使用,即java该如何使用日志文件

Python015

log4j的使用,即java该如何使用日志文件,第1张

java使用日志文件log4j的方法:

1、 新建一个Java工程,导入Log4j包,pom文件中对应的配置代码如下:

<!-- log4j support -->

<dependency>

   <groupId>log4j</groupId>

   <artifactId>log4j</artifactId>

   <version>1.2.17</version>

</dependency>

2、resources目录下创建log4j.properties文件

### 设置###

log4j.rootLogger = debug,stdout,D,E

### 输出信息到控制抬 ###

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 = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

### 输出DEBUG 级别以上的日志到=/home/duqi/logs/debug.log ###

log4j.appender.D = org.apache.log4j.DailyRollingFileAppender

log4j.appender.D.File = /home/duqi/logs/debug.log

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}  [ %t:%r ] - [ %p ]  %m%n

### 输出ERROR 级别以上的日志到=/home/admin/logs/error.log ###

log4j.appender.E = org.apache.log4j.DailyRollingFileAppender

log4j.appender.E.File =/home/admin/logs/error.log

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

3、输出日志的例子如下

package com.javadu.log

import org.slf4j.Logger

import org.slf4j.LoggerFactory

public class Log4JTest {

   private static final Logger logger = LoggerFactory.getLogger(Log4JTest.class)

   public static void main(String[] args) {

       // 记录debug级别的信息

       logger.debug("This is debug message.")

       // 记录info级别的信息

       logger.info("This is info message.")

       // 记录error级别的信息

       logger.error("This is error message.")

   }

}

4、输出结果

首先,控制台输入如下图所示:

然后,查看/Users/duqi/logs目录下的debug.log和error.log文件,内容分别如下,可以看出:

满足您的要求请将回答设置为采纳并关注,如有疑问请留言。。。。

java是一种编程语言,所谓java编程,就是用java语言编程,面向对象,使用java的特性编程,面向对象,继承,多态等。。。

概述

Document是itext的基础,你可以添加文档数据(用户阅读的信息)和元数据(pdf内部使用的信息)。在创建document对象时,你可以定义page size,page color and page margins。

构造函数

查看一下API,Document的构造函数有三个。

其中第一个Document给size,color,margins都设置了默认值。查看源代码,默认为Document(PageSize.A4, 36, 36, 36, 36)

第二个构造函数就可以自定义页面的大小了,例如:

Java代码

Rectangle rect = new Rectangle(800,600)

Document document = new Document(rect)

Rectangle指定了宽为800,高位600的页面。

Rectangle

在这里有必要看看Rectangle

我们看看一个函数做了什么

Java代码

/**

* Constructs a <CODE>Rectangle</CODE>-object starting from the origin

* (0, 0).

*

* @param urx

*upper right x

* @param ury

*upper right y

*/

public Rectangle(float urx, float ury) {

this(0, 0, urx, ury)

}

哦,原来是左下角(0,0)为起点,右上角为宽高。如图所示:

当然,通过public Rectangle(float llx, float lly, float urx, float ury)可以随意改变左下角的位置。

Java代码

/**

* Constructs a <CODE>Rectangle</CODE>-object.

*

* @param llx

*lower left x

* @param lly

*lower left y

* @param urx

*upper right x

* @param ury

*upper right y

*/

public Rectangle(float llx, float lly, float urx, float ury) {

this.llx = llx

this.lly = lly

this.urx = urx

this.ury = ury

}

Page Size

理论上将,你可以随意的创建页面的大小,但是不同的PDF规范,强制规范了页面的大小。这一点,比较抽象,我就不详细介绍了,具体可以翻阅itext_in_action_2006 2.1.1小结。

Itext提供了一个很实用的类PageSize,它的作用就是返回static final Rectangle对象的集合。提供了标准化的页面大小。例如:

Java代码

Document document = new Document(PageSize.A4)

横向打印

接下来有个很有趣的函数rotate()。

在打印的时候,经常需要横向打印。有了rotate,这下方便了。

Java代码

Document document = new Document(PageSize.A4.rotate())

还有Page color和Page Margins,

Java代码

Rectangle rect = PageSize.A4

rect.setBackgroundColor(Color.BLUE)

Document document = new Document(rect)

Java代码

Document document = new Document(PageSize.A4, 36,70, 120, 100)

测试代码

Java代码

import java.awt.Color

import java.io.FileOutputStream

import java.io.IOException

import com.lowagie.text.Document

import com.lowagie.text.DocumentException

import com.lowagie.text.PageSize

import com.lowagie.text.Paragraph

import com.lowagie.text.Rectangle

import com.lowagie.text.pdf.PdfWriter

import junit.framework.TestCase

/**

* @blog http://reymont.iteye.com/

* @MSN [email protected]

* @author reymont.li

* @version create time:2011-7-29 上午10:01:44

*/

public class DocumentStudy extends TestCase{

public void testNewDocumentMargin(){

Document document = new Document(PageSize.A4, 36,70, 120, 100)

try {

PdfWriter.getInstance(

document,

new FileOutputStream("resource/NewDocumentMargin.pdf"))

document.open()

document.add(new Paragraph("Hello World"))

} catch (DocumentException de) {

System.err.println(de.getMessage())

} catch (IOException ioe) {

System.err.println(ioe.getMessage())

}

document.close()

}

public void testNewDocumentColor(){

Rectangle rect = PageSize.A4

rect.setBackgroundColor(Color.BLUE)

Document document = new Document(rect)

try {

PdfWriter.getInstance(

document,

new FileOutputStream("resource/NewDocumentColor.pdf"))

document.open()

document.add(new Paragraph("Hello World"))

} catch (DocumentException de) {

System.err.println(de.getMessage())

} catch (IOException ioe) {

System.err.println(ioe.getMessage())

}

document.close()

}

public void testNewDocumentRotate(){

Document document = new Document(PageSize.A4.rotate())

try {

PdfWriter.getInstance(

document,

new FileOutputStream("resource/NewDocumentRotate.pdf"))

document.open()

document.add(new Paragraph("Hello World"))

} catch (DocumentException de) {

System.err.println(de.getMessage())

} catch (IOException ioe) {

System.err.println(ioe.getMessage())

}

document.close()

}

public void testNewDocument2(){

Rectangle rect = new Rectangle(500,500,800,600)

Document document = new Document(rect)

try {

PdfWriter.getInstance(

document,

new FileOutputStream("resource/NewDocument2.pdf"))

document.open()

document.add(new Paragraph("Hello World"))

} catch (DocumentException de) {

System.err.println(de.getMessage())

} catch (IOException ioe) {

System.err.println(ioe.getMessage())

}

document.close()

}

public void testNewDocument1(){

Rectangle rect = new Rectangle(0,0,800,600)

Document document = new Document(rect)

try {

PdfWriter.getInstance(

document,

new FileOutputStream("resource/NewDocument1.pdf"))

document.open()

document.add(new Paragraph("Hello World"))

} catch (DocumentException de) {

System.err.println(de.getMessage())

} catch (IOException ioe) {

System.err.println(ioe.getMessage())

}

document.close()

}

public void testNewDocument(){

Rectangle rect = new Rectangle(800,600)

Document document = new Document(rect)

try {

PdfWriter.getInstance(

document,

new FileOutputStream("resource/NewDocument1.pdf"))

document.open()

document.add(new Paragraph("Hello World"))

} catch (DocumentException de) {

System.err.println(de.getMessage())

} catch (IOException ioe) {

System.err.println(ioe.getMessage())

}

document.close()

}

}