• 如何配置使用 Log4j


    最近在用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.      
    4.     public class Hello { 
    5.      
    6.       static Logger logger = Logger.getLogger(Hello.class); 
    7.      
    8.       public 
    9.       static 
    10.       void main(String argv[]) { 
    11.         BasicConfigurator.configure(); 
    12.         logger.debug("Hello world."); 
    13.         logger.info("What a beatiful day."); 
    14.       } 
    15.     } 

    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.

  • 相关阅读:
    采样频率和采样点数以及奈奎斯特采样定理
    三阶截断点TOI或IP3
    动态库、静态库
    Qt添加静态库
    给测试开发工程师的5条建议
    软件测试中7个令人震惊的真相
    2022 年值得测试同学关注的技术趋势
    度量自动化测试效果的10个指标
    如何优化selenium webdriver的执行速度
    单元测试用例该如何设计
  • 原文地址:https://www.cnblogs.com/wlf-919874006/p/4811311.html
Copyright © 2020-2023  润新知