最近在用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>n3.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>n3.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>n3.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%nPS:
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、测试代码如下:
- 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.