I am using log4j 1.2 and I want to achieve the following behaviour:
I want to be able to log from all classes in the package. The log should be written in a detailed version (including info and debug messages) to a file, but I want to receive a short version (errors and fatals) as String.
From the documentation I guessed that I probably have to define two different logger instances which are ancestors or each other and assign appenders to them. But I neither know the details nor do I know if this is the intended way to achieve this behaviour.
Can somebody please give me a hint?
Define only one logger which you affect two file appenders: one with level error and the other with the debug level. The level filtering will be done in the appender.
<appender name="DebugAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logs/debug.log" />
<param name="MaxBackupIndex" value="5" />
<param name="MaxFileSize" value="5MB" />
<param name="threshold" value="debug" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %t %-5p %c - %m%n" />
</layout>
</appender>
<appender name="ErrorAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="logs/error.log" />
<param name="MaxBackupIndex" value="5" />
<param name="MaxFileSize" value="5MB" />
<param name="threshold" value="error" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %t %-5p %c - %m%n" />
</layout>
</appender>
<logger name="mylogger">
<level value="debug" />
<appender-ref ref="DebugAppender" />
<appender-ref ref="ErrorAppender" />
</logger>
You should be able to use the same logger and just apply a filter to the handlers, i.e. set the log level for the console handler (which I assume you mean with "as String") to ERROR.
Related
I want to create two log file.
Because my project has two modules and log each module in separate log files.
After that ,I have to log each module logging data independently .i have Single Log4j file.when i use appender -ref inside the root then i can achive but at that time aop or java logging cant appear in that file only manualy logger msg shown there
Try Something like :
<log4j:configuration>
<appender name="fileAppender1" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="ALL" />
<param name="MaxFileSize" value="3KB" />
<param name="MaxBackupIndex" value="10" />
<param name="File" value="F:/logs/Testing/Project_moduleOne.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM-dd-yyyy HH:mm:ss:SSS} %-5p %m%n" />
</layout>
</appender>
<appender name="fileAppender2" class="org.apache.log4j.RollingFileAppender">
<param name="Threshold" value="ALL" />
<param name="MaxFileSize" value="3KB" />
<param name="MaxBackupIndex" value="10" />
<param name="File" value="F:/logs/PAD_Testing/Project_moduleTwo.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{MMM-dd-yyyy HH:mm:ss:SSS} %-5p %m%n" />
</layout>
</appender>
<!--sets the priority log level for org.springframework -->
<logger name="com.sp.logger1" additivity="false">
<appender ref="fileAppender1"/>
</logger>
<logger name="com.sp.logger2" additivity="false">
<appender ref="fileAppender2" />
</logger>
<!--sets the default priority log level -->
<root>
<priority value="all"></priority>
<appender-ref ref="fileAppender1" />
<appender-ref ref="fileAppender2" />
</root>
</log4j:configuration>
I'm trying to use logger in java jvm. but I can't change the logger mode.
I can see only "info" message and I want to see also "debug" message.
I tried to change to debug mode in log4j.xml in this way:
``<appender name="DISCOVERY" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/tmp/ncs-logs/jvm-discovery-app.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="100"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c{2}]:%L %m%n"/>
</layout>
</appender>
<!-- value="<%-4p> %d{dd-MMM-yyyy::HH:mm:ss.SSS} %C{1} %t: - %m%n" -->
<logger name="com.lte.btscompact.mngmt.sync.app">
<level value="DEBUG" />
<appender-ref ref="DISCOVERY"/>
</logger>
<root>
<appender-ref ref="DISCOVERY" />
</root>
</log4j:configuration>
and in cli I did this command:
set java-vm java-logging logger com.tailf.packages.ned.acs level level-all
but I still can't see debug message.
what else should I do?
Double-check your appender config - it must not have a threshold higher than DEBUG:
<param name="Threshold" value="INFO"/>
versus
<param name="Threshold" value="DEBUG"/>
I'm helping a friend with a project. He uses Spring; I am not. I gave him a log4j.xml file I use to enable logging to a file in a logs/ subdirectory. He tried the log4j.xml but his app is ignoring it (doesn't react to its presence or absence), sending output to the console. It appears Spring is auto-configuring log4j to do this. We also tried -Ddebug.log4j=true to find discern more about log4j's initialization when Spring is involved, but that parameter had no effect.
By default, log4j doesn't make any assumptions about its configuration, it definitely expects either a configuration file or a programmatic configuration.
It scans through the classpath for log4j.properties or log4j.xml. If you prefer a different path and different name, you need pass a system variable something like
-Dlog4j.configuration=relative_path_to_file.xml
Right now in our project
we use below xml file
<appender name="EvolutionWebASYNC" class="org.apache.log4j.AsyncAppender">
<appender-ref ref="logFileOut" />
</appender>
<appender name="logFileOut" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="C:/home/wasadmin/applogs/myproject/myproject.log" />
<param name="Append" value="true" />
<param name="Threshold" value="ALL" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="5" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d %-5p - [TXID: %X{TXID}] [UID: %X{UID}] [ORGOID: %X{ORGOID}] [UA_ORGOID: %X{UA_ORGOID}] - %m%n" />
</layout>
</appender>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d %-5p - [TXID: %X{TXID}] [UID: %X{UID}] [ORGOID: %X{ORGOID}] [UA_ORGOID: %X{UA_ORGOID}] - %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="EvolutionWebASYNC" />
</root>
I hope it helps !!!
I have a huge code base which serves info requests about colleges. There is a base framework code, and we have some separate packages for college1, college2, etc.,
Whenever there is a request for some info about college X, both framework code and college specific code both are triggered. I want to see logs generated for all requests for Company X to go to one location, and company Y to go to one location.
How do I use JUL or apache's logging.properties file to achieve this ? How do I ensure logs written by common framework libraries for company X go to one location, and logs written by common framework libraries for company Y go to some other location ?
I would have an xml file that contain the appenders to specify what packages go into what log file.
You can then load the xml file in your application entry point. Below is an example I have used in my current project
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<param name="threshold" value="error" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%t] %p %c - %m%n" />
</layout>
</appender>
<appender name="codebaseXAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="codebaseX.log" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%t] %d %p %c - %m%n"/>
</layout>
</appender>
<appender name="codebaseYAppender" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="codebaseY.log" />
<param name="MaxFileSize" value="10MB" />
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%t] %d %p %c - %m%n"/>
</layout>
</appender>
<!-- Root Logger -->
<root>
<priority value="error" />
<appender-ref ref="console" />
</root>
<!-- Application logger -->
<logger name="com.codebasePackageX" additivity="true">
<level value="info" />
<appender-ref ref="codebaseXAppender" />
</logger>
<!-- Application logger -->
<logger name="com.codebasePackageY" additivity="true">
<level value="info" />
<appender-ref ref="codebaseYAppender" />
</logger>
</log4j:configuration>
Is there a way to specify a pattern for an original filename?
I essentially want to do the following:
<param name="file" value="%d{yyyy/MM/dd HH:mm:ss}: %m%n_testlog.log"/>
How would I go about doing this?
I have a daily rolling log but I do it a little differently, using TimeBasedRollingPolicy from log4j extras:
<log4j:configuration debug="true">
<appender name="roll" class="org.apache.log4j.rolling.RollingFileAppender">
<param name="append" value="true" />
<param name="encoding" value="UTF-8" />
<!-- The active file to log to -->
<param name="file" value="/location/to/log/current.log />
<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
<param name="FileNamePattern"
value="/location/to/log/archive/oldLog-%d{yyyy-MM-dd}.log" />
</rollingPolicy>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{DATE} %5p %c{2} - %m%n" />
</layout>
</appender>
<logger name="mypackage">
<level value="debug" />
<appender-ref ref="roll" />
</logger>
</log4j:configuration>
The above is in my code, which I based on this entry at the log4j Wiki.
Paul
I'm not sure this would help you:
<param name="File" value="Info"/>
<param name="DatePattern" value="yyyy-MM-dd-HH-mm-ss'_testlog.log'"/>