当前位置: 首页 > news >正文

接管所有System.out.println转成Logger输出日志

新建日志类:

/*** @Description: 用logback接管System.out和System.err* @Author: thp-mac* @Date: 2024/6/14* @Version: 1.0**/
public class MyLog {private static final Logger logger = LoggerFactory.getLogger(MyLog.class);/*** 把System.out和System.err替换为新的PrintStream,用logger.info代替System.out.print和System.err.print* @param SysPrintStream  可以是System.out和System.err* @return  返回新的PrintStream*/public static PrintStream createMyPrintStreamInfo(final PrintStream SysPrintStream) {PrintStream myStream= new PrintStream(SysPrintStream) {public void print(final String string) {logger.info(string);}};return myStream;}public static PrintStream createMyPrintStreamError(final PrintStream SysPrintStream) {PrintStream myStream= new PrintStream(SysPrintStream) {public void print(final String string) {logger.error(string);}};System.out.println();return myStream;}/*** 在程序启动时运行这个函数,让logback接管Sytem.out和System.err*/public static void init() {System.setOut(createMyPrintStreamInfo(System.out)); //替换System.outSystem.setErr(createMyPrintStreamError(System.err)); //替换System.err}}

在启动main中调用他:

@EnableScheduling
@EnableSwagger2
@SpringBootApplication
public class MainApplication {public static void main(String[] args) {MyLog.init();//程序启动时运行这个方法,主要就是这一句SpringApplication.run(MainApplication.class, args);}}
http://www.dtcms.com/a/565585.html

相关文章:

  • 建三江廉政建设网站长春网站制作推广
  • [LitCTF 2023]这是什么?SQL !注一下 !
  • 小数位进制转换怎么用 python 表示
  • 网站页面报价镇海区建设工程安监站网站
  • ESP32 HTTP回调机制详解与优化实践
  • Pycharm+Deepseek结合使用Continue插件无法返回中文产生乱码
  • k8s基础概念、Pod、k8s基础命令
  • 设计模式——适配器(adapter)
  • Js随堂笔记2025-11-3
  • 贵州网站建设设计公司哪家好怎么网站搜索排名优化
  • 使用 Docker Compose 轻松实现 INFINI Console 离线部署与持久化管理
  • 基于SpringBoot的高校社团管理系统【协同过滤推荐算法+数据可视化】
  • 中山网站建设文化流程免费网站建设市场
  • MDI Jade9.0中文版下载及详细安装教程,MDI Jade安装包(附详细教程)
  • hash滑窗|dp
  • 免费APP 微信 网站平台汕头网
  • 网页翻译用什么软件长春百度关键词优化
  • 【js逆向案例三】瑞数6
  • 等保三级“通关”秘籍:如何化繁为简,高效通过
  • 【文笔碎屑】更深的温柔
  • 建设网站费用明细北京市建设工程资源交易网
  • gateface做网站文章类型网站
  • Spring Boot3零基础教程,StreamAPI 介绍,笔记98
  • windows-scoop管理jdk版本
  • 构建轻量级Thrift服务自动化部署Pipeline
  • 什么是seo优化?广州网站seo推广
  • OUC AI Lab第五章:生成式对抗网络 Diffusion
  • JAVA113 Leecode 3 无重复字符的最长字串
  • 给上市公司做网站有什么用ui设计软件sketch
  • 【05】JMeter导出接口 保存文件到本地