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

Mark Bowman commented on LOG4J2-1628:
-------------------------------------

{noformat}
package org.lcogt.commons.log;

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.layout.AbstractStringLayout;
import org.apache.logging.log4j.message.Message;
import org.json.simple.JSONValue;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.Date;

/**
 * A Log4J layout that formats messages to adhere to LCOGT standards
 * http://intranet.lco.gtn/Software:Logging_Best_Practice
 * Created by ariba on 8/26/15.
 * Ported to log4j2 by mbowman 4th November 2015
 */
@Plugin(name = "LCOGTLayout", category = "Core", elementType = "layout")
public class LCOGTLayout extends AbstractStringLayout
{
    protected LCOGTLayout(Charset charset)
    {
        super(charset);
    }

    @PluginFactory
    public static LCOGTLayout createLayout(@PluginAttribute(value = "charset", 
defaultString = "UTF-8") Charset charset)
    {
        return new LCOGTLayout(charset);
    }

    private static final ConcurrentDateFormatAccess dfa = new 
ConcurrentDateFormatAccess();


    @Override
    public String toSerializable(LogEvent loggingEvent)
    {
        Message message = loggingEvent.getMessage();
        StringBuilder builder = new StringBuilder();
        builder.append(dfa.format(new 
Date(loggingEvent.getTimeMillis()))).append(" ");
        builder.append(loggingEvent.getLevel()).append(": ");
        builder.append(loggingEvent.getLoggerName()).append(": ");

        builder.append(message.getFormattedMessage());

        if (loggingEvent.getThrown() != null)
        {
            
builder.append("\n").append(getStackTrace(loggingEvent.getThrown()));
        }

        builder.append(" | ");

        builder.append(JSONValue.toJSONString(loggingEvent.getContextMap()));

        builder.append("\n");
        return builder.toString();
    }

    /**
     * Get the stack trace for the specified exception as a string
     *
     * @param t Exception to get stack trace
     * @return Stack trace as string
     */
    private String getStackTrace(Throwable t)
    {
        String stackTrace = "";
        if (t != null)
        {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw, true);
            t.printStackTrace(pw);
            pw.flush();
            sw.flush();
            stackTrace = sw.toString();
        }
        return stackTrace;
    }
}
{noformat}

> File locking regression for Async FileAppender
> ----------------------------------------------
>
>                 Key: LOG4J2-1628
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1628
>             Project: Log4j 2
>          Issue Type: Bug
>          Components: Appenders
>    Affects Versions: 2.6.2
>         Environment: centos
>            Reporter: Mark Bowman
>
> We have several threads writing to an async file with locking set to true. 
> Under log4j 2.5 the message from each thread are interleaved correctly. Using 
> log4j 2.6.2 some messages are scrambled as if multiple threads are writing to 
> the file simultaneously. Reverting to 2.5 fixes the problem.
> Configuration to follow.



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

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to