关于JAVA中Log4jBean日志打印

JAVA中Log4jBean日志打印

日志打印可以清晰明了的看到你的程序走到那一步了,接下来咱们直入正题:

  • 1.需要jar包支持
  • 2.需要写配置文件
  • 3.需要写一个Log4jBean的类
  • 4.最后就开始用了

代码块

首先是在项目上建一个lib文件夹,导入日志jar包,例如:
这里写图片描述

接着是配置文件log4j.xml,例如:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <properties>
    <property name="logPath">D:\log</property>
  </properties>

  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%F]-[%p]-%L %m%n " />
    </Console>  
    <RollingFile name="RollingFile" filename="${logPath}/log.log"
      filepattern="${logPath}/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz">
  <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%F]-[%p]-%L %m%n " />
      <Policies>
        <SizeBasedTriggeringPolicy size="10240kb"/>
      </Policies>   
       <DefaultRolloverStrategy max="20"/>
    </RollingFile>

  </Appenders>
  <Loggers>
    <Root level="info">
      <AppenderRef ref="Console" />
      <!-- <AppenderRef ref="LogFile" /> -->
      <AppenderRef ref="RollingFile" />
    </Root>
  </Loggers>
</Configuration>

最后是写一个Log4jBean的类,例如:

public class Log4jBean {
    public static Logger logger = LogManager.getLogger(Log4jBean.class
            .getName());

}

按照上面的流程走完之后就可以在你想用日志打印的地方直接用了,例如:
这里写图片描述
效果如下,可以看到时间,你所在的类,方法以及多少行:
这里写图片描述


猜你喜欢

转载自blog.csdn.net/weixin_39921821/article/details/81328745
今日推荐