最近在用Java 写一个FTP上传下载文件工具,正好需要产生操作日志。

利用Log4j.jar可以轻松解决。下面介绍一下Log4j的使用,具体概念到官网查阅:

-----------------------------------------------------------------------------

1、下载:

http://archive.apache.org/dist/logging/log4j/

2、添加jar到java项目:

3、创建:Log4j.Properties文件,放在src文件夹下即可:

3.1 输出到console端:

<span style="color:#666666;font-style: italic;"># Root logger option</span>
log4j.rootLogger=INFO, stdout <span style="color:#666666;font-style: italic;"># Direct log messages to stdout</span>
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=<span style="color:#000000;font-weight: bold;">%</span>d<span style="font-weight: bold;">{</span>yyyy-MM-dd HH:mm:ss<span style="font-weight: bold;">}</span> <span style="color:#000000;font-weight: bold;">%</span>-5p <span style="color:#000000;font-weight: bold;">%</span>c<span style="font-weight: bold;">{</span><span style="color:#000000;">1</span><span style="font-weight: bold;">}</span>:<span style="color:#000000;font-weight: bold;">%</span>L - <span style="color:#000000;font-weight: bold;">%</span>m<span style="color:#000000;font-weight: bold;">%</span>n

3.2 输出到日志文件:

<span style="color:#666666;font-style: italic;"># Root logger option</span>
log4j.rootLogger=INFO, <span style="font-weight: bold;">file</span> <span style="color:#666666;font-style: italic;"># Direct log messages to a log file</span>
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=<span style="color:#000000;">1</span>
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=<span style="color:#000000;font-weight: bold;">%</span>d<span style="font-weight: bold;">{</span>yyyy-MM-dd HH:mm:ss<span style="font-weight: bold;">}</span> <span style="color:#000000;font-weight: bold;">%</span>-5p <span style="color:#000000;font-weight: bold;">%</span>c<span style="font-weight: bold;">{</span><span style="color:#000000;">1</span><span style="font-weight: bold;">}</span>:<span style="color:#000000;font-weight: bold;">%</span>L - <span style="color:#000000;font-weight: bold;">%</span>m<span style="color:#000000;font-weight: bold;">%</span>n

3.3 输出到console同时输出到日志文件:

<span style="color:#666666;font-style: italic;"># Root logger option</span>
log4j.rootLogger=INFO, <span style="font-weight: bold;">file</span>, stdout <span style="color:#666666;font-style: italic;"># Direct log messages to a log file</span>
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=<span style="color:#000000;">1</span>
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=<span style="color:#000000;font-weight: bold;">%</span>d<span style="font-weight: bold;">{</span>yyyy-MM-dd HH:mm:ss<span style="font-weight: bold;">}</span> <span style="color:#000000;font-weight: bold;">%</span>-5p <span style="color:#000000;font-weight: bold;">%</span>c<span style="font-weight: bold;">{</span><span style="color:#000000;">1</span><span style="font-weight: bold;">}</span>:<span style="color:#000000;font-weight: bold;">%</span>L - <span style="color:#000000;font-weight: bold;">%</span>m<span style="color:#000000;font-weight: bold;">%</span>n <span style="color:#666666;font-style: italic;"># Direct log messages to stdout</span>
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=<span style="color:#000000;font-weight: bold;">%</span>d<span style="font-weight: bold;">{</span>yyyy-MM-dd HH:mm:ss<span style="font-weight: bold;">}</span> <span style="color:#000000;font-weight: bold;">%</span>-5p <span style="color:#000000;font-weight: bold;">%</span>c<span style="font-weight: bold;">{</span><span style="color:#000000;">1</span><span style="font-weight: bold;">}</span>:<span style="color:#000000;font-weight: bold;">%</span>L - <span style="color:#000000;font-weight: bold;">%</span>m<span style="color:#000000;font-weight: bold;">%</span>n

3.4  按天roll over(翻滚,转存)日志:

<span style="color:#666666;font-style: italic;"></span># Root logger option
log4j.rootLogger=INFO,console,<span style="background-color: rgb(51, 255, 51);">dailyFile</span>
##Daily log:
log4j.appender.dailyFile=org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyFile.File=e:/ftplog/autoftp.log
log4j.appender.dailyFile.Append=true
log4j.appender.dailyFile.layout=org.apache.log4j.PatternLayout
log4j.appender.dailyFile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p - %m%n
log4j.appender.dailyFile.Threshold=INFO
<span style="color:#33ff33;"><span style="background-color: rgb(51, 255, 51);"><span style="color:#000000;">log4j.appender.dailyFile.DatePattern='.'yyyy-MM-dd</span></span> </span> # Direct log messages to console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=INFO
log4j.appender.console.ImmediateFlush=true
log4j.appender.console.Target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

PS:

Debug Level:  日志记录等级

We have used DEBUG with both the appenders. All the possible options are:

  • TRACE

  • DEBUG

  • INFO

  • WARN

  • ERROR

  • FATAL

  • ALL

Daily Log File Generation: 每日文件生成模式

DatePattern Description
'.' yyyy-MM Roll over at the end of each month and the beginning of the next month.
'.' yyyy-MM-dd This is the default value and roll over at midnight each day.
'.' yyyy-MM-dd-a Roll over at midday and midnight of each day.
'.' yyyy-MM-dd-HH Roll over at the top of every hour.
'.' yyyy-MM-dd-HH-mm Roll over every minute.
'.' yyyy-ww Roll over on the first day of each week depending upon the locale.

