Possible to use different log4j appenders for different methods? - java

I have a log4j configuration file and it uses a ConsoleAppender with a specific layout.
I use
logger.info("some text");
in several places in my java code.
Is it possible to create another appender with a different layout in the configuration file and use the specific appender wherever I choose in the code?

Is it possible to create another appender with a different layout in the configuration file and use the specific appender wherever I choose in the code?
Yes you can create different appender with a different layout and create different categories for each appender.
use the category name to get the logger instead of appender name.
Sample: (log4j.xml)
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="stdout1" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%.4t] %-5p %c{1} - %m%n" />
</layout>
</appender>
<appender name="stdout2" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%.4t] %-5p %c{1} - %m%n" />
</layout>
</appender>
<category name="category1">
<priority value="INFO" />
<appender-ref ref="stdout1" />
</category>
<category name="category2">
<priority value="DEBUG" />
<appender-ref ref="stdout2" />
</category>
</log4j:configuration>
Java Code: (get the logger based on category name)
org.apache.log4j.Logger.getLogger("category1").warn("debug msg for stdout1 appender");
org.apache.log4j.Logger.getLogger("category2").info("info msg for stdout2 appender");
Output:
[main] WARN category1 - warn msg for stdout1 appender
2014-05-09 02:46:29,216 [main] INFO category2 - info msg for stdout2 appender

Related

Disable log4j and slf4j initialization output for external libraries

When running my application I see on the stdout the following log4j initialization infos:
log4j: Threshold ="null".
log4j: Level value for root is [OFF].
log4j: root level set to OFF
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n].
log4j: Adding appender named [console] to category [root].
This is probably coming from external libraries using log4j and slf4j.
I am trying to suppress these initialization infos, but even setting the root logger level to OFF does not seem to have any effect.
This is the log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<level value="OFF" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
Any suggestion?
Solved setting debug to false in log4j.xml:
<log4j:configuration debug="false"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<level value="OFF" />
<appender-ref ref="console" />
</root>
</log4j:configuration>

can't log Hibernate queries log4j

I am trying to log hibernate queries into a webapp specific log file.
I can log any message but I am still unable to log hibernate queries.
Them are being logged inside the server.log, even in a different file, but I need an app-specific log for each app running on the same server.
I am using log4j 1.2, I am able to create the log files, to log every information passed to console but hibernate queries.
Inside a database.properties file I found the hibernate.show_sql=true that is responsible for the logging output in console.
This is the kind of console's logging output I refer:
16:10:35,827 INFO [stdout] (http-localhost-127.0.0.1-8080-1) Hibernate: select [...](here is outputted the correct query passed, with the ? of the prepared statement)
Anyway, what I need is to log that output.
I tried different log4j.xml settings, but still with no success, it creates log files but does not log those outputs.
Here is the current setting, that is logging everything inside the same file, when I will be able to log even those information I will go on separating the logs.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
<appender name="mainFileAppender2" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="maxFileSize" value="100MB" />
<param name="maxBackupIndex" value="50" />
<param name="File" value="${webapp.root}/WEB-INF/logs/mainCORE.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c{1}:%L %m %n" />
</layout>
</appender>
<appender name="journaldev-hibernate2" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${webapp.root}/WEB-INF/logs/hib-queriesCORE.log" />
<param name="Append" value="false"/><!--value="true" /-->
<param name="ImmediateFlush" value="true" />
<param name="MaxFileSize" value="200MB" />
<param name="MaxBackupIndex" value="50" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %d{Z} [%t] %-5p (%F:%L) - %m%n" />
</layout>
</appender>
<root>
<priority value="debug"></priority>
<!-- <appender-ref ref="stdout"/> -->
<appender-ref ref="mainFileAppender2"/>
</root>
This configuration logs everything but those outputs. In the server.log are logged as follows:
16:10:35,827 INFO [stdout] (http-localhost-127.0.0.1-8080-1) Hibernate: select [...](rest of the query)
Configure the logger for the following categories:
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
The first one will log the SQL-Statement (with ? for parameter values), the second one will print those parameter values if you need them.
So something like this should do the trick in your case:
<category name="org.hibernate.SQL">
<appender-ref ref="journaldev-hibernate2"/>
<priority value="DEBUG"/>
</category>
<category name="org.hibernate.type">
<appender-ref ref="journaldev-hibernate2"/>
<priority value="TRACE"/>
</category>
Solved this way; Anyway it also extracts the values extracted from the query
log = ${jboss.server.log.dir}/log/
#Loggers
log4j.rootLogger = INFO, FILE
log4j.logger.org.hibernate.SQL = DEBUG, HIBERNATE
log4j.logger.org.hibernate.type = TRACE, HIBERNATE
#Appenders
# Main appender
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=${log}/main.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d %-5p %c{1}:%L %m %n
log4j.appender.FILE.ImmediateFlush=true
log4j.appender.FILE.Threshold=debug
log4j.appender.FILE.Append=true
log4j.appender.FILE.DatePattern=dd-MM-yyyy
# Hibernate queries appender
log4j.appender.HIBERNATE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.HIBERNATE.File=${log}/hib-queries.log
log4j.appender.HIBERNATE.layout=org.apache.log4j.PatternLayout
log4j.appender.HIBERNATE.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
log4j.appender.HIBERNATE.ImmediateFlush=true
log4j.appender.HIBERNATE.Threshold=debug
log4j.appender.HIBERNATE.Append=true
log4j.appender.HIBERNATE.DatePattern=dd-MM-yyyy

log4j appender priority conflict?

I have this log4j.properties:
# Global logging configuration
log4j.rootLogger=WARN, file
### Direct log messages to a log file
log4j.appender.file = org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File= C:\\eclipse\\servers\\apache-tomcat-6.0.39\\logs\\log4j.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ISO8601} %5p %c{1}:%L - %m%n
### Console messages Appender
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{ISO8601} %5p %c{1}:%L - %m%n
### Appender-to-Class definition
log4j.logger.com.XXX.payplatform.test.*=INFO, console
log4j.additivity.com.XXX.payplatform.test.*=false
So, ok, the WARN level information is being registered in the log4j.log file. That's OK.
But, I want to see INFO-level messages in my eclipse console, but i am not getting any response there.
Any solutions? I think the problem is in the "WARN, file" rootLogger configuration, so i tried this:
# Global logging configuration
log4j.rootLogger=INFO, file
But no changes resulted...
Thank you!
Read more Apache log4j 1.2 - Short introduction to log4j and find the samples as well.
Level Inheritance
The inherited level for a given logger C, is equal to the first non-null level in the logger hierarchy, starting at C and proceeding upwards in the hierarchy towards the root logger.
Basic Selection Rule
A log request of level p in a logger with (either assigned or inherited, whichever is appropriate) level q, is enabled if p >= q.
This rule is at the heart of log4j. It assumes that levels are ordered. For the standard levels, we have DEBUG < INFO < WARN < ERROR < FATAL.
It should be like this. Add console here and set default root log level to INFO.
log4j.rootLogger=INFO, file, console
OR
Try with log4j.xml instead of log4j.properties. Read more Configuring Log4j 2
Sample log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="FILE_LOG" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="/logs/log4j.log" />
<param name="Append" value="true" />
<param name="ImmediateFlush" value="true"/>
<param name="DatePattern" value="'.'dd-MM-yyyy-HH"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{ISO8601} %5p %c{1}:%L - %m%n" />
</layout>
</appender>
<category name="com.XXX.payplatform.test">
<priority value="INFO" />
<appender-ref ref="STDOUT"/>
</category>
<root>
<priority value ="ERROR" />
<appender-ref ref="FILE_LOG" />
</root>
</log4j:configuration>

How to Write Java server logs to separate log file

Right now we are using a customized logger method for our application but now we have a stand alone code for which we need to write to a separate log file. We have little idea about log4j. I just want to know where to change properties if any so that i don't disturb the existing logger application as well as we write our logs into new log file.
First define a file appender:
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=/mylogfile.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
Then point your package to use this appender:
log4j.logger.mypackage=LOGFILE
log4j.additivity.mypackage=false
The last line is important if you do not want your package to inherit the global appender. Doing so will result in the log messages from mypackage also printed at the default appender.
in log4j you can direct log entries to separate files based upon pacakge name.
log4j.logger.your.package1= LOG,STDOUT
log4j.additivity.your.package1=false
log4j.logger.your.package2= DEBUG, STDOUT
log4j.additivity.your.package2=false
I use XML configuration.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="logfile" class="org.apache.log4j.FileAppender">
<param name="file" value="app.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d-%5p(%F:%L)-%m%n"/>
</layout>
</appender>
<appender name="myLogfile" class="org.apache.log4j.FileAppender">
<param name="file" value="myFile.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d-%5p(%F:%L)-%m%n"/>
</layout>
</appender>
<logger name="org.myApp">
<level value="INFO"/>
<appender-ref ref="myLogfile" />
</logger>
<root>
<level value="ERROR" />
<appender-ref ref="logfile" />
</root>
</log4j:configuration>
Just use FileAppender and specify a new filename in the constructor.

How to get java Logger output to file by default [duplicate]

This question already has answers here:
Where does java.util.logging.Logger store their log
(3 answers)
Closed 9 years ago.
Netbeans thoughtfully sprinkles Logger.getLogger(this.getClass().getName()).log(Level. [...]
statements into catch blocks. Now I would like to point them all to a file (and to console).
Every logging tutorial and such only me tells how to get a specific logger to output into a file, but I assume there is a better way than fixing every automatically generated logging statement? Setting a handler for some sort of root logger or something?
I just add the following at startup
Handler handler = new FileHandler("test.log", LOG_SIZE, LOG_ROTATION_COUNT);
Logger.getLogger("").addHandler(handler);
You can specify your own values for LOG_SIZE and LOG_ROTATION_COUNT
You may need adjust the logging level to suit.
You have to define where the log is writting in the logger configuration file.
For example, if you use log4j, a log4j.xml (or log4j.properties) file will contain such information.
For example, here is a simple log4j.xml file that logs directly into a file (my-app.log) and in the console:
<?xml version="1.0" encoding="UTF-8"?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="rolling" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="my-app.log" />
<param name="DatePattern" value=".yyyy-MM-dd" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p [%C] [IP=%X{ipAddress}] [user=%X{user}] %m%n" />
</layout>
</appender>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p [%C] [user=%X{user}] %m%n" />
</layout>
</appender>
<root>
<priority value="info" />
<appender-ref ref="console" />
<appender-ref ref="rolling" />
</root>
</log4j:configuration>

Categories

Resources