ant的log4j2版本的Listener

简介: 点击(此处)折叠或打开 /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.

点击(此处)折叠或打开

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */

  18. package org.apache.tools.ant.listener;

  19. import java.util.Map;
  20. import org.apache.logging.log4j.LogManager;
  21. import org.apache.logging.log4j.Logger;
  22. import org.apache.logging.log4j.core.Appender;
  23. import org.apache.tools.ant.BuildEvent;
  24. import org.apache.tools.ant.BuildListener;
  25. import org.apache.tools.ant.Project;
  26. import org.apache.tools.ant.Target;
  27. import org.apache.tools.ant.Task;

  28. /**
  29. * Listener which sends events to Log4j logging system
  30. *
  31. */
  32. public class Log4j2Listener implements BuildListener {

  33.     /** Indicates if the listener was initialized. */
  34.     private final boolean initialized;

  35.     /**
  36.      * log category we log into
  37.      */
  38.     public static final String LOG_ANT = "org.apache.tools.ant";

  39.     /**
  40.      * Construct the listener and make sure there is a valid appender.
  41.      */
  42.     public Log4j2Listener() {
  43.         final Logger log = LogManager.getLogger(LOG_ANT);
  44.         final Logger rootLog = LogManager.getRootLogger();
  45.         MapString, Appender> appenderMap =
  46.                 ((org.apache.logging.log4j.core.Logger) rootLog).getAppenders();
  47.         initialized = !(appenderMap.isEmpty());
  48.         if (!initialized) {
  49.             log.error("No log4j.properties in build area");
  50.         }
  51.     }

  52.     /**
  53.      * @see BuildListener#buildStarted
  54.      */
  55.     /** {@inheritDoc}. */
  56.     public void buildStarted(final BuildEvent event) {
  57.         if (initialized) {
  58.             final Logger log = LogManager.getLogger(Project.class.getName());
  59.             log.info("Build started.");
  60.         }
  61.     }

  62.     /**
  63.      * @see BuildListener#buildFinished
  64.      */
  65.     /** {@inheritDoc}. */
  66.     public void buildFinished(final BuildEvent event) {
  67.         if (initialized) {
  68.             final Logger log = LogManager.getLogger(Project.class.getName());
  69.             if (event.getException() == null) {
  70.                 log.info("Build finished.");
  71.             } else {
  72.                 log.error("Build finished with error.", event.getException());
  73.             }
  74.         }
  75.     }

  76.     /**
  77.      * @see BuildListener#targetStarted
  78.      */
  79.     /** {@inheritDoc}. */
  80.     public void targetStarted(final BuildEvent event) {
  81.         if (initialized) {
  82.             final Logger log = LogManager.getLogger(Target.class.getName());
  83.             log.info("Target \"" + event.getTarget().getName() + "\" started.");
  84.         }
  85.     }

  86.     /**
  87.      * @see BuildListener#targetFinished
  88.      */
  89.     /** {@inheritDoc}. */
  90.     public void targetFinished(final BuildEvent event) {
  91.         if (initialized) {
  92.             final String targetName = event.getTarget().getName();
  93.             final Logger cat = LogManager.getLogger(Target.class.getName());
  94.             if (event.getException() == null) {
  95.                 cat.info("Target \"" + targetName + "\" finished.");
  96.             } else {
  97.                 cat.error("Target \"" + targetName
  98.                     + "\" finished with error.", event.getException());
  99.             }
  100.         }
  101.     }

  102.     /**
  103.      * @see BuildListener#taskStarted
  104.      */
  105.     /** {@inheritDoc}. */
  106.     public void taskStarted(final BuildEvent event) {
  107.         if (initialized) {
  108.             final Task task = event.getTask();
  109.             final Logger log = LogManager.getLogger(task.getClass().getName());
  110.             log.info("Task \"" + task.getTaskName() + "\" started.");
  111.         }
  112.     }

  113.     /**
  114.      * @see BuildListener#taskFinished
  115.      */
  116.     /** {@inheritDoc}. */
  117.     public void taskFinished(final BuildEvent event) {
  118.         if (initialized) {
  119.             final Task task = event.getTask();
  120.             final Logger log = LogManager.getLogger(task.getClass().getName());
  121.             if (event.getException() == null) {
  122.                 log.info("Task \"" + task.getTaskName() + "\" finished.");
  123.             } else {
  124.                 log.error("Task \"" + task.getTaskName()
  125.                     + "\" finished with error.", event.getException());
  126.             }
  127.         }
  128.     }

  129.     /**
  130.      * @see BuildListener#messageLogged
  131.      */
  132.     /** {@inheritDoc}. */
  133.     public void messageLogged(final BuildEvent event) {
  134.         if (initialized) {
  135.             Object categoryObject = event.getTask();
  136.             if (categoryObject == null) {
  137.                 categoryObject = event.getTarget();
  138.                 if (categoryObject == null) {
  139.                     categoryObject = event.getProject();
  140.                 }
  141.             }

  142.             final Logger log
  143.                 = LogManager.getLogger(categoryObject.getClass().getName());
  144.             switch (event.getPriority()) {
  145.                 case Project.MSG_ERR:
  146.                     log.error(event.getMessage());
  147.                     break;
  148.                 case Project.MSG_WARN:
  149.                     log.warn(event.getMessage());
  150.                     break;
  151.                 case Project.MSG_INFO:
  152.                     log.info(event.getMessage());
  153.                     break;
  154.                 case Project.MSG_VERBOSE:
  155.                     log.debug(event.getMessage());
  156.                     break;
  157.                 case Project.MSG_DEBUG:
  158.                     log.debug(event.getMessage());
  159.                     break;
  160.                 default:
  161.                     log.error(event.getMessage());
  162.                     break;
  163.             }
  164.         }
  165.     }
  166. }