4、测试代码如下:

  1. import org.apache.log4j.Logger;
  2. import org.apache.log4j.BasicConfigurator;
  3. public class Hello {
  4. static Logger logger = Logger.getLogger(Hello.class);
  5. public
  6. static
  7. void main(String argv[]) {
  8. BasicConfigurator.configure();
  9. logger.debug("Hello world.");
  10. logger.info("What a beatiful day.");
  11. }
  12. }
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator; public class Hello { static Logger logger = Logger.getLogger(Hello.class); public
static
void main(String argv[]) {
BasicConfigurator.configure();
logger.debug("Hello world.");
logger.info("What a beatiful day.");
}
}

Output:

2013-12-14 13:02:06 ERROR Hello:12 - Hello world.

2013-12-14 13:02:06 INFO  Hello:13 - What a beatiful day.

----------------------------------------

Dylan   presents.

如何配置使用 Log4j的更多相关文章

  1. 有效Log4j按指定级别定向输出日志到指定的输出文件地址配置Threshold,log4j中如何屏蔽父logger输出源rootlogger的additivity配置,log4j向多个文件记录日志

    log4j向多个文件记录日志 关键配置,指定想要的日志级别信息输出到指定的日志文件中: log4j.appender.errorLogger.Threshold=ERROR #扩展,可指定只在子类自己 ...

  2. 【JavaEE】SSH+Spring Security基础上配置AOP+log4j

    Spring Oauth2大多数情况下还是用不到的,主要使用的还是Spring+SpringMVC+Hibernate,有时候加上SpringSecurity,因此,本文及以后的文章的example中 ...

  3. 02_MyBatis项目结构,所需jar包,ehcache.xml配置,log4j.properties,sqlMapConfig.xml配置,SqlMapGenerator.xml配置

     项目结构(所需jar包,配置文件) sqlMapConfig.xml的配置内容如下: <?xmlversion="1.0"encoding="UTF-8&qu ...

  4. 【配置】log4j.properties 详解与配置步骤

    一.Log4j基本使用方法 Log4j由三个重要的组件构成:[日志信息的优先级],[日志信息的输出目的地],[日志信息的输出格式]. 日志信息的优先级从高到低有ERROR.WARN. INFO.DEB ...

  5. 【Java】日志知识总结和经常使用组合配置(commons-logging,log4j,slf4j,logback)

    Log4j Apache的一个开放源码项目,通过使用Log4j,我们能够控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接口服务 器.NT的事件记录器.UNIX Syslog守护进程等.用 ...

  6. 在android中配置 slf4j + log4j 日志记录框架

    需求: 在项目开发中,需要记录 操作日志 .起初自己写了个简单的日志记录文本写入到文本的方法,后来随着项目的膨胀,需要考虑更多的操作,开始考虑性能问题. 实现: 考虑使用 slf4j + log4j ...

  7. log4j 路径环境变量配置和log4j加载配置

    1.lo4j日志路径从环境变量读取,log4j.xml配置如下: 具体配置如下: log4j.appender.R.Encoding=UTF-8 log4j.appender.R=org.apache ...

  8. tomcat配置使用log4j管理日志

    从tomcat官网下载和tomcat对应的tomcat-juli.jar和tomcat-juli-adapters.jar,从log4j官网下载log4j的jar包(我用的是log4j-1.2.17. ...

  9. java:Mybatis框架1(基本配置,log4j,Junit4(单元测试))

    1.mybatis01: db.properties: driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test userna ...

  10. mybatis 配置的log4j文件无效,不能正常显示日志信息

    正在学习mybatis,配置好后log4j.properties文件后,日志信息不能正常显示,没有效果. 查看了一下mybatis的相关文档,在日志一栏找到问题愿意 原因是我们的mybatis选了其他 ...

随机推荐

  1. CRC编码

    一.循环冗余码校验英文名称为Cyclical Redundancy Check,简称CRC. 它是利用除法及余数的原理来作错误侦测(Error Detecting)的.实际应用时,发送装置计算出CRC ...

  2. 阿里云centos6.5下搭建javaWeb运行环境

    一.主要方法 http://blog.csdn.net/hdfyq/article/details/38456981 上面文章的步骤至mysql安装完毕以及设置(记住 update user set ...

  3. Elasticsearch1.7到2.3升级实践总结

    概括 简述 升级分为Elasticsearch server升级和Elasticsearch client api升级 为什么要迁移 当前团队内多个业务方公用一套ES集群,容易被影响,重要业务应该独自 ...

  4. Node.js的UnitTest单元测试

    body{ font: 16px/1.5em 微软雅黑,arial,verdana,helvetica,sans-serif; } 在专业化的软件开发过程中,无论什么平台语言,现在都需要UnitTes ...

  5. Java经典实例:按字符颠倒字符串

    使用StringBuilder类的reverse()方法来实现. /** * Created by Frank */ public class StringRevChar { public stati ...

  6. GJM :用JIRA管理你的项目(三)基于LDAP用户管理 [转载]

    感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...

  7. spring 的自建request请求

    public String myRequest() throws IOException, URISyntaxException{                String url="ht ...

  8. 线程(三)__Interrupt 、setDaemon()、join

    一.wait和sleep区别? 1.wait可以指定也可以不指定.sleep必须指定时间. 2.在同步中时,对cpu的执行权和锁的处理不同.它们都能将线程处于冻结状态. wait:释放执行权,释放锁. ...

  9. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 解决办法

    09:00:30.307 [http-8080-6] ERROR org.hibernate.transaction.JDBCTransaction -JDBC begin failed com.my ...

  10. GifShot - 创建动态 GIF 的 JavaScript 库

    GifShot 是一个可以创建流媒体,视频或图像的 GIF 动画的 JavaScript 库.该库的客户端特性使其非常便携,易于集成到几乎任何网站.利用最先进的浏览器 API ,包括 WebRTC , ...