[
https://issues.apache.org/jira/browse/LOG4J2-314?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13714346#comment-13714346
]
Remko Popma commented on LOG4J2-314:
------------------------------------
[~sdeboy]: One thread (AsyncLoggers or AsyncAppender) should be fine, as long
as on the back end, the message ends up in the correct log file. It also should
not be necessary to create special Logger objects.
[[email protected]]: Thanks for the pointer to the RoutingAppender!
[[email protected]]: I was guessing that you were trying to connect
your custom log configuration code to the XML configuration in order to re-use
the AsyncAppender defined in the XML.
I think there is a simpler way to do what you want to do that does not involve
any custom log configuration code. You should be able to get rid of the
NAPLogger class and just use XML configuration. I'm thinking the application
code can look like this:
{code}
// No need to create/configure a custom "dumpLogger" in your program: just use
a standard Logger
private static final Logger logger = LogManager.getLogger(MyClass.class);
private void someMethod(String requestId) {
ThreadContext.put("requestId", requestId);
logger.info("This message is written to a new file with requestId in the
file name");
logger.info("Another message written to that same file");
ThreadContext.remove(requestId);
logger.info("This message is written to the default log file");
}
{code}
Sudharma, would this solve your problem? Note: with this solution you cannot
set the MAX-SIZE for rollover...
It may take a bit of experimenting with the XML configuration to set up a
RoutingAppender that creates a new file for each unique requestId. The pattern
that you would use with the above example is $$\{ctx:requestId\}.
Here is an example configuration file. (I haven't tried this but it may help
get you started.)
{code}
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="DEBUG" name="MyApp" packages="">
<appenders>
<!-- SUMMARY FILE APPENDER -->
<RollingFile name="SUMMARY_ALL" fileName="./logs/css-summary.log"
filePattern="logs/$${date:yyyy-MM}/summary-%d{yyyy-MM-dd-HH}-%i.log.gz">
<PatternLayout>
<pattern>%d{ISO8601} [%t] %p %c %L - %m%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="6" modulate="true" />
<SizeBasedTriggeringPolicy size="50 MB" />
</Policies>
</RollingFile>
<!-- Routing sends messages to a separate dump file if ThreadContext has
key "requestId",
other events are sent to the SUMMARY_ALL log file. -->
<Routing name="Routing">
<Routes pattern="$${ctx:requestId}">
<Route>
<RollingFile name="Rolling-${ctx:requestId}"
fileName="logs/dump-${ctx:requestId}.log"
filePattern="./logs/${date:yyyy-MM}/${ctx:requestId}-dump-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<pattern>%d{ISO8601} [%t] %p %c %L - %m%n</pattern>
</PatternLayout>
<SizeBasedTriggeringPolicy size="500 MB" />
</RollingFile>
</Route>
<Route appender-ref="SUMMARY_ALL" key="Summary"/>
</Routes>
</Routing>
<!-- Async appender wraps the Routing appender, so all logging is
asynchronous. -->
<Async name="Async" bufferSize="256">
<appender-ref ref="Routing" />
</Async>
</appenders>
<loggers>
<root level="DEBUG">
<appender-ref ref="Async" />
</root>
</loggers>
</configuration>
{code}
> Multiple thread creation problem with AsyncAppender
> ---------------------------------------------------
>
> Key: LOG4J2-314
> URL: https://issues.apache.org/jira/browse/LOG4J2-314
> Project: Log4j 2
> Issue Type: Question
> Components: Appenders, Configurators
> Reporter: Sudharma Puranik
> Attachments: asyncAppendThread.png, log4j2.xml, loggerThread.png,
> TestAppender.zip
>
>
> I have a log4j2.xml configuration file from which I am getting the Logger,
> attached is the snapshot. when my logger is created a new thread is created.
> Programmatically,I create a RollingFileAppender and attach the it to
> AsyncAppender . I attach the Async Appender to the configuration of the
> log4j.xml, And when I start the Async Appender again a new thread is created.
> Is this a bug? because I get a logger a new thread is generated and when I
> create a async appender another thread is created and both are doing the same
> job.
> Also there is already a asyncappender in my log4j2.xml and I am creating one
> via program. Will this cause a problem? I did this because I am currently
> unable to hook my programmatically created appender to the AsyncAppender
> defined in log4j2.xml.
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]