目录
相关文章
|
消息中间件 安全 Dubbo
Log4j安全漏洞前车之鉴,呕心整理工作中常用开源组件避坑版本
Log4j安全漏洞前车之鉴,呕心整理工作中常用开源组件避坑版本
542 0
|
消息中间件 弹性计算 数据可视化
SpringBoot 整合 Elastic Stack 最新版本(7.14.1)分布式日志解决方案,开源微服务全栈项目【有来商城】的日志落地实践
SpringBoot 整合 Elastic Stack 最新版本(7.14.1)分布式日志解决方案,开源微服务全栈项目【有来商城】的日志落地实践
|
5月前
|
监控 关系型数据库 MySQL
|
4月前
|
弹性计算 Prometheus Cloud Native
SLS Prometheus存储问题之Union MetricStore在性能测试中是如何设置测试环境的
SLS Prometheus存储问题之Union MetricStore在性能测试中是如何设置测试环境的
|
3月前
|
缓存 Oracle Java
JDK8到JDK22版本升级的新特性问题之在JDK17中,日志的刷新如何操作
JDK8到JDK22版本升级的新特性问题之在JDK17中,日志的刷新如何操作
|
6月前
|
存储 监控 Serverless
在处理阿里云函数计算3.0版本的函数时,如果遇到报错但没有日志信息的情况
在处理阿里云函数计算3.0版本的函数时,如果遇到报错但没有日志信息的情况【1月更文挑战第23天】【1月更文挑战第114篇】
101 5
|
IDE Linux 开发工具
IntelliJ IDEA 2023.2.1 修复版本日志
IntelliJ IDEA 2023.2.1 修复版本日志
|
Linux CDN
利用工具合并CDN日志操作——Linux版本
利用工具合并CDN日志操作——Linux版本自制脑图
156 0
利用工具合并CDN日志操作——Linux版本
|
CDN Windows
利用工具合并CDN日志操作——Windows版本
利用工具合并CDN日志操作——Windows版本自制脑图
130 0
利用工具合并CDN日志操作——Windows版本
下一篇
无影云桌面