1

I have 2 different com components which are instantiated in same process. Both components are using Log4cxx DomConfigurator and configuring different log4cxx config xml file. Each specifies different paths for logs. But logs are created in the path specified by the com component which is instantiated latest.

I want these 2 COM Component logs will be in seperate file. How to achieve this?

srajeshnkl
  • 883
  • 3
  • 16
  • 49

3 Answers3

0

If you are using same process or thread then logcxx try to overwrite previous one. So better start another thread and assign logcxx to do next logging. Then everything will be fine.

Srijeyanthan
  • 126
  • 1
  • 2
  • I have 2 different components which will be running in same process. The 2 process are different. They both may or may not run in same time. In few times both may run or few times any one will be running they both won't be knowing whether another is running or not? If both are not running at same time there is no problem, When both components are running then logs are directed to latest one. How to handle it? – srajeshnkl May 03 '12 at 08:58
  • As you said, you are using two components which are both independent. Then you can start your logcxx separately without any problem. Only thing you have to change is in your xml configuration file. Just specify your log path. Here process or thread doesn't. matter. please check my answer below. – Srijeyanthan May 04 '12 at 07:17
0

Component 1 having this code or you can write in a logcxx wrapper common class which any component can use.

strPath = "AppLog1.dat";
log4cxx::BasicConfigurator::configure();
log4cxx::File cfgFile(strPath);
log4cxx::PropertyConfigurator::configureAndWatch(cfgFile, 60000);

Component 2 having the same code with different AppLog2.dat file

sample dat file .

#Log4cxx configuration file . 
#Final version of log4cxx config 
#Author - Sri  4/5/2012

# Root logger set to DEBUG using the A2 apender defined above. 
# Set options for appender named 'DT' 
# DT's layout is TTCC, using the 
# ISO8061 date format with context printing enabled. 

log4j.appender.DT=RollingFileAppender 
log4j.appender.DT.layout=TTCCLayout
log4j.appender.DT.File=.\\log\\log.log
log4j.appender.DT.layout.ContextPrinting=enabled
log4j.appender.DT.layout.DateFormat=ISO8601
log4j.appender.DT.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.DT.rollingPolicy.FileNamePattern=.\\log\\log-%d.log.zip




# Set options for appender named 'RawAppender' 
# RawAppender's layout is PatternLayout

log4j.appender.RawAppender=RollingFileAppender 
log4j.appender.RawAppender.layout=TTCCLayout
log4j.appender.RawAppender.File=.\\log\\Raw.log
log4j.appender.RawAppender.layout.ContextPrinting=enabled
log4j.appender.RawAppender.layout.DateFormat=ISO8601
log4j.appender.RawAppender.rollingPolicy=org.apache.log4j.rolling.
TimeBasedRollingPolicy
log4j.appender.RawAppender.rollingPolicy.FileNamePattern=.\\log\\RawData-%d.log.zip


# Set options for appender named 'ErrorAppender' 
# ErrorAppender's layout is TTCC, using the 
# ISO8061 date format with context printing enabled. 

log4j.appender.ErrorAppender=RollingFileAppender 
log4j.appender.ErrorAppender.MaxBackupIndex=10 
log4j.appender.ErrorAppender.layout=TTCCLayout
log4j.appender.ErrorAppender.File=.\\log\\Error.log
log4j.appender.ErrorAppender.layout.ContextPrinting=enabled
log4j.appender.ErrorAppender.layout.DateFormat=ISO8601
log4j.appender.ErrorAppender.rollingPolicy=org.apache.log4j.rolling.
TimeBasedRollingPolicy
log4j.appender.ErrorAppender.rollingPolicy.FileNamePattern=.\\log\\Error-%d.log.zip


# Root logger set to DEBUG using the A2 apender defined above. 

log4j.rootLogger=DEBUG, DT


# The logger 'DTError' inherits its level from the 
# logger hierarchy. Output will go to the appender's of the root 
# logger, DT in this case. 

log4j.logger.DTError=INHERIT,ErrorAppender

//////////////////////////////////////////////////////////////////////// When you compose your second component's configuration file , just change log path or file name.

cheers .

Srijeyanthan
  • 126
  • 1
  • 2
0

I realize that this thread is a couple of years old... but don't feel that it was answered.

Your question was how to output logging to different files per component. The title of this question 'Multiple DOM configurators in a single process'... isn't the right answer to what you're trying to do.

If you're using a DOM configuration, you can easily define multiple Appenders that will write to different files.

<appender name="LogFileA" class="org.apache.log4j.FileAppender">
    <param name="file" value="/tmp/logs/logA.log"/>
    <param name="append" value="true"/>

    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d %c %-5p (%F:%L) - %m%n"/>
    </layout>
</appender>

<appender name="LogFileB" class="org.apache.log4j.FileAppender">
    <param name="file" value="/tmp/logs/logB.log"/>
    <param name="append" value="true"/>

    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d %c %-5p (%F:%L) - %m%n"/>
    </layout>
</appender>

Then define a logger per component that will use the appropriate Appender.

<logger name="componentA">
    <priority value="all"/>
    <appender-ref ref="LogFileA"/>
</logger>

<logger name="componentB">
    <priority value="all"/>
    <appender-ref ref="LogFileB"/>
</logger>

Configure and retrieve a pointer to the appropriate logger:

log4cxx::xml::DOMConfigurator::configure("xml_config_file");
log4cxx::LoggerPtr componentA = log4cxx::Logger::getLogger("componentA");
log4cxx::LoggerPtr componentB = log4cxx::Logger::getLogger("componentB");

While I doubt this answer will help you... I hope that it will help someone else with a similar question.

AceFunk
  • 684
  • 1
  • 8
  • 14