We have a bunch of projects that get deployed in the same Jboss app. server. Each project has its own log4j.properties in its WEB-INF directory.
The idea is for each project to have its own logfile into which it writes its logs.
Any ideas on how this can be achieved.
First of all you don't mention the JBoss version you're using. You've to take in mind that JBoss versions prior to AS7 include its own log4j version (libraries and configuration file: conf/jboss-log4j.xml), so by default JBoss will ignore the log4j.properties file you've in each of your projects.
That said, you have two approaches to configure a log file for each application:
Centralized way: configure it in Jboss, through the
conf/jboss-log4j.xml. This way you won't need to modify your
applications, or
Modify your applications so that they take their own
log4j libraries (you'll have to include them in the ear/war), not
the Jboss ones.
For the first approach you'll have to define an appender for each of your applications, and then categories for the application packages, in the conf/jboss-log4j.xml file. For example, to configure a log file for an application called app1.war which classes are in a package called com.foo.app1, you'd have to add:
<appender name="APP1_APPENDER" class="org.jboss.logging.appender.DailyRollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.log.dir}/app1.log"/>
<param name="Append" value="true"/>
<!-- Rollover at midnight each day -->
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<!-- The default pattern: Date Priority [Category] (Thread) Message\n -->
<param name="ConversionPattern" value="%d %-5p [%c] (%t) %m%n"/>
</layout>
</appender>
...
...
<category name="com.foo.app1" additivity="true">
<priority value="INFO"/>
<appender-ref ref="APP1_APPENDER"/>
</category>
You'll have to add a customized set of blocks like this one for each project you want to define a log file for, respectively.
If you prefer the second approach, as said you'll have to include the log4j libraries in each project and isolate each application so that it takes the libraries included in the application not the ones from JBoss. Look at section 10.3.7 of the Jboss logging file for more information about this approach.
Related
I recently deployed a java app on my production server. In which I have installed Tomcat version 8.5 and I have been having problems since the log files stderr and stdout grow in size exaggeratedly and therefore I lower the performance of the server or sometimes it is necessary to restart it since it does not respond. I would like to know how to configure these files so that they create only a certain amount with a specific size. I have tried configuring it through log4j it has given me error when integrating this same a tomcat. I have also configured certain parameters (for example: java.util.logging.FileHandler.limit = 1024) for logging in which the size and quantity are assigned but these do not apply to the configuration and therefore I continue to throw the server .
In a production environment, a good practice is to use a process that works with small log files, and that creates a new one if the critical size has gone.
With log4j you can use a RollingFileAppender. See the minimal configuration :
<appender name="rolling.file.appender" class="org.apache.log4j.RollingFileAppender">
<param name="maxFileSize" value="50MB" />
<param name="maxBackupIndex" value="10" />
<param name="file" value="${catalina.base}/logs/myApplication.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<!-- Root Logger -->
<root>
<priority value="INFO" />
<appender-ref ref="rolling.file.appender" />
</root>
Documentation: https://tomcat.apache.org/tomcat-8.0-doc/logging.html
I have 4 applications which configured to log in same file opus-event.log and all have following configuration.
I am facing issue is each application logging in original(opus-event.log) as well as backup file like opus-event.log2015-10-16 and event.log2015-10-17 on date 19 oct.
<appender name="event" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Threshold" value="DEBUG"/>
<param name="file" value="${jboss.server.log.dir}/opus-event.log"/>
<param name="DatePattern" value="yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%m%n"/>
</layout>
</appender>
Please some one help me to know what issue it is ? is it possible to resolve using log4j configuration without changing log file name?
You can't simply log into one file from 4 separate applications - there needs to be someone who manages and synchronizes the writes to the file.
Possible solutions:
You can use multiple files (one for each application)
If all applications are running in one JVM (e.g. application server), you might be able to configure them to use some shared logging service
Send the log entries to a logging service - all 4 programs would send their logs to a logger application that would write them to file - see Syslog for inspiration
I am using log4j to write all the logs in a file.
i have 2 different java projects say proj1 and proj2, where project1 is required project for proj2. I have added proj1 as a dependency for proj2.
proj1 has log4j setup done and is working fine.
Now my problem is when I am running a method in proj2, it will call proj1 as well.
So I want to have a single logfile for both the projects.
Any input please?
There are several ways to write to a single log file but which way is best depends on a couple of details which you omit.
If proj2 includes proj1 as a library, you can make it use proj1's log4j configuration file. This works because you only have a single VM. The most simple solution here is to either copy the first project's config into the other or not give the second project any log config; it will then read the config from its dependencies.
If proj2 starts proj1 as an external process, you need to configure both projects to use a SocketAppender since only a single Java VM can ever write to a single log file.
Related:
log4j: How to use SocketAppender?
I was also facing the same problem but i got the solution and
have configured my log4j.xml like this:
used a appender:
<appender name="FILE1" class="org.apache.log4j.RollingFileAppender">
<errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler" />
<param name="File" value="E:/OESController.log" />
<param name="Append" value="true" />
<param name="MaxFileSize" value="20KB" />
<param name="MaxBackupIndex" value="2" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %d{dd MMM yyyy HH:mm:ss.SSS} %-5l - %m%n%n" />
</layout>
</appender>
<!-- Root Logger -->
<root>
<priority value="error" />
<appender-ref ref="FILE" />
</root>
Note:*Root logger logs for entire application.*
let me know if you still face the problem.
We are starting a Spring MVC based web application. We will be using tomcat as the web server.
I need to configure log4j in the application and log to the application specific file and not to the tomcat log files.
e.g. tomcat has its own log files like localhost.log etc. I want something like myAppName.log in the tomcats log folder.
The logging configuration will go in lo4j.xml or log4j.properties in the application war file.
Plus I dont want to hard code the output log file in the web application.
But I am not sure how to do this.
Please help me. As well correct me if I am wrong somewhere.
Do like this, initialize the logger with following code,
Logger log = Logger.getLogger(this.getClass());
log the information like follows,
log.debug("My message");
and place the log4j.xml in your class path. content like follows,
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="infoLogsFile" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="MyApplication.log"/>
<param name="Threshold" value="DEBUG"/>
<param name="MaxFileSize" value="100MB"/>
<param name="ImmediateFlush" value="TRUE"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<root>
<priority value ="DEBUG" />
<appender-ref ref="infoLogsFile"/>
</root>
</log4j:configuration>
Do not forget to add the required jars like log4jXXX.jars and apache common logging jars. with this you will be able to see all log messages in MyApplication.log file creates in bin folder of your tomcat.
Try this:
Put the log4j jar as part of the web application
Do not put a configuration as part of your web application
Create your log4j.xml wherever you like
When you start tomcat provide this argument
-Dlog4j.configuration=file:///.../log4j.xml
I have a spring application that has configured log4j (via xml) and that runs on Tomcat6 that was working fine until we add a bunch of dependencies via Maven. At some point the whole application just started logging part of what it was supposed to be declared into the log4.xml
"a small rant here" Why logging has to be that hard in java world? why suddenly an application that was just fine start behaving so weird and why it's so freaking hard to debug?
I've been reading and trying to solve this issue for days but so far no luck, hopefully some expert here can give me some insights on this
I've added log4j debug option to check whether log4j is taking reading the config file and its values and this is what part of it shows
log4j: Level value for org.springframework.web is [debug].
log4j: org.springframework.web level set to DEBUG
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [org.compass] additivity to [true].
log4j: Level value for org.compass is [debug].
log4j: org.compass level set to DEBUG
As you can see debug is enabled for compass and spring.web but it only shows "INFO" level for both packages. My log4j config file has nothing out of extraordinary just a plain ConsoleAppender
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
What's the trick to make this work? What it's my misunderstanding here?
Can someone point me in the right direction and explain how can I make this logging mess more bullet proof?
It might not be log4j that is doing the logging, and hence your log4j config would be ignored. Spring logs using Commons Logging, which is an api that can delegate to various logging frameworks, including log4j. To decide which implementation to use, commons logging looks into the classpath.
If you have added a dependency that dragged its own logging implementation into the classpath, commons logging might now use the other implementation.
I recommend to set a breakpoint at a call to the logging facility, and trace the execution to see which logging implementation is used.
As it was working until you loaded a number of dependencies via Maven, perhaps there's a Log4j configuration loaded inadvertently via these dependencies ?
Run mvn dependency:tree to see what's being loaded, and then see if any of these dependencies has a Log4j configuration.
I think your problem is that you're not setting the threshold param on your appenders, and (maybe) because you're not assigning those appenders to your loggers.
Try adding param name="threshold" value="debug" to your appenders and then explicitly adding them to the specific (or root) loggers like so:
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="threshold" value="debug" />
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<logger name="org.springframework.web">
<level value="debug" />
<appender-ref ref="console" />
</logger>
Also, this page says "This appender will not log any messages with priority lower than the one specified here even if the category's priority is set lower" which is probably the source of your problem.
This seems like a good thread for you to read:
http://forum.springsource.org/showthread.php?t=88250
Cutting to the chase, it seem that the poster had a problem with a Tomcat security setting. An updated Tomcat policy file fixed the issue.
Probably has something do with reading outside of the webapp for your log4j.xml file.