ant的log4j2版本的Listener


点击(此处)折叠或打开

  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. }

上一篇:C#对word、excel、pdf等格式文件的操作总结


下一篇:centos7根分区扩容(适用目前主流版本)