Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6658338
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: Java

2017-06-12 20:33:10


点击(此处)折叠或打开

  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. *
  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.         Map<String, 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. }

阅读(3352) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

renxiao20032017-06-12 20:38:19

Ant中只有log4j 1.x版的Listener,没有Log4j 2.x版的。我实现了一个。