001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.layout; 018 019import java.io.IOException; 020import java.nio.charset.Charset; 021 022import org.apache.commons.csv.CSVFormat; 023import org.apache.commons.csv.CSVPrinter; 024import org.apache.commons.csv.QuoteMode; 025import org.apache.logging.log4j.core.Layout; 026import org.apache.logging.log4j.core.LogEvent; 027import org.apache.logging.log4j.core.config.Configuration; 028import org.apache.logging.log4j.core.config.Node; 029import org.apache.logging.log4j.core.config.plugins.Plugin; 030import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 031import org.apache.logging.log4j.core.config.plugins.PluginConfiguration; 032import org.apache.logging.log4j.core.config.plugins.PluginFactory; 033import org.apache.logging.log4j.status.StatusLogger; 034 035/** 036 * A Comma-Separated Value (CSV) layout to log events. 037 * 038 * Depends on Apache Commons CSV 1.2. 039 * 040 * @since 2.4 041 */ 042@Plugin(name = "CsvLogEventLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true) 043public class CsvLogEventLayout extends AbstractCsvLayout { 044 045 public static CsvLogEventLayout createDefaultLayout() { 046 return new CsvLogEventLayout(null, Charset.forName(DEFAULT_CHARSET), CSVFormat.valueOf(DEFAULT_FORMAT), null, null); 047 } 048 049 public static CsvLogEventLayout createLayout(final CSVFormat format) { 050 return new CsvLogEventLayout(null, Charset.forName(DEFAULT_CHARSET), format, null, null); 051 } 052 053 @PluginFactory 054 public static CsvLogEventLayout createLayout( 055 // @formatter:off 056 @PluginConfiguration final Configuration config, 057 @PluginAttribute(value = "format", defaultString = DEFAULT_FORMAT) final String format, 058 @PluginAttribute("delimiter") final Character delimiter, 059 @PluginAttribute("escape") final Character escape, 060 @PluginAttribute("quote") final Character quote, 061 @PluginAttribute("quoteMode") final QuoteMode quoteMode, 062 @PluginAttribute("nullString") final String nullString, 063 @PluginAttribute("recordSeparator") final String recordSeparator, 064 @PluginAttribute(value = "charset", defaultString = DEFAULT_CHARSET) final Charset charset, 065 @PluginAttribute("header") final String header, 066 @PluginAttribute("footer") final String footer) 067 // @formatter:on 068 { 069 070 final CSVFormat csvFormat = createFormat(format, delimiter, escape, quote, quoteMode, nullString, recordSeparator); 071 return new CsvLogEventLayout(config, charset, csvFormat, header, footer); 072 } 073 074 protected CsvLogEventLayout(final Configuration config, final Charset charset, final CSVFormat csvFormat, final String header, final String footer) { 075 super(config, charset, csvFormat, header, footer); 076 } 077 078 @Override 079 public String toSerializable(final LogEvent event) { 080 final StringBuilder buffer = getStringBuilder(); 081 // Revisit when 1.3 is out so that we do not need to create a new 082 // printer for each event. 083 try (final CSVPrinter printer = new CSVPrinter(buffer, getFormat())) { 084 printer.print(event.getNanoTime()); 085 printer.print(event.getTimeMillis()); 086 printer.print(event.getLevel()); 087 printer.print(event.getThreadId()); 088 printer.print(event.getThreadName()); 089 printer.print(event.getThreadPriority()); 090 printer.print(event.getMessage().getFormattedMessage()); 091 printer.print(event.getLoggerFqcn()); 092 printer.print(event.getLoggerName()); 093 printer.print(event.getMarker()); 094 printer.print(event.getThrownProxy()); 095 printer.print(event.getSource()); 096 printer.print(event.getContextMap()); 097 printer.print(event.getContextStack()); 098 printer.println(); 099 return buffer.toString(); 100 } catch (final IOException e) { 101 StatusLogger.getLogger().error(event.toString(), e); 102 return getFormat().getCommentMarker() + " " + e; 103 } 104 } 105 106}