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 "Sudharma Puranik (JIRA)" <ji...@apache.org> on 2013/07/23 12:30:48 UTC

[jira] [Comment Edited] (LOG4J2-314) How to create multiple appenders at runtime, sharing an AsyncAppender?

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

Sudharma Puranik edited comment on LOG4J2-314 at 7/23/13 10:28 AM:
-------------------------------------------------------------------

check this example probably you will understand how Routing works.

{noformat}
public static void main(String[] args) {
		ThreadContext.put("fruits", "apple");
		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");

		for (int i = 0; i <= 5000; i++) {
			logger.debug(" Loop 1 " + i);
		}
//		ThreadContext.remove("requestId");

		 ThreadContext.put("fruits", "orange");
		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");

		logger.debug("Sudharma Tested");
		for (int i = 0; i <= 500000; i++) {
			logger.debug("Loop 1 " + i);
		}
		logger.info("This message is written to the default log file");

	}
{noformat}


*log4j2.xml*
{noformat}
<Routing name="Routing">
			<Routes pattern="$${ctx:fruits}">
				<Route key="apple">
					<RollingFile name="Rolling-${ctx:fruits}" fileName="logs/apple-${ctx:fruits}.log"
						filePattern="./logs/${date:yyyy-MM}/${ctx:fruits}-apple-%d{yyyy-MM-dd}-%i.log.gz">
						<PatternLayout>
							<pattern>%d{ISO8601} [%t] %p %c{3} %L - %m%n</pattern>
						</PatternLayout>
						<Policies>
							<TimeBasedTriggeringPolicy interval="6"
								modulate="true" />
							<SizeBasedTriggeringPolicy size="10 MB" />
						</Policies>
					</RollingFile>
				</Route>
				<Route>
					<RollingFile name="Rolling-${ctx:fruits}" fileName="logs/other-${ctx:fruits}.log"
						filePattern="./logs/${date:yyyy-MM}/${ctx:fruits}-other-%d{yyyy-MM-dd}-%i.log.gz">
						<PatternLayout>
							<pattern>%d{ISO8601} [%t] %p %c{3} %L - %m%n</pattern>
						</PatternLayout>
						<Policies>
							<TimeBasedTriggeringPolicy interval="6"
								modulate="true" />
							<SizeBasedTriggeringPolicy size="10 MB" />
						</Policies>
					</RollingFile>
				</Route>
			</Routes>
		</Routing>
{noformat}

Fruits with Apple go to apple log, Now for orange its not defined so it will go to others, the point is that whatever you route you have to add it to ThreadContext, even default.
                
      was (Author: sudharma.puranik@gmail.com):
    check this example probably you will understand how Routing works.

{noformat}
public static void main(String[] args) {
		ThreadContext.put("fruits", "apple");
		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");

		for (int i = 0; i <= 5000; i++) {
			logger.debug(" Loop 1 " + i);
		}
//		ThreadContext.remove("requestId");

		 ThreadContext.put("fruits", "orange");
		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");

		logger.debug("Sudharma Tested");
		for (int i = 0; i <= 500000; i++) {
			logger.debug("Loop 1 " + i);
		}
		logger.info("This message is written to the default log file");

	}
{noformat}


*log4j2.xml*
{noformat}
<Routing name="Routing">
			<Routes pattern="$${ctx:fruits}">
				<Route key="apple">
					<RollingFile name="Rolling-${ctx:fruits}" fileName="logs/apple-${ctx:fruits}.log"
						filePattern="./logs/${date:yyyy-MM}/${ctx:fruits}-apple-%d{yyyy-MM-dd}-%i.log.gz">
						<PatternLayout>
							<pattern>%d{ISO8601} [%t] %p %c{3} %L - %m%n</pattern>
						</PatternLayout>
						<Policies>
							<TimeBasedTriggeringPolicy interval="6"
								modulate="true" />
							<SizeBasedTriggeringPolicy size="10 MB" />
						</Policies>
					</RollingFile>
				</Route>
				<Route>
					<RollingFile name="Rolling-${ctx:fruits}" fileName="logs/other-${ctx:fruits}.log"
						filePattern="./logs/${date:yyyy-MM}/${ctx:fruits}-other-%d{yyyy-MM-dd}-%i.log.gz">
						<PatternLayout>
							<pattern>%d{ISO8601} [%t] %p %c{3} %L - %m%n</pattern>
						</PatternLayout>
						<Policies>
							<TimeBasedTriggeringPolicy interval="6"
								modulate="true" />
							<SizeBasedTriggeringPolicy size="10 MB" />
						</Policies>
					</RollingFile>
				</Route>
			</Routes>
		</Routing>
{noformat}
                  
> How to create multiple appenders at runtime, sharing an 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: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org