版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!
恰饭广告
src目录下新建log4j.properties,并设置编码格式为utf-8
log4j.properties配置文件内容
### 设置级别和目的地(这里多个目的地) ### log4j.rootLogger = CONSOLE,InfoLog,ErrorLog ### 输出到控制台 ### log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Target = System.out log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L [%t:%r]- %m%n ### 输出文件info ### log4j.logger.infolog = InfoLog log4j.appender.InfoLog = org.apache.log4j.DailyRollingFileAppender log4j.appender.InfoLog.File = logs/info.log log4j.appender.InfoLog.Append = true log4j.appender.InfoLog.Threshold = INFO log4j.appender.InfoLog.DatePattern = '.'yyyy-MM log4j.appender.InfoLog.layout = org.apache.log4j.PatternLayout log4j.appender.InfoLog.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n ### 输出文件error ### log4j.logger.errorlog = ErrorLog log4j.appender.ErrorLog = org.apache.log4j.DailyRollingFileAppender log4j.appender.ErrorLog.File = logs/error.log log4j.appender.ErrorLog.Append = true log4j.appender.ErrorLog.Threshold = ERROR log4j.appender.ErrorLog.DatePattern = '.'yyyy-MM log4j.appender.ErrorLog.layout = org.apache.log4j.PatternLayout log4j.appender.ErrorLog.layout.ConversionPattern =%-d{yyyy-MM-dd HH\:mm\:ss} [ %t\:%r ] - [ %p ] %m%n
注意:日志文件路径
调用MainTest.java
import org.apache.log4j.LogManager; import org.apache.log4j.Logger; public class MainTest { public static void main(String[] args) { Logger logger = LogManager.getLogger(MainTest.class.getName()); logger.info("我是info信息"); logger.error("我是error信息"); } }
注意:日志等级
info等级会记录info及以上的内容
erro等级最高,所以只记录error内容
原文链接:https://www.idaobin.com/archives/2062.html
让我恰个饭吧.ヘ( ̄ω ̄ヘ)
恰饭广告