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

org.perf4j.log4j.StatisticsCsvLayout Maven / Gradle / Ivy

Go to download

Performance statistics logging and monitoring toolkit extension to log4j and the java.util.logging framework.

There is a newer version: 0.9.16
Show newest version
/* Copyright (c) 2008-2009 HomeAway, Inc.
 * All rights reserved.  http://www.perf4j.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.perf4j.log4j;

import org.apache.log4j.Layout;
import org.apache.log4j.spi.OptionHandler;
import org.apache.log4j.spi.LoggingEvent;
import org.perf4j.helpers.GroupedTimingStatisticsCsvFormatter;
import org.perf4j.helpers.MiscUtils;
import org.perf4j.GroupedTimingStatistics;

/**
 * A layout that outputs {@link org.perf4j.GroupedTimingStatistics} instances as comma-separated values. Thus, this
 * layout is designed to be attached to appenders that are themselves attached to an
 * {@link AsyncCoalescingStatisticsAppender}.
 * 

* By default, each GroupedTimingStatistics object is output as a bunch of lines, with one line for each tagged * {@link org.perf4j.TimingStatistics} instance contained within the GroupedTimingStatistics object. The following * "columns" are output, separated by commas: *

    *
  1. tag - the tag name of the code block that the statistics refer to *
  2. start - the start time of timing window *
  3. stop - the stop time of the timing window *
  4. mean - the mean execution time of stop watch logs that completed in the timing window *
  5. min - the min execution time of stop watch logs that completed in the timing window *
  6. max - the max execution time of stop watch logs that completed in the timing window *
  7. stddev - the standard deviation of the execution times of stop watch logs that completed in the timing window *
  8. count - the count of stop watch logs that completed during the timing window *
*

* You can modify the columns output using the Columns option. For example, you could specify the Columns option * as "tag,start,stop,mean,count" to only output those specified values. In addition to the values specified above you * can also use "tps" to output transactions per second. *

* In addition to the default output of one line per tag for each GroupedTimingStatistics object, this layout also * supports a Pivot option which outputs just a single line for an entire GroupedTimingStatistics object. When * pivot is true you should set the Columns to specify the values from the specific tags you want to output. For * example, setting Pivot to true and setting Columns to "start,stop,codeBlock1Mean,codeBlock2Mean" would cause, for * each GroupedTimingStatistics object, a single line to be output with the start and stop times of the window, the * mean execution time for all stop watch logs with a codeBlock1 tag, and the mean execution time for all stop watch * logs with a codeBlock2 tag. * * @author Alex Devine */ public class StatisticsCsvLayout extends Layout implements OptionHandler { // --- configuration options --- /** * Pivot option */ private boolean pivot = false; /** * Columns option, a comma-separated list of column values to output. */ private String columns = GroupedTimingStatisticsCsvFormatter.DEFAULT_FORMAT_STRING; /** * PrintNotStatistics option */ private boolean printNonStatistics = false; // --- contained objects --- /** * The csvFormatter is created in the {@link #activateOptions} method. The work of actually formatting the * GroupedTimingStatistics object is delegated to this object. */ protected GroupedTimingStatisticsCsvFormatter csvFormatter; // --- configuration options --- /** * The Pivot option, which is false by default, determines whether or not a single line will be output for * each GroupedTimingStatistics object, or whether one line for each tag within a GroupedTimingStatistics object * will be output. * * @return the Pivot option. */ public boolean isPivot() { return pivot; } /** * Sets the value of the Pivot option. * * @param pivot The new Pivot option value. */ public void setPivot(boolean pivot) { this.pivot = pivot; } /** * The Columns option is a comma-separated list of the values that should be output for each line that * is printed. See the class javadoc for the allowed value. * * @return the Columns option. */ public String getColumns() { return columns; } /** * Sets the value of the Columns option. * * @param columns The new Columns option value. */ public void setColumns(String columns) { this.columns = columns; } /** * Gets the value of the PrintNonStatistics option. In general, this layout should only be used for * appenders that deal with GroupedTimingStatistics objects (e.g. a FileAppender attached to an * {@link AsyncCoalescingStatisticsAppender}). By default, any logging event where the message is NOT a * GroupedTimingStatistics object is not output. However, if this option is set to true, then * non-GroupedTimingStatistics messages will be output as their string value. * * @return the PrintNonStatistics option */ public boolean isPrintNonStatistics() { return printNonStatistics; } /** * Sets the value of the PrintNonStatistics option. * * @param printNonStatistics The new PrintNonStatistics option value. */ public void setPrintNonStatistics(boolean printNonStatistics) { this.printNonStatistics = printNonStatistics; } public String format(LoggingEvent event) { try { //we assume that the event is a GroupedTimingStatistics object return csvFormatter.format((GroupedTimingStatistics) event.getMessage()); } catch (ClassCastException cce) { //then it's not a GroupedTimingStatistics object if (isPrintNonStatistics()) { return MiscUtils.escapeStringForCsv(event.getMessage().toString(), new StringBuilder()) .append(MiscUtils.NEWLINE).toString(); } else { return ""; } } } /** * This layout ignores Throwables set on the LoggingEvent. * * @return true */ public boolean ignoresThrowable() { return true; } public void activateOptions() { csvFormatter = new GroupedTimingStatisticsCsvFormatter(isPivot(), getColumns()); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy