All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.unkrig.antology.task.SimpleLoggingTask Maven / Gradle / Ivy

Go to download

The ANTOLOGY Ant library contributes useful tasks and types to APACHE ANT. It integrates seamlessly with other libraries like ant-contrib.

There is a newer version: 2.0.7
Show newest version

/*
 * antology - Some contributions to APACHE ANT
 *
 * Copyright (c) 2011, Arno Unkrig
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
 * following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
 *       following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
 *       following disclaimer in the documentation and/or other materials provided with the distribution.
 *    3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
 *       products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package de.unkrig.antology.task;

import java.io.File;
import java.util.logging.Level;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

import de.unkrig.commons.nullanalysis.Nullable;
import de.unkrig.commons.text.expression.Parser;
import de.unkrig.commons.util.logging.SimpleLogging;
import de.unkrig.commons.util.logging.formatter.PrintfFormatter;
import de.unkrig.commons.util.logging.handler.StdoutHandler;

// CHECKSTYLE JavadocMethod:OFF

/**
 * An ANT task which configures the {@link SimpleLogging} facility.
 */
public
class SimpleLoggingTask extends Task {

    /** Type for the {@code debug="..."} attribute. */
    public enum Debug { FINE, FINER, FINEST }

    @Nullable private File    out;
    private boolean           stdout;
    @Nullable private Level   level;
    @Nullable private Boolean noWarn, quiet, normal, verbose;
    @Nullable private Debug   debug;
    @Nullable private String  spec;
    @Nullable private String  formatter;

    // BEGIN CONFIGURATION SETTERS

    /**
     * Messages of levels INFO (inclusive) through WARNING (exclusive) will be written to the given file.
     */
    public void setOut(File outputFile) { this.out = outputFile; }

    /**
     * Messages of levels INFO (inclusive) through WARNING (exclusive) will be written to STDOUT.
     */
    public void setStdout(boolean value) { this.stdout = value; }

    /**
     * Configures the amount of logging; more precisely: The level of the "root logger".
     * 
     *   
     *     
     *     
     *     
     *   
     *   
     *   
     *   
     *   
     *   
     *     
     *     
     *     
     *   
     *   
     *     
     *     
     *     
     *   
     *   
     *     
     *     
     *     
     *   
     * 
levelLevels logged to STDERRLevels logged to STDOUT
SEVERESEVERE-
WARNINGSEVERE, WARNING-
INFOSEVERE, WARNINGINFO
CONFIGSEVERE, WARNINGINFO, CONFIG
FINESEVERE, WARNING
FINE*
INFO, CONFIG
FINERSEVERE, WARNING
FINE, FINER*
INFO, CONFIG
FINESTSEVERE, WARNING
FINE, FINER, FINEST*
INFO, CONFIG
*

* *: FINE, FINER and FINEST log records are printed with class, method, source and line number *

*

* The default is determined by a number of sources, e.g. the "{@code logging.properties}" file. However in * many cases, the level of the root logger will initially be {@code INFO}. *

*/ public void setLevel(String level) { this.level = Level.parse(level); } /** * Shorthand for {@link #setLevel(String) level}{@code ="WARNING"}: Messages of levels INFO and WARNING will be * suppressed. */ public void setNoWarn(boolean value) { this.noWarn = value; } /** * Shorthand for {@link #setLevel(String) level}{@code ="INFO"}: Messages of level INFO ("normal output") will be * suppressed. */ public void setQuiet(boolean value) { this.quiet = value; } /** * Shorthand for {@link #setLevel(String) level}{@code ="INFO"}: Messages of level INFO ("normal output") and above * (WARNING and SEVERE) will be logged. */ public void setNormal(boolean value) { this.normal = value; } /** * Shorthand for {@link #setLevel(String) level}{@code ="CONFIG"}: Messages of level CONFIG ("verbose output") will * be logged. */ public void setVerbose(boolean value) { this.verbose = value; } /** * Alias for {@link #setLevel(String) level}{@code ="FINE|FINER|FINEST"}. */ public void setDebug(Debug value) { this.debug = value; } // SUPPRESS CHECKSTYLE LineLength:8 /** * Sets the level of the named loggers, adds the given handler on them and sets the given * formatter on the handler. *

* The spec is parsed as follows: *

*
     * spec := [ level ] [ ':' [ logger-names ] [ ':' [ handler ] [ ':' [ formatter ] ] ] ]
     * logger-names := logger-name [ ',' logger-name ]...
     * 
*

* The level component must be parsable by {@link Level#parse(String)}, i.e. it must be a decimal * number, or one of {@code SEVERE}, {@code WARNING}, {@code INFO}, {@code CONFIG}, {@code FINE}, {@code FINER}, * {@code FINEST} or {@code ALL}. *

*

* The handler and formatter components denote {@link Parser expressions}, with the * automatically imported packages "java.util.logging", "de.unkrig.commons.util.logging.handler" and * "de.unkrig.commons.util.logging.formatter". *

*

* Example spec: *

*
     * FINE:de.unkrig:ConsoleHandler:FormatFormatter("%5$tF %5$tT.%5$tL %10$-20s %3$2d %8$s%9$s%n")
     * 
*

* If any of the components of the spec is missing or empty, a reasonable default value is assumed: *

*
*
level
*
* Logger: Level inherited from parent logger *
* Handler: Handler's default log level, typically {@code ALL} *
* *
loggers
*
* The root logger *
* *
handler
*
* {@link StdoutHandler} *
* *
formatter
*
* {@link PrintfFormatter#MESSAGE_AND_EXCEPTION MESSAGE_AND_EXCEPTION} *
*
*/ public void setSpec(String spec) { this.spec = spec; } /** * The formatter to use. * * @see SimpleLogging#setFormatter(String) */ public void setFormatter(String spec) { this.formatter = spec; } // END CONFIGURATION SETTERS /** * The ANT task "execute" method. * * @see Task#execute() */ @Override public void execute() throws BuildException { try { if (this.out != null) SimpleLogging.setOut(this.out); if (this.stdout) SimpleLogging.setStdout(); if (this.level != null) SimpleLogging.setLevel(this.level); if (this.noWarn == Boolean.TRUE) SimpleLogging.setNoWarn(); if (this.quiet == Boolean.TRUE) SimpleLogging.setQuiet(); if (this.normal == Boolean.TRUE) SimpleLogging.setNormal(); if (this.verbose == Boolean.TRUE) SimpleLogging.setVerbose(); if (this.debug != null) { switch (this.debug) { case FINE: SimpleLogging.setLevel(Level.FINE); break; case FINER: SimpleLogging.setLevel(Level.FINER); break; case FINEST: SimpleLogging.setLevel(Level.FINEST); break; default: throw new IllegalArgumentException(); } } if (this.formatter != null) SimpleLogging.setFormatter(this.formatter); if (this.spec != null) SimpleLogging.configureLoggers(this.spec); } catch (Exception e) { throw new BuildException(e); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy