how to make log4j to write to the console as well

后端 未结 4 770
傲寒
傲寒 2020-11-29 00:45

Is there any way to tell to log4j to write its log to the file and to the console? thanks there are my properties:

log4j.rootLogger=DEBUG,console,R
log4j.roo         


        
相关标签:
4条回答
  • 2020-11-29 01:09

    This works well for console in debug mode

    log4j.appender.console=org.apache.log4j.ConsoleAppender
    log4j.appender.console.Threshold=DEBUG
    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,SSS} %-5p - %m%n
    
    0 讨论(0)
  • 2020-11-29 01:11

    Write the root logger as below for logging on both console and FILE

    log4j.rootLogger=ERROR,console,FILE

    And write the respective definitions like Target, Layout, and ConversionPattern (MaxFileSize for file etc).

    0 讨论(0)
  • 2020-11-29 01:15

    Your log4j File should look something like below read comments.

    # Define the types of logger and level of logging    
    log4j.rootLogger = DEBUG,console, FILE
    
    # Define the File appender    
    log4j.appender.FILE=org.apache.log4j.FileAppender    
    
    # Define Console Appender    
    log4j.appender.console=org.apache.log4j.ConsoleAppender    
    
    # Define the layout for console appender. If you do not 
    # define it, you will get an error    
    log4j.appender.console.layout=org.apache.log4j.PatternLayout
    
    # Set the name of the file    
    log4j.appender.FILE.File=log.out
    
    # Set the immediate flush to true (default)    
    log4j.appender.FILE.ImmediateFlush=true
    
    # Set the threshold to debug mode    
    log4j.appender.FILE.Threshold=debug
    
    # Set the append to false, overwrite    
    log4j.appender.FILE.Append=false
    
    # Define the layout for file appender    
    log4j.appender.FILE.layout=org.apache.log4j.PatternLayout    
    log4j.appender.FILE.layout.conversionPattern=%m%n
    
    0 讨论(0)
  • Your root logger definition is a bit confused. See the log4j documentation.

    This is a standard Java properties file, which means that lines are treated as key=value pairs. Your second log4j.rootLogger line is overwriting the first, which explains why you aren't seeing anything on the console appender.

    You need to merge your two rootLogger definitions into one. It looks like you're trying to have DEBUG messages go to the console and INFO messages to the file. The root logger can only have one level, so you need to change your configuration so that the appenders have appropriate levels.

    While I haven't verified that this is correct, I'd guess it'll look something like this:

    log4j.rootLogger=DEBUG,console,file
    log4j.appender.console=org.apache.log4j.ConsoleAppender
    log4j.appender.file=org.apache.log4j.RollingFileAppender
    

    Note that you also have an error in casing - you have console lowercase in one place and in CAPS in another.

    0 讨论(0)
提交回复
热议问题