log4j2
来源:互联网 发布:钢结构公司起名 知乎 编辑:程序博客网 时间:2024/05/16 05:45
log4j2
目录
依赖
配置文件
指定配置文件路径
示例
依赖
<dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.2</version> </dependency>
配置文件
<?xml version="1.0" encoding="UTF-8"?><Configuration status="WARN"><Appenders> <Console name="myConsole" target="SYSTEM_OUT"> <PatternLayout pattern="[%d{MM-dd HH:mm:ss,SSS\} %-5p] [%t] %C{1} - %m%n"/> </Console> <RollingFile name="common" fileName="/aaa/bbb/ccc/filename.log" filePattern="/aaa/bbb/ccc/filename.log.%d{yyyy-MM-dd}"> <PatternLayout> <Pattern>[%d{MM-dd HH:mm:ss SSS\} %-5p] [%t] %C{1} - %m%n</Pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile> <RollingFile name="payment" fileName="/aaa/bbb/ccc/filename.log" filePattern="/aaa/bbb/ccc/filename.log.%d{yyyy-MM-dd}.log"> <PatternLayout> <Pattern>[%d{MM-dd HH:mm:ss SSS\} %-5p] [%t] %C{3\} - %m%n</Pattern> </PatternLayout> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> </RollingFile></Appenders><Loggers> <logger name="payment" level="INFO" additivity="false"> <AppenderRef ref="payment" /> </logger> <Root level="info"> <AppenderRef ref="myConsole" /> <AppenderRef ref="common" /> </Root></Loggers></Configuration>
指定配置文件路径
public static String setLogConfigFilePath(){ Logger log; ConfigurationSource source; String config = System.getProperty("user.dir"); String fullPath = config + "\\log4j2.xml"; System.out.println("Log4j2 fullPath = " + fullPath); File file = new File(fullPath); //this ConfigurationSource() could load xml dynamicly, info->debug->info test OK!!! try { source = new ConfigurationSource(new FileInputStream(file), file); Configurator.initialize(null, source); log = LogManager.getLogger("receive"); log.info("haha~~"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return fullPath; }
示例
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;public class AnotherClazz { private static Logger log = LogManager.getLogger(AnotherClazz.class); public static void sayHi(){ System.out.println("hi~~~"); log.info("hi~~~"); }}
log4j
配置文件
log4j.rootLogger=INFO, deallog log4j.appender.deallog=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.deallog.File=/aaa/bbb/logs/post-payback-esb.loglog4j.appender.deallog.Append=truelog4j.appender.deallog.Threshold=INFOlog4j.appender.deallog.layout=org.apache.log4j.PatternLayoutlog4j.appender.deallog.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%nlog4j.additivity.esblog=falselog4j.logger.esblog=INFO,esbloglog4j.appender.esblog=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.esblog.File=/aaa/bbb/logs/post_payback.esbreceive.loglog4j.appender.esblog.Append=truelog4j.appender.esblog.Threshold=INFOlog4j.appender.esblog.layout=org.apache.log4j.PatternLayoutlog4j.appender.esblog.layout.ConversionPattern=%d{ABSOLUTE} - %m%n
指定配置文件路径
public static String setLogConfigFilePath(){ PropertyConfigurator.configure("E://log4j.properties"); Logger log = Logger.getLogger(TaskMain.class); log.info("haha"); }
0 0
- Log4j2
- log4j2
- log4j2
- log4j2
- log4j2
- Log4J2
- Log4j2
- Log4j2
- log4j2
- log4j2配置文件log4j2.xml
- log4j2配置文件log4j2.xml
- log4j2配置文件log4j2.xml
- 入门log4j2
- log4j2 使用说明
- log4j2配置
- log4j2使用
- Log4j2详解
- log4j2 配置文件
- C++ 中的 Lambda 表达式
- 9anchor
- 【基础知识思考整理】函数指针有什么用?
- Excel 插件开发
- 在 eclipse 中 配置 tomcat 服务器
- log4j2
- HDU 5889 Barricade 最短路最小割 -
- 泊松分布采样 (Poisson-Disk-Sample)代码及详细注释【OpenCV】
- 面向服务的体系结构与企业体系结构,第 2 部分: 相似点与不同处
- 模型构建问题
- 在Android App里实现语言切换
- 51nod N的阶乘
- POJ1088滑雪(记忆化搜索与dp)
- 10fitsSystemWindows对CoordinatorLayout的影响