Log4j2配置及使用
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.0</version>
</dependency>
- Log4j will inspect the "log4j.configurationFile" system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension.
- If no system property is set the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
- If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
- If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
- If a test file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.
- If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
- If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
- If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ERROR">
<!--Configuration 配置项
status:log4j本身的日志级别, “trace”, “debug”, “info”, “warn”, “error” and “fatal”
monitorInterval:每隔多少秒从新读取配置文件,可以在不重启的情况下修改配置-->
<Appenders>
<!--Appenders定义日志输出,有Console、File、RollingRandomAccessFile、MongoDB、Flume 等
有Console:输出源到控制台
File:将日志输出到文件,通过fileName指定存储到的文件(目录不存在会自动创建)
RollingRandomAccessFile:也是写入文件,但可以定义规则按照文件大小或时间等重新创建一个新的日志文件存储;如果是按时间分割需要配合filePattern使用
-->
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %F %logger{36} - %msg%n"/>
<!--PatternLayout指定输出日志的格式 -->
</Console>
<File name="debuglog" fileName="./log/debug.log" append="true”>
<!--fileName为存储日志的文件地址;append为是否在已有文件上追加,true为追加,false为重建-->
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
</File>
<RollingFile name="customscript" fileName="${LOG_HOME}${FILE_NAME}" filePattern="${LOG_HOME}${FILE_NAME}.%d{yyyy-MM-dd}.log">
<!—filePattern为分割存储的日志的名字;必须加Policies,分割方式-->
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %M %L - %msg%xEx%n"/>
<Policies>
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<!--日志器,通过LogManager.getLogger(日志器name)的日志器名字决定使用具体哪个日志器
分为根Root日志器和自定义日志器-->
<Root level="ERROR">
<!--当根据名字找不到对应的日志器时,使用Root的日志器
leve:日志输出等级(默认ERROR);TRACE > DEBUG > INFO > WARN > ERROR, ALL or OFF-->
<AppenderRef ref="Console"/>
<!--AppenderRef:关联Appenders中定义的输出规则,可以有多个,日志可以同时输出到多个地方-->
<AppenderRef ref="debuglog"/>
</Root>
<Logger name="customlog" level="INFO" additivity="false">
<!--Logger自定义日志器:
name为日志器的名字,通过LogManager.getLogger(日志器name)获得该日志器连接
additivity:相加性。默认为true,若为true会将当前日志内容也打印到它上面的日志器内,这里上面日志器是Root-->
<AppenderRef ref="Console"/>
</Logger>
</Loggers>
</Configuration>
%d{HH:mm:ss.SSS} 表示输出到毫秒的时间
%t 输出当前线程名称
%-5level 输出日志级别,-5表示左对齐并且固定输出5个字符,如果不足在右边补0
%logger 输出logger名称,因为Root Logger没有名称,所以没有输出
%msg 日志文本
%n 换行
其他常用的占位符有:
%F 输出所在的类文件名,如Log4j2Test.java
%L 输出行号
%M 输出所在方法名
%l 输出语句所在的行数, 包括类名、方法名、文件名、行数
fileName 指定当前日志文件的位置和文件名称
filePattern 指定当发生Rolling时,文件的转移和重命名规则
SizeBasedTriggeringPolicy 指定当文件体积大于size指定的值时,触发Rolling
DefaultRolloverStrategy 指定最多保存的文件个数
TimeBasedTriggeringPolicy 这个配置需要和filePattern结合使用,注意filePattern中配置的文件重命名规则是${FILE_NAME}-%d{yyyy-MM-dd HH-mm}-%i,最小的时间粒度是mm,即分钟
TimeBasedTriggeringPolicy指定的size是1,结合起来就是每1分钟生成一个新文件。如果改成%d{yyyy-MM-dd HH},最小粒度为小时,则每一个小时生成一个文件
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="25 KB" />
</Policies>
三、Java
package util; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; public class Log { static final Logger logger_root = LogManager.getLogger(Log.class.getName());//配置文件没配置Log的class名字,所以用默认的Root
static final Logger logger_custom = LogManager.getLogger("customlog");//配置文件定义了customlog,所以用customlog public static void main(String[] args) {
// 记录debug级别的信息
logger_root.debug("This is debug message.");
// 记录info级别的信息
logger_root.info("This is info message.");
// 记录error级别的信息
logger_root.error("This is error message."); // 记录debug级别的信息
logger_custom.debug("This is debug message.");
// 记录info级别的信息
logger_custom.info("This is info message.");
// 记录error级别的信息
logger_custom.error("This is error message.");
}
}
Log4j2配置及使用的更多相关文章
- 转:spring boot log4j2配置(使用log4j2.yml文件)---YAML 语言教程
转:spring boot log4j2配置(使用log4j2.yml文件) - CSDN博客http://blog.csdn.net/ClementAD/article/details/514988 ...
- log4j2配置ThresholdFilter,让info文件记录error日志
日志级别: 是按严重(重要)程度来分的(如下6种): ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < ...
- Log4j2配置之Appender详解
Log4j2配置之Appender详解 Appender负责将日志事件传递到其目标.每个Appender都必须实现Appender接口.大多数Appender将扩展AbstractAppender,它 ...
- Log4j2 - 配置
官方文档:http://logging.apache.org/log4j/2.x/index.html 1 概述 Log4j2的配置包含四种方式,其中3种都是在程序中直接调用Log4j2的方法进行配置 ...
- log4j2配置详解
1. log4j2需要两个jar log4j-api-2.x.x.jar log4j-core-2.x.x.jar .log4j和log4j2有很大的区别,jar包不要应错. 2. ...
- 【Log4j2 配置详解】log4j2的资源文件具体怎么配置
可以先附上一个log4j2的资源文件详细内容,对照着看 ### set log levels ### log4j.rootLogger = INFO , C , D , E ### console # ...
- Log4j2 配置笔记(Eclipse+maven+SpringMVC)
Log4j2相关介绍可以百度看下,这里只注重配置Log4j2 能够马上跑起来: 1.pom.xml文件中添加Log4j2的相关Maven配置信息 <!-- log4j2 --> <d ...
- Spring Boot初探之log4j2配置
一.背景 下面讲在使用Spring Boot搭建微服务框架时如何配置log4j2,通过log4j2输出系统中日志信息. 二.添加log4j2的配置文件 在项目的src/main/rescources目 ...
- Log4j2配置与使用
依赖包: <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api --> <depend ...
- spring boot log4j2配置
[传送门]:log4j官网配置文件详解 1. 排除 spring boot 自带的 spring-boot-starter-logging 依赖 <dependency> <gro ...
随机推荐
- pyqt 8行内就可以跑一个浏览器
pyqt 8行内就可以跑一个浏览器 from PyQt4.QtCore import * from PyQt4.QtGui import * from PyQt4.QtWebKit import * ...
- SD-WAN产品常见问题
转载: http://www.safecdn.cn/website-announcement/2018/12/sd-wan-problem/102.html 1.SD-WAN加速产品最大的优势是什么? ...
- python中的jion
on将列表或元组中的每个元素连接起来,举个例子: 1 a = ["hello", "world", "dlrb"] 2 a1 = " ...
- ant 标签详解
Ant 开发 Ant的构建文件当开始一个新的项目时,首先应该编写Ant构建文件.构建文件定义了构建过程,并被团队开发中每个人使用.Ant构建文件默认命名为build.xml,也可以取其他的名字.只不过 ...
- asp.net excel模板下载
string filePath = Server.MapPath("~/model/模板.xls");//路径 FileInfo fileInfo = new FileInfo(f ...
- leetcode解题报告 32. Longest Valid Parentheses 用stack的解法
第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...
- 应用DBExportDoc导出mysql库为word07文档
1.相关软件下载 DBExportDoc V1.0 For MySQL 密码:znu3 MySQL Connector/ODBC 2.安装mysql-connector-odbc并配置数据源 安装略. ...
- tar 打包当前目录下文件但不包括该录
今天想打包一些文件,但是不想把该目录打包进去 比如我想把test目录下文件打个包,安装正常的命令来 tar zcf test.tar.gz test 这样肯定会把test目录也打进去,解压后肯定是te ...
- Oracle分区表常见操作
Oracle分区表常用于业务中大表使用,如历史交易记录表等,提高表记录查询效率.本文主要描述范围分区表的创建.新增以及索引创建. Oracle操作分区表相关信息 显示数据库所有分区表的信息:DBA_P ...
- 自定义标签在IE6-8的困境
或许未来前端组件化之路都是自定义标签,但这东西早在20年前,JSTL已在搞了.现在Web Component还只有webkit支持.但一个组件库,还需要一个特殊的标识它们是一块的.不过这个XML已经帮 ...