java - How to use log4j to log different package's message into same log file? -
i beginner using log4j, please don't mind stupid questions.
now have jar contain methods use in different classes. let calculator method. method has several messages , exception handling need log.
i have 2 classes need use method calculation , these 2 classes have own log file. classa.log , classb.log
the question how log message , error came calculator in classa.log , classb.log depends on class calling calculator.
i want classa.log , classb.log like:
[datetime] info - [foo.a] class [datetime] info - [foo.a] calling calculator [datetime] info - [cal.mycal.calculator] enter calculator , start calculating [datetime] warn - [cal.mycal.calculator] warning message calculator [datetime] error - [cal.mycal.calculator] error message calculator [datetime] info - [foo.a] mistakes made. [datetime] info - [foo.a] class done
in log4j.xml, need add logger , ref class need log
<logger name="foo.a"> <level value="info" /> <appender-ref ref="a" /> </logger> <logger name="cal.mycal.calculator"> <level value="info" /> <appender-ref ref="a" /> </logger>
and appender
<appender name="a" class="org.apache.log4j.dailyrollingfileappender"> <param name="file" value="log/classa.log"/> <param name="datepattern" value="'.'yyyy-mm-dd"></param> <param name="append" value="true"/> <layout class="org.apache.log4j.patternlayout"> <param name="conversionpattern" value="[%d{dd-mmm-yyyy hh:mm:ss}] %-5p - [%c] %m%n"/> </layout> </appender>
but after doing this, method call foo.b won't write classb.log. message calculator write classa.log
how logger:
protected final log log = logfactory.getlog(getclass());
can have suggestions? thank you.
Comments
Post a Comment