Springboot学习:整合log4j2打印System.out日志

Springboot整合log4j2打印System.out日志

记一次开发踩坑,springboot整合log4j2的过程中,try catch 捕获异常后,Exception.printStackTrace()方法却没有将错误日志打印到日志文件中,通过阅读源码发现,printStackTrace使用的是System.err进行日志打印,所以采用下面的办法进行处理,自定义PrintStream

1、继承PrintStream父类,并重写print这一类方法,使用log输出。

public class Log4j2ErrPrintStream extends PrintStream {
	private Logger log = LoggerFactory.getLogger("SystemErr");
	private static PrintStream instance = new Log4j2ErrPrintStream(System.err);
 
	private Log4j2ErrPrintStream(OutputStream out) {
		super(out);
	}
 
	public static void redirectSystemErr() {
		System.setErr(instance);
	}
 
	public void print(boolean b) {
		println(b);
	}
 
	public void print(char c) {
		println(c);
	}
 
	public void print(char[] s) {
		println(s);
	}
 
	public void print(double d) {
		println(d);
	}
 
	public void print(float f) {
		println(f);
	}
 
	public void print(int i) {
		println(i);
	}
 
	public void print(long l) {
		println(l);
	}
 
	public void print(Object obj) {
		println(obj);
	}
 
	public void print(String s) {
		println(s);
	}
 
	public void println(boolean x) {
		log.error(String.valueOf(x));
	}
 
	public void println(char x) {
		log.error(String.valueOf(x));
	}
 
	public void println(char[] x) {
		log.error(x == null ? null : new String(x));
	}
 
	public void println(double x) {
		log.error(String.valueOf(x));
	}
 
	public void println(float x) {
		log.error(String.valueOf(x));
	}
 
	public void println(int x) {
		log.error(String.valueOf(x));
	}
 
	public void println(long x) {
		log.error(String.valueOf(x));
	}
 
	public void println(Object x) {
		log.error(String.valueOf(x));
	}
 
	public void println(String x) {
		log.error(x);
	}
}

2、自定义SpringApplicationBuilder启动类

public class MyBuilder{
	public MyBuilder(Class<?> sources, String[] args) {
		//使用log4j2来打印system输出
		Log4j2OutPrintStream.redirectSystemOut();
		new SpringApplicationBuilder(sources).run(args);
	}
}

3、使用自定义的启动类启动服务

@SpringBootApplication
public class TestServer {
    public static void main( String[] args ) {
    	new MyBuilder(TestServer .class, args);
    }
}

各位大神走过路过有什么好的建议,欢迎在下方评论交流

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要使用 Log4j 来获取 System.out.println() 打印的信息,你可以通过以下步骤进行操作: 1. 首先,确保你的项目中已经引入了 Log4j 的相关依赖。你可以在 Maven 或 Gradle 的配置文件中添加 Log4j 的依赖项,或者手动将 Log4j 的 JAR 文件添加到项目的类路径中。 2. 创建一个 Log4j 的配置文件,比如 `log4j.properties` 或 `log4j.xml`,用于配置 Log4j 的日志输出方式和级别。在配置文件中,你可以指定日志输出的目标(如控制台、文件等)、日志级别等。 3. 在你的 Java 代码中,使用 Log4j 的 Logger 对象来代替 System.out.println() 打印日志。你可以在需要打印日志的地方获取 Logger 对象,并使用其提供的方法打印不同级别的日志信息。 下面是一个示例代码: ```java import org.apache.log4j.Logger; public class MyClass { private static final Logger logger = Logger.getLogger(MyClass.class); public static void main(String[] args) { logger.debug("Debug message"); // 打印 Debug 级别的日志 logger.info("Info message"); // 打印 Info 级别的日志 logger.error("Error message"); // 打印 Error 级别的日志 } } ``` 在上述示例代码中,我们通过 `Logger.getLogger(MyClass.class)` 获取了一个 Logger 对象,并使用其提供的方法打印不同级别的日志信息。你可以根据需要选择打印不同级别的日志信息。 需要注意的是,你需要根据实际情况修改 Log4j 的配置文件,将日志输出到控制台或其他目标,并设置相应的日志级别。具体的配置方式可以参考 Log4j 的文档或相关资料。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值