You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by "Remko Popma (JIRA)" <ji...@apache.org> on 2015/01/23 02:55:34 UTC

[jira] [Commented] (LOG4J2-943) How to configure StatisticsCsvLayout in log4j2 ?

    [ https://issues.apache.org/jira/browse/LOG4J2-943?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14288585#comment-14288585 ] 

Remko Popma commented on LOG4J2-943:
------------------------------------

If I may offer a quick suggestion: if the org.perf4j StatisticsCsvLayout is not directly compatible with log4j2, you may need to create your own (or convince the perf4j people to port this layout to log4j2).

Fortunately creating a custom layout is not very difficult. Here is some sample code. You can also look at the log4j2 source code for more details, like how to pass parameters.

{code}
@Plugin(name = "StatisticsCsvLayout", category = "Core", elementType = "layout", printObject = true)
public final class StatisticsCsvLayout extends AbstractLayout<LogEvent> {

    public StatisticsCsvLayout(String param1, String param2) {
         // TODO you may or may not need parameters, feel free to add or remove them
    }

    @Override
    public byte[] toByteArray(LogEvent event) {
        final byte[] result = createCsv(event);
        return result;
    }

    private byte[] createCsv(LogEvent event) {
          // TODO your implementation goes here
    }

    @Override
    public LogEvent toSerializable(LogEvent event) {
        return event;
    }
    @Override
    public String getContentType() {
        return "text/csv";
    }
    @Override
    public Map<String, String> getContentFormat() {
        return new HashMap<String, String>();
    }
    @PluginFactory
    public static StatisticsCsvLayout createLayout() {
            @PluginAttribute("param1") final String param1,
            @PluginAttribute("param2") final String param2) { // add more if necessary...
        return new StatisticsCsvLayout(param1, param2);
    }
}
{code}

You would then configure like this (you may not need the packages declaration):
{code}
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" packages="package.of.layout.class">
  <Appenders>
    <File name="file" fileName="log.csv">
      <StatisticsCsvLayout param1="someValue" />
    </File>
  </Appenders>
  
  <Loggers>
    <Root level="trace" includeLocation="false">
      <AppenderRef ref="file"/>
    </Root>
  </Loggers>
</Configuration>
{code}


> How to configure StatisticsCsvLayout in log4j2 ?
> ------------------------------------------------
>
>                 Key: LOG4J2-943
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-943
>             Project: Log4j 2
>          Issue Type: Question
>          Components: Appenders, Layouts, Plugins
>    Affects Versions: 2.1
>         Environment: Windows7, Java
>            Reporter: Mike Andrew
>            Priority: Critical
>              Labels: log4j2, log4j2.xml
>             Fix For: 2.1
>
>
> Need help from migrating log4j to log4j2. I have a rollingFileAppender. I need this appender to make use of "StatisticsCsvLayout" in org.perf4j. Previously in log4j 1.x version we used to directly refer the StatisticsCsvLayout from any appender using following tag in log4j.xml.
> <Layout class="org.perf4j.log4j.StatisticsCsvLayout"/>
> We are unable to confire this from log4j2.xml as there is no provision to make use of external layouts directly. Please tell me how to configure this in log4j2.xml.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org