配置log4j_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 配置log4j

配置log4j

 2015/5/5 0:45:34  1259988502  程序员俱乐部  我要评论(0)
  • 摘要:如何配置使用Log4j分类:java2013-12-1414:473519人阅读评论(2)收藏举报javalog4j目录(?)[+]最近在用Java写一个FTP上传下载文件工具,正好需要产生操作日志。利用Log4j.jar可以轻松解决。下面介绍一下Log4j的使用,具体概念到官网查阅:-----------------------------------------------------------------------------1、下载:http://archive.apache
  • 标签:配置
如何配置使用 Log4j
分类: java 2013-12-14 14:47 3519人阅读 评论(2) 收藏 举报
javalog4j
目录(?)[+]
最近在用Java 写一个FTP上传下载文件工具,正好需要产生操作日志。
利用Log4j.jar可以轻松解决。下面介绍一下Log4j的使用,具体概念到官网查阅:
-----------------------------------------------------------------------------
1、下载:
http://archive.apache.org/dist/logging/log4j/
2、添加jar到java项目:

3、创建:Log4j.Properties文件,放在src文件夹下即可:
  3.1 输出到console端:
# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
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} %-5p %c{1}:%L - %m%n
   3.2 输出到日志文件:
# Root logger option
log4j.rootLogger=INFO, file

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
  3.3 输出到console同时输出到日志文件:
# Root logger option
log4j.rootLogger=INFO, file, stdout

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=C:\\loging.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

# Direct log messages to stdout
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} %-5p %c{1}:%L - %m%n
  3.4  按天roll over(翻滚,转存)日志:
# Root logger option
log4j.rootLogger=INFO,console,dailyFile
##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
log4j.appender.dailyFile.DatePattern='.'yyyy-MM-dd 

# 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、测试代码如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
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.


感谢 http://blog.csdn.net/indexman/article/details/17318817
上一篇: java生成解析Json 下一篇: 没有下一篇了!
发表评论
用户名: 匿